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