I’m goofing around with a script that automates a backup of my system. I want the application to run at startup and run ONLY if it is Monday, Wednesday, or Friday AND if the time is 8AM to 11AM. Otherwise, I want it to quit.
I’ve gotten as far as what you see below (cut off at the “tell application ‘finder’” part, because I’ve gotten that part right. AppleScript tells me there is something wrong with my “else”'s, but when I remove the conditionality it just runs the backup after failing the date/time test.
Ideas?
set work_Days to {Monday, Wednesday, Friday}
if work_Days contains ((current date)'s weekday) then
end if
else
tell application "backup" to quit
end if
if (current date)'s time > 28800 and (current date)'s time < 39600 then
end if
else
tell application "backup" to quit
end if
tell application "Finder"
run
if exists disk "ELYK" then
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:AddressBook:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:AddressBook:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):Application Support:"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:untitled folder:" to "AddressBook"
copy folder "Lucifer:Users:TheEdge:Library:Application Support:AddressBook:" to folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:AddressBook:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:Firefox:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:Firefox:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):Application Support:"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:untitled folder:" to "Firefox"
copy folder "Lucifer:Users:TheEdge:Library:Application Support:Firefox:" to folder "ELYK:BACKUPS:AutoBackup (latest):Application Support:Firefox:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Desktop:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Desktop:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Desktop"
copy folder "Lucifer:Users:TheEdge:Desktop:" to folder "ELYK:BACKUPS:AutoBackup (latest):Desktop:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Documents:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Documents:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Documents"
copy folder "Lucifer:Users:TheEdge:Documents:" to folder "ELYK:BACKUPS:AutoBackup (latest):Documents:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Music:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Music:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Music"
copy folder "Lucifer:Users:TheEdge:Music:" to folder "ELYK:BACKUPS:AutoBackup (latest):Music:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Nonsense:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Nonsense:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Nonsense"
copy folder "Lucifer:Users:TheEdge:Nonsense:" to folder "ELYK:BACKUPS:AutoBackup (latest):Nonsense:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Pending:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Pending:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Pending"
copy folder "Lucifer:Users:TheEdge:Pending:" to folder "ELYK:BACKUPS:AutoBackup (latest):Pending:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Pictures:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Pictures:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Pictures"
copy folder "Lucifer:Users:TheEdge:Pictures:" to folder "ELYK:BACKUPS:AutoBackup (latest):Pictures:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Public:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Public:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Public"
copy folder "Lucifer:Users:TheEdge:Public:" to folder "ELYK:BACKUPS:AutoBackup (latest):Public:"
end if
if exists folder "ELYK:BACKUPS:AutoBackup (latest):Things:" then
delete folder "ELYK:BACKUPS:AutoBackup (latest):Things:"
make new folder at "ELYK:BACKUPS:AutoBackup (latest):"
set name of folder "ELYK:BACKUPS:AutoBackup (latest):untitled folder:" to "Things"
copy folder "Lucifer:Users:TheEdge:Things:" to folder "ELYK:BACKUPS:AutoBackup (latest):Things:"
end if
end tell
end if
Thanks in advance for help!
Model: MacBook 13"
AppleScript: 2.2
Browser: Firefox 3.0.1
Operating System: Mac OS X (10.5)
OS X has a built-in backup program called rsync, accessible by the shell.
It creates the folders at destination location, copies only the new or changed files and deletes files, which no longer exist at source location.
It has a hugh amount of parameters and is very powerful.
set {weekday:wk, time:tm} to (current date)
if wk is in {Monday, Wednesday, Friday} and (tm > 28800 and tm < 39600) then
set destination to "/Volumes/ELYK/BACKUPS/AutoBackup (latest)/"
set homefolder to POSIX path of (path to home folder)
set appSupportFolder to POSIX path of (path to application support folder from user domain)
-- folderList contains all folders to be backuped, all items must be a quoted POSIX path
set folderList to {quoted form of (appSupportFolder & "AddressBook/"), quoted form of (appSupportFolder & "FireFox/"), quoted form of POSIX path of (path to desktop), ¬
quoted form of POSIX path of (path to pictures folder), quoted form of POSIX path of (path to music folder), quoted form of POSIX path of (path to shared documents folder), ¬
quoted form of POSIX path of (path to documents folder), quoted form of (homefolder & "Nonsense/"), quoted form of (homefolder & "Things/"), quoted form of (homefolder & "Pending/")}
-- coerce the path list to a space delimited string
set {TID, text item delimiters} to {text item delimiters, space}
set source to folderList as text
set text item delimiters to TID
-- make the backup, preserve all Finder attributes and deletes files at destination location, which no longer exist at source location
do shell script "/usr/bin/rsync -atE --delete " & source & space & quoted form of destination
end if
else is used to divide an if block statement into things to do if the condition is true and alternative things to do if it’s not.
if work_Days contains ((current date)'s weekday) then
-- Do this.
else
-- Do this instead.
end if
If no alternative actions are required, the else is omitted.
if work_Days contains ((current date)'s weekday) then
-- Do this.
end if
So:
set work_Days to {Monday, Wednesday, Friday}
if work_Days contains ((current date)'s weekday) then
tell application "backup" to quit
end if
if (current date)'s time > 28800 and (current date)'s time < 39600 then
tell application "backup" to quit
end if
However:
These can be combined into one if statement.
It’s inefficient and slightly risky to use the current date function several times in a row. The time ” and even the date ” could change between one call of the function and the next.
A script application won’t quit until it’s finished running the script, so you have to stop the script execution instead. A non stay-open script app will then quit of its own accord anyway.
set work_Days to {Monday, Wednesday, Friday}
set currentDate to (current date) -- Get the current date just once and store it in a variable.
set wkday to currentDate's weekday
set t to currentDate's time
if work_Days contains wkday or (t > 28800 and t < 39600) then
error number -128 -- Stop the script execution with a "User canceled" error
end if
Everywhere where you have copy in your Finder tell block, it should be duplicate.
Shell script is the way to go, but in case you’re interested in another Applescript method:
set work_Days to {Monday, Wednesday, Friday}
if work_Days does not contain ((current date)'s weekday) then
tell application "Backup" to quit
end if
if not ((current date)'s time > 28800 and (current date)'s time < 39600) then
tell application "Backup" to quit
end if
set Folderlist to {"Addressbook", "Firefox", "Desktop", "Documents", "Music", "Nonsense", "Pending", "Pictures", "Public", "Things"}
set x to "ELYK:BACKUPS:AutoBackup (latest):"
set Luc to "Lucifer:Users:TheEdge:"
tell application "Finder"
repeat with eachfolder in Folderlist
if (eachfolder contains "Addressbook") or (eachfolder contains "Firefox") then
set nw to x & "Application Support:" & eachfolder
set nw2 to x & "Application Support:"
set thesource to Luc & "Library:Application Support:" & eachfolder
else
set nw to x & eachfolder
set nw2 to x
set thesource to Luc & eachfolder
end if
try
delete folder nw
set newfolder to make new folder at nw2 with properties {name:eachfolder}
duplicate folder thesource to folder newfolder
end try
end repeat
end tell
This is untested, so take care.
Hmmm,
Here’s what I think you’re doing with the if statements:
set work_Days to {Monday, Wednesday, Friday}
if work_Days contains ((current date)'s weekday) and (((current date)'s time > 28800 and (current date)'s time < 39600)) then
set Folderlist to {"Addressbook", "Firefox", "Desktop", "Documents", "Music", "Nonsense", "Pending", "Pictures", "Public", "Things"}
set x to "ELYK:BACKUPS:AutoBackup (latest):"
set Luc to "Lucifer:Users:TheEdge:"
tell application "Finder"
repeat with eachfolder in Folderlist
if (eachfolder contains "Addressbook") or (eachfolder contains "Firefox") then
set nw to x & "Application Support:" & eachfolder
set nw2 to x & "Application Support:"
set thesource to Luc & "Library:Application Support:" & eachfolder
else
set nw to x & eachfolder
set nw2 to x
set thesource to Luc & eachfolder
end if
try
delete folder nw
set newfolder to make new folder at nw2 with properties {name:eachfolder}
duplicate folder thesource to folder newfolder
end try
end repeat
end tell
end if
Thanks to Nigel and cwt for your insights. I think I follow what you are suggesting and will play around with it soon.
@ Stefan–thank you as well for your suggestion, though short of someone giving me a rundown on shell scripts I have no idea how to apply what you’ve suggested. AppleScript is complicated enough for a dabbler like myself!
The script does exactly the same as yours, it backups the specified folders and their entire contents to the folder “ELYK:BACKUPS:AutoBackup (latest):”
at the specified times. The rsync way is more efficient and faster because it doesn’t involve the Finder.