help me understand "try"

Hi all - looking for a little newbie help.
I’m trying to understand how the ‘try’ command behaves. As I understand it,

try
do something.
on error (if an error from the last line (do something) is returned to the try statement right?)
(then the script will do whatever’s on the next line, right?)
do this if there’s an error
end try
(it’s done, right?)

what happens if there is no error? it goes on to the next part of the script, skipping the on error part, right?
How do you make it stop running the script if there is an error? Can I add a tell me - quit command between the on error and the end try statment?
I think I’m looking at this somewhat like an if-then statement: do this - if there’s an error, then do this - else …what happens next?
someone please help me understand how this part behaves?

sigh I’m having a hard time finding good documentation of all the applescript syntax and its usage.

Also, how do you make the try statement wait until the ‘do something’ part has finifshed whatever it’s doing? I’ve seen threads about the timeout, wait, and return commands, but I’ve unable to find anything that gives me enough info to allow me to use those commands. The timeout statement is easy enough to understand, but what if I’m waiting for a user to answer a dialog and he doesn’t come to his computer for 4 days - or 4 months or whatever. It seems to make more sense to me to have the script wait for the input before continuing.

If it helps, I can post the script I’m working on and elaborate more on what it is that I’m trying to accomplish.
thanks all

Hi iwasnevy,

I wrote wrote in your last post how to quit, but you probably didn’t see it. The AppleScriptLanguageguide has good documentation on the error handler and the ScriptingAdditions has good documentation on the standard additions commands like ‘display dialog’ although it is kind of old.

http://www.apple.com/macosx/features/applescript/resources.html

The ‘quit’ command is used only in stay open scripts because stay open scripts don’t quit on their own. In non-stay open script an compiled scripts, use ‘return’ or ‘error number -128’. ‘error number -128’ is used to quit from subroutines, because a ‘return’ from a subroutine just returns from the subroutine. Here’s an example of ‘display dialog’ with error handler.


try
	display dialog "hello" giving up after 5 -- 5 seconds
set r to result
on error
	beep 2
	return
end try
beep 3
return r

If the user presses ‘Cancel’, then an error occurs, the script beeps twice and returns. Otherwise, the script sets the variable r to the result of the dialog, beeps 3 times, and returns r whose value you can see in the result. The ‘giving up after’ parameter makes the script quit on its own after 5 seconds.

Here’s an example where you can use an ‘if’ statement instead. You need to change the name of the button from “Cancel” to something else, otherwise the script will error when the user presses cancel and the script will quit right away.


display dialog "hello" buttons {"Quit", "OK"} default button "OK" giving up after 5
set r to result
if button returned of r is "OK" then
	set p to "Everything is OK"
else if button returned of r is "Quit" then
	beep 2
	return
else
	-- the user didn't respond in time
	set p to "You're too late"
end if
display dialog p

The timeout statement is not that easy to understand at the beginning. It doesn’t make the script wait for a certain time.

gl,

Thanks for clarifying that, Kel
I did see your post in the last thread, but didn’t fully grasp the concept. I understand it now. I finally found some documentation about the try statement, and used return in the script to make it quit gracefully after an error. I had thought return was used for getting a variable returned from something, but it works wonders to quit the script. AS is confusing! I also understand the timeout command and used it to keep my script from timing out. I guess what I was trying to figure out was what was happening while my rsync command was running. This stupid script has been kicking my butt for far too long, and I would have given up on it long ago if I weren’t so pig-headed!


set backupDisk to "jafar"
set backupPath to "archive:backup:"
set a to POSIX path of backupPath
tell application "Finder"
	--check if backup disk is mounted
	if disk backupDisk exists then
		do shell script "logger -s Backup: Backup disk is mounted: Looking for destination path."
	else
		do shell script "logger -s Backup: Backup disk is not mounted: Quitting normally."
		return
	end if
	--check for destination path
	if folder backupPath of disk backupDisk exists then
		do shell script "logger -s Backup: Destination path found. Performing backup."
	else
		do shell script "logger -s Backup: Destination path not found: Creating destination folders."
		do shell script "mkdir -p /Volumes/" & backupDisk & a
		do shell script "logger -s Backup: Destination folders created: Performing backup."
	end if
end tell
try
	with timeout of 21600 seconds --6 hours
		do shell script "rsync -aEc --exclude .Spotlight-V100 --exclude .Trashes --delete ~ /Volumes/" & backupDisk & a
	end timeout
on error errMsg
	do shell script "logger -is Backup: Error reported by rsync: " & quoted form of errMsg & ": Quitting."
	activate
	display dialog "Rsync reported a problem backing up your files. Please see the system log for details." with icon 2 buttons {"Show Log", "OK"} default button 2
	if button returned of result is "show log" then
		tell application "Console"
			open ":var:log:system.log"
			activate
		end tell
	end if
	return
end try
do shell script "logger -is Backup: Completed successfully: Quitting."
tell application "Finder"
	activate
	display alert "Your files have been backed up."
end tell

So far, this all works as intended. The rsync command was taking so long to complete that the script would time out, and I was trying to figure out what was going on. After the rsync shell finished, on error or not, it would finish out the script. the return statement fixed that, so that’s what I was trying to understand. It seems like there should be a more graceful way to wait for the rsync shell to finish. What if it’s transferring a load of files that takes 3 days? (I know that probably wouldn’t happen, but what if?) Anyway, I’m nearly finished with it, I think. I’ve just got to run a full test to be sure it works, and if it doesn’t, I’m about ready to give up! :slight_smile:
Thank you for your help!
-Kevin

Hi iwasnevy,

Glad you got it running.

The thing about a script is that when you don’t place a run handler in it, then there is an implicit run handler. For instance, when you run a command like this:

display dialog “hello”
return
beep 3

it’s the same thing as this:

on run
display dialog “hello”
return
beep 3
end run

When you use return, you’re retuning from the run subroutine. If you don’t use a return, the last result of a command is returned, automatically with an implicit return.

About, your script and the timeout. I don’t know how to do this, but maybe you can run the shell script in the background somehow. I think You use an “&” at the end of the shell script. Not sure how you do this and you might make another post about this.

gl,

Thanks kel
I’m starting to understand this stuff a little better. I’m a newbie and I thought I’d give this backup thing a try with applescript. I was simply running rsync in an automator action scheduled in Cron, and this whole script thing started when I realized that my files weren’t being backed up properly, and I had no clue because there was no error reporting. I thought I could use a simple applescript to report errors, but It’s been such a headache to get something so simple done that I now know that I don’t want to write AS or do programming for a living! I’m ok with the timout at 6 hours or whatever - it works for me.
It is intriguing (sp?) tho, what can be done with it. Like an addiction, I want to keep working on it, even though I know it’s bad for my sanity! :slight_smile:
Next challenge: how to make it check for adequate free space before running rsync - but half of me says - what’s the point, rsync will return an error on its own if there’s not enough free space, and I’ve got that part covered. I started out with something like this:

set x to physical size of path to home folder
set y to free space of backupDisk
if x > y then
error not enough space

but the problem was that if the backup had already been done, (the previous day) the script didn’t take into account that the backup folder is already full of data and would be overwritten, allowing enough space. The existing backup path would have to be taken out of the freespace equation, so I thought something like this might work: (not necessarily in its proper syntax yet):

set x to size of path to home folder
set y to free space of backupDisk
if bakupPath exists, set n to physical size of backupPath
y + n = y
end if
if x > y then
error not enough space
else
space is good
end if

Assuming that the backup path is /archive/bacup/, and this is specified in the variable ‘backupPath’ at the beginning of the script; The problem is that I’ve got several other items stored in ‘archive’ but not backup, so it has to get the size of the folder ‘backup’ not ‘archive’. Keep in mind that I want to keep the backup path in the variable set at the beginning of the script. How to specify getting the size of only the last backup folder without breaking up the variable “backupPath” set at the beginning of the script? did that make sense?
aaargh!
see, my head is spinning already. I actually had a dream the other night that I was being attacked by applescript commands… :slight_smile:
thanks for your help
-Kevin

Actually, I don’t understand what’s the problem. :slight_smile:

If free space - size of backup folder > size of home folder then
go
else
no go
end if

Yeah, I don’t see what the problem is.

gl,

What does this have to do with try blocks?

lol
nothing - I got a little off track. this should be a new post.