Exit With Error Value

I have an applescript.app which I want to use inside of a shell script to check the status of a VPN connection.

Within the applescript I can get the various results corresponding to the status of the connection, but I cannot see how I can then pass this back to the shell script which called it. Using 'Return' statements do not change the exit value of the script itself.

I.e., the applescript always returns '0' - since it completes successfully.

Is there any way I can force it to complete with some other exit value?

Ta.
-david

Server 10.4.8

Posted on Jan 17, 2007 10:17 AM

Reply
5 replies

Jan 17, 2007 11:57 AM in response to Camelot

Thanks for quick reply, Camelot 🙂

I now have the following for the status script...

-------------------------------
set the_file to (((path to desktop) as string) & "exitVal.txt")
set nref to open for access file the_file with write permission
close access nref

tell application "Internet Connect"
-- state= 8 if established
-- state = 5 if authenticating
-- state = 0 if closed
if state of (status of configuration "VPN (PPTP)") = 8 then
write "open" to file the_file
else
write "closed" to file the_file
end if
end tell
-------------------------

Again, I've cobbled it together from a web search so let me know if there is anything 'strange' in it.

Ta.
-david

Jan 17, 2007 12:26 PM in response to David_x

Yes, actually, the other answer you got was wrong.

Just throw an error inside the script, probably (given what you want to do) at the end. For example:

osascript -e 'error "This is a test." number 9000'

on the command line will give (on the command line) something like:

6:22: execution error: This is a test (-128)

If you read the man page for osascript, you will see that by default script errors are written to stderr; if you have your shell script watch stderr between the start of the script and the end of it, you'll catch the result message.

A few things to keep in mind:

--> AppleScript can catch its own errors using a "try" block, similar to in other languages. The basic syntax is:

try
(* Do some stuff here *)
on error msg number num
(* Process the error here *)
end try

--> If you throw an error yourself, try to use an error number which is "safe". Most of the negative numbers are already in use as standard signals. For example, -128 is "User canceled", while -43 is "file not found" (which pops up if you cast a string to an alias and the string does not describe a path to a valid item in the filesystem -- that's actually a very simple way to test for file existence). If you use numbers starting with 9000, which seems to be the consensus starting point, then you have the additional advantage of being able to figure out where things went wrong.

Jan 17, 2007 2:25 PM in response to John Franklin1

Good one, John! 🙂

That 'osascript' looks quite useful for this sort of thing as it allows 'returns' in text form as well.

So I can have an Applescript like this..

----------------------------
tell application "Internet Connect"
-- state= 8 if established
if state of (status of configuration "VPN (PPTP)") = 8 then
return "open"
else
return ""
end if
end tell
----------------------------

The reason for the emptystring return in the 'else' statement allows for a simple test in the following shell script...

----------------------------
#!/bin/sh

CHECK_VPN="`osascript ${SCRIPTDIR}/vpnStatus3.app`"

if [ $CHECK_VPN ] ; then
echo 'VPN is open'
else
echo 'VPN is closed'
fi
----------------------------


Very neat! 🙂

-david

Server 10.4.8

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Exit With Error Value

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.