Handler doesn't repeat like i think it should

i’ve got this script that is designed to see if a network drive has been mounted, if not, it attempts to mount it, and if it can’t, it gives the user the option to try again, work offline, or quit.

The way I’ve written it, it seems to me that if it can’t mount the drive, but the user clicks on “Try Again” each time it fails, then this script should run forever, or until it successfully mounts the drive. However, currently, this script will only repeat once if you click “Try Again.”

After you click try again the second time, the script ends and I’m not exactly sure why.

Hopefully someone here can help me get a better understanding of what the script is doing!

Thanks
Joel


global theButton
mountDrive("User", "drowssap")

on mountDrive(u, p)
	tell application "Finder"
		with timeout of 60 seconds
			if exists disk "Drive" then
				return
			else
				try
					with timeout of 20 seconds
						mount volume "afp://192.168.1.1:548/user" as user name u with password p
					end timeout
				on error
					tell application "Finder"
						activate
					end tell
					display dialog "The server could not be found. Would you like to work offline?." buttons {"Yes", "Stop", "Try Again"} default button 1 with title "Could Not Locate Server" with icon 1
					set theButton to the button returned of the result
				end try
			end if
		end timeout
	end tell
end mountDrive

if theButton is equal to "Yes" then
	return
else
	if theButton is equal to "Stop" then
		myError("User Cancelled Script") of me
	else
		if theButton is equal to "Try Again" then
			mountPSC("User", "drowssap")
		end if
	end if
end if


on myError(theError)
	tell application "Finder"
		activate
		display dialog theError buttons {"Ok"} default button 1 with icon 1 with title "Script Cancelled"
	end tell
end myError

Model: Macbook
AppleScript: 2.2.1
Browser: Firefox 3.0.5
Operating System: Mac OS X (10.5)

It looks like you have no repeat loop, so you are only calling the handler twice. Once you call it the second time the script ends.

Hi Dj,
thanks for responding so quickly!

So there’s no explicit repeat loop, but shouldn’t the

       if theButton is equal to "Try Again" then
           mountPSC("User", "drowssap")

part of the script act like a psuedo repeat loop?

everytime the user clicks “Try Again” shouldn’t it call that handler, and effectively make it repeat?

Model: Macbook
AppleScript: 2.2.1
Browser: Firefox 3.0.5
Operating System: Mac OS X (10.5)

I think that the code in the run handler should only run once unless it is in a repeat loop.

Perhaps something like this could work?

		
		if theButton is equal to "Try Again" then
			repeat until theButton is not equal to "Try Again"
				mountPSC("User", "drowssap")
			end repeat
		end if

Hi,

there is no mountPSC() handler.
Nested blocks like the Finder tell block and the timeout block is not recommended.
The inner timeout block is useless, because scripting addition commands like mount volume don’t cause timeout errors.
You can omit also the outer timeout block and the outer Finder tell block, if you check the existence of the disk with


.
if  "Drive" is in (do shell script "/bin/ls /Volumes") then
		return
	else
.

Hi guys,
the “mountPSC” should read “mountDrive,” my typo.

thank you both for your input, both suggestions have been very helpful! I’m still wary of shell scripting, only because I haven’t had the time to familiarize myself with it just yet, but it works perfectly, so thanks Stefan.

And the repeat loop seems to have resolved my quitting issue as well, thanks again DJ.

I don’t know what I’d do without this website!

Thanks again for all of your help

Joel