What is the command to make a Application Compiled script to just shut up and go away when an error occurs? We have server mount scripts, but if someone cancels the mount dialog box the script shows a failure message… and you know end users, they don’t like seeing error messages… I know there is the On Error step, but I don’t know where to put it in my script or what the additional part of the command would be to silence and quit the script. Thanks!
My current scripts look like this:
set serverIP to "10.10.10.10"
set ShareName to "MyMount"
try
set ping_result to (do shell script "ping -c 1 -q " & serverIP)
end try
tell application "Finder"
activate
if "100% packet loss" is not in ping_result then
if (list disks) does not contain ShareName then
mount volume "afp://" & serverIP & "/" & ShareName
end if
end if
end tell
If I might suggest a completely different kind of solution, what about something like below. This trys to connect for a while and then gives up.
set start_time to current date
set keep_trying to true
repeat until keep_trying is false
try
tell application “Finder”
activate
if (list disks) does not contain ShareName then
mount volume “afp://” & serverIP & “/” & ShareName
end if
end tell
on error
set keep_trying to true
end try
if (current date) - start_time > 30 then set keep_trying to false
--This gives the loop a 30 second timeout if all else fails
I just realized a mistake in the code I posted before. Here’s the correction below.
BTW, I originally wrote a control loop like this to open a sub folder from a Windows share which had a space in the name because smb:// currently won’t work with any spaces in the URL, no matter how you try to enclose it in quotes or encode the spaces or anything like that I have tried. I needed to have the script connect to the share and then keep trying to open the sub folder until the share was actually mounted. In the case of trying to connect to the share, itself, you might want to adjust the timeout and add a delay between tries, probably using the “delay” command.
set start_time to current date
set keep_trying to true
repeat until keep_trying is false
try
tell application “Finder”
activate
if (list disks) does not contain ShareName then
mount volume “afp://” & serverIP & “/” & ShareName
end if
set keep_trying to false
end tell
on error
set keep_trying to true
end try
if (current date) - start_time > 30 then set keep_trying to false
--This gives the loop a 30 second timeout if all else fails
Hmm, as I understand it, the “with time out of …” code just allows you to change the default timeout for Apple Events to something else. In other words, it won’t actually repeat an attempt to do something for you, it just lets you choose the time limit for one attempt to complete. You would think the code below would do the same sort of thing as my loop above but it doesn’t. What it does is kill the whole script whenever any Apple Event from any iteration of the loop times out. As long as each Apple Event takes less then 30 seconds, the loop is infinite.
with timeout of 30 seconds
repeat
-- insert code to do something
end repeat
end timeout
Let me expand on what PreTech said. I just remembered something about the default behavior of dialog boxes in AppleScript. If you have a dialog box in a repeat loop and the user hits “cancel” it will break the loop. The “exit repeat” command, in the script below, will be skipped if there is an error because the control loop goes right to the “on error” statement as soon as there is an error.
Here’s an interactive form of the control loop:
repeat
try
-- do stuff
exit repeat
on error
display dialog "Should I keep trying?"
end try
end repeat