Re: Getting values returned by Unix command-line programs
Re: Getting values returned by Unix command-line programs
- Subject: Re: Getting values returned by Unix command-line programs
- From: steve harley <email@hidden>
- Date: Thu, 18 Mar 2004 00:52:08 -0700
on 17 Mar 2004, at 9:35 PM, Rob Lewis wrote:
In Applescript, how do you retrieve values returned by Unix
command-line programs? The program of interest outputs a
space-delimited string of values, which I can retrieve with:
set retval to do shell script "myprog"
set AppleScript's text item delimiters to " "
set newText to text items of retval
set myvar to item 4 of newText
But the developer told me that the program also exits with a value of
0 or 1 depending on its result. How do you pick up this value?
a nonzero return status from the shell command throws an error, so you
could use a try block:
try
set retval to do shell script "myprog"
on error the_text number the_return_status
-- when there's a non-zero return status,
-- the_text will contain the output (stderr & stdout combined) of
the command
-- the_return_status will be 1 in your case, anything else is a
"real" error
end error
i had just mentioned in another thread that it's unfortunate that do
shell script works this way.. i think non-zero return status from shell
commands is not semantically equivalent to AppleScript's notion of an
error.. because of that i'd lean toward something like this instead:
set retval to do shell script "if myprog ; then echo 0 ; else echo 1
; fi
which will not throw an error (unless something really goes wrong) and
will tack a newline and a "0" or "1" onto the text returned.. so you
could then:
set return_status to paragraph -1 of retval
--> "0" or "1"
there are several similar ways to do this, based on suppressing the
failure status
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.