Optimizing an ASS app.

I have taught myself Applescript starting October and have scripted a program to backup my work to my afp personal volume. I finally have everything working and am testing it but I cannot get the cancel click event to take precedence over the backup process. Any help is much appreciated.

I have 10.2.8 that is limited in what it can run by school admin. So I can’t run the example posted in other topics showing the problem. If you see any chance to streamline the code please post!

The GUI has 2 textboxes (“name” and “pass”) and 2 buttons (“backMeUp” and “cancel”). Here’s the code (sorry no formatting, lack of 10.3 holds me back):

global theirname
global theirnum
global stopitnow
global isthere
global failed
global classfoldersize

on clicked theObject
	if name of theObject is "backMeUp" then
		set isthere to true
		set stopitnow to false
		set failed to false
		set theirname to "0"
		set theirnum to "0"
		checks()
		repeat 1 times
			if contents of text field "name" of window "main" is "" then
				display alert "Wait!" as critical message �
					"You must enter both the username and password for your personal folder" default button "OK"
				
			else if contents of text field "pass" of window "main" is "" then
				display alert "Wait!" as critical message �
					"You must enter both the username and password for your personal folder" default button "OK"
				
			else if isthere is false then
				display alert "Whups!" as critical message �
					"Your ' My Classes' (note the space before My) folder was not found in the " & �
					"Users:student:Documents folder." default button "OK"
			else
				tell window "main" to set uses threaded animation of progress indicator "progress" to true
				barber(false)
				tellthem("Preparing...")
				delay 1
				tell window "main" to set minimum value of progress indicator "progress" to 0
				tell window "main" to set maximum value of progress indicator "progress" to 9
				advance(1)
				delay 1
				if stopitnow then exit repeat
				ignoring case
					set theirname to contents of text field "name" of window "main"
				end ignoring
				set theirnum to contents of text field "pass" of window "main"
				set makescript to "'#!/bin/bash'; tar cfz /Users/student/Documents/" & theirname & �
					"backup.tgz '/Users/student/Documents/ My Classes' " & �
					"'/Users/student/Library/Hog Bay Notebook'"
				set copybkup to "'#!/bin/bash'; cp /Users/student/Documents/" & theirname & �
					"backup.tgz /Volumes/" & theirname
				set loginscript to "'#!/bin/bash'; mkdir /Volumes/" & theirname & "; /sbin/mount_afp afp://" & theirname & ":" & �
					theirnum & "@freeman-students.henrico.k12.va.us/" & theirname & �
					" /Volumes/" & theirname & "; disktool -r"
				if stopitnow then exit repeat
				if (stopitnow or failed) is true then exit repeat
				foldersize()
				if classfoldersize is less than 4 then
					set failed to true
					set whyfail to "empty"
				end if
				if (stopitnow or failed) is true then exit repeat
				tellthem("Checking for your personal folder.")
				advance(2)
				delay1
				set mountedDisks to list disks
				if mountedDisks contains theirname then
					tellthem("Found!")
					delay 1
					set alreadythere to true
					if (stopitnow or failed) is true then exit repeat
				else if mountedDisks does not contain theirname then
					set alreadythere to false
					tellthem("Checking for an internet connection.")
					advance(3)
					delay 1
					if (stopitnow or failed) is true then exit repeat
					try
						do shell script "curl [url=http://www.google.com]www.google.com[/url]"
					on error
						set failed to true
						delay 1
						set whyfail to "net"
					end try
				end if
				if (stopitnow or failed) is true then exit repeat
				if alreadythere is false then
					tellthem("Logging into your personal folder.")
					advance(4)
					delay 1
					try
						do shell script loginscript
					on error
						set failed to true
						set whyfail to "pf"
					end try
				end if
				if (stopitnow or failed) is true then exit repeat
				tellthem("Collecting and compressing your work.")
				advance(5)
				delay 1
				barber(true)
				if (stopitnow or failed) is true then exit repeat
				try
					do shell script makescript
				on error
					set failed to true
					delay 1
					set whyfail to "make"
				end try
				if (stopitnow or failed) is true then exit repeat
				if alreadythere is false then
					barber(false)
					tellthem("Targeting personal folder.")
					advance(6)
					delay 1
					set mountedDisks to list disks
					if mountedDisks does not contain theirname then
						set failed to true
						delay 1
						set whyfail to "pf"
					end if
				end if
				if (stopitnow or failed) is true then exit repeat
				tellthem("Saving backup file to personal folder.")
				advance(7)
				delay 1
				barber(true)
				delay 1
				try
					do shell script copybkup
				on error
					set failed to true
					delay 1
					set whyfail to "save"
				end try
				if (stopitnow or failed) is true then exit repeat
				if alreadythere is false then
					barber(false)
					tellthem("Closing your personal folder.")
					delay 1
					advance(8)
					delay 1
					do shell script "'#!/bin/bash'; umount /Volumes/" & theirname & "; disktool -r"
				else
					barber(false)
					delay 1
				end if
				tellthem("Done.")
				advance(9)
				delay 1
				advance(0)
				tellthem("")
			end if
		end repeat
		tell window "main" to set uses threaded animation of progress indicator "progress" to false
		if stopitnow is false then
			if failed then
				tellthem("Error")
				advance(0)
				delay 1
				barber(false)
				delay 1
				if whyfail is "make" then
					beep
					display alert "Error" as critical message �
						"Could not access your work.  Make sure your ' My Classes' folder is" & �
						" in the right place." attached to window "main" default button "OK"
				else if whyfail is "net" then
					beep
					display alert "Error" as critical message �
						"Could not connect to the internet; make sure you are connected " & �
						"or if you are at school, you have School Airport location " & �
						"selected." attached to window "main" default button "OK"
				else if whyfail is "pf" then
					beep
					display alert "Error" as critical message �
						"Personal folder error.  Login to your personal folder before running " & �
						"BackerUpper." attached to window "main" default button "OK"
				else if whyfail is "save" then
					beep
					display alert "Error" as critical message �
						"Could not save to your personal folder.  It may be full.  Please delete any " & �
						"unneeded files and try again." attached to window "main" default button "OK"
				else if whyfail is "empty" then
					beep
					display alert "Error" as critical message �
						"Your ' My Classes' folder doesn't seem to have anything in it.  Move your work to the" & �
						" ' My Classes' folder in your Documents folder so that it will be backed " & �
						"up." attached to window "main" default button "OK"
				end if
			end if
		end if
	else if name of theObject is "cancel" then
		-- update status window
		set stopitnow to true
		tellthem("Cancelling")
		advance(0)
		barber(false)
		delay 1
		-- kill shell script processes
		--set listem to do shell script "'#!/bin/bash'; /bin/ps -ax"
		--set howmanyprogs to the number of paragraphs in listem
		--set loopcounter to 1
		--repeat howmanyprogs times
		--	if paragraph loopcounter of listem contains theirname or "curl" or "tar cfz" or "cp " then
		--		set psNumber to the first word of paragraph loopcounter of listem
		--		set theCommand to "'#!/bin/bash'; /bin/kill -9 " & psNumber
		--		do shell script theCommand
		--	end if
		--	set loopcounter to loopcounter + 1
		--end repeat
		tellthem("")
	end if
end clicked

on alert ended theObject with reply theReply
	tellthem("")
end alert ended

on checks()
	tell application "Finder"
		if not (exists folder " My Classes" of folder "Documents" of folder "student" of folder "Users" of startup disk) then
			set isthere to false
		end if
	end tell
end checks

on foldersize()
	tell application "Finder"
		set classfoldersize to size of folder "Macintosh HD:Users:student:Documents: My Classes"
	end tell
end foldersize

on should quit after last window closed theObject
	return true
end should quit after last window closed

on advance(here)
	tell window "main" to set contents of progress indicator "progress" to here
end advance

on barber(barber)
	tell window "main" to set indeterminate of progress indicator "progress" to barber
end barber

on tellthem(this)
	tell window "main" to set contents of text field "statusMessage" to this
end tellthem

Ok, I’ll give it a shot, but I’m new to ASS too, so take it for what it’s worth…

firstly, I like to keep the “on clicked” (and other similar) handlers as uncluttered as possible. this is a achieved like this:
Instead of putting your main script inside the “on clicked” handler, you put somewhere else (at the bottom of the page for exapmple, or just ender the “end clicked” line).

you enclose your main script with something like “on do_main()”, and you put a comand in the on_clicked handler that calls this. for example:

on clicked
	if the name of the_object is "Backup" then
		my do_main()
	end if
end clicked


on do_main()
	
	-- insert your main script here
	
end do_main

this make it much easier to get around and see what’s what and troubleshoot problems.
do get your cancel button to work you would do something like this:

put a handler for the ok button in the on_clicked event handler:

on clicked
	if the name of the_object is "Backup" then
		my do_main()
	else if name of the_object is "cancel" then
		set stop_backup to yes
	end if
end clicked


on do_main()
	
	-- insert your main script here
	
end do_main

then what I have done in a similar situation is put something like this at various points throughout the main script (such as at the beginning of a repeat, and after delays):

on do_main()
	repeat
		
		if stop_backup is yes then
			exit repeat
		end if
		
		
		-- your script
		
		
	end repeat
end do_main

replace “exit repeat” with stop or quit, or anything else that’s appropriate.
I’m not sure what the ()s are for in

 on do_main()

but they are important.

that should give you some ideas to start experimenting with at the least,
good luck,
arumdevil