Okay here is my situation. I have created a script whose function is to ultimately back up my script files which I save to my iCloud Drive/Script Editor folder, to two different locations. Each location is in a folder named “JIMZ_Scriptz” on two different Time Capsules. Initially I duplicated every item located in my iCloud Drive/Script Editor folder to the “JIMZ_Scriptz” on both of my Time Capsules. Because every file is already duplicated, the function of my script now is to only duplicate the files of my iCloud drive/ Script Editor folder, whose creation date is within the last three days and who's modification date is within the last three days to the “JIMZ_Scriptz” on both of my Time Capsules
In my research and learning process, I’ve stumbled across … how to have an AppleScript change the values of its already set properties based on how many times the script is run. For example I have this script set up that on every fifth run, it will set the value of this property… property archiveMode : missing value
… to true. Next I have a conditional set in this script that if archiveMode is true, then it will perform the actions I set in the conditional statement.
But here is the catch. As long as the script does not get recompiled and resaved, with every run of the script in Script Editor, it will continue to change the value in the property number_of_runs : 0
from 1 to 2 to 3 and so on. When compiled and saved as an application and that application is run outside of Script Editor, it loses the ability to save and store new values to properties and variables. Right now I have the properties set in a way that every time I launch the compiled application, it sets archiveMode to true, displaying a dialog asking me if I want to run those certain actions.
Q: How To Have A Custom Action Located Within An Applescript.app, Execute Only On Every Fifth Launch Of The Application?
property inputPath1 : alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
property inputPath2 : alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
property inputPath3 : alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
property destinationOne : "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz"
property destinationTwo : "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"
property modiedDate : (current date) - (days * 3)
property creationDate : (current date) - (days * 3)
property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_runs : 0
property number_of_files : 0
set number_of_runs to number_of_runs + 1
if number_of_runs = 5 then
set archiveMode to true
end if
stopTimeMachine()
set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"
if archiveMode is true then
try
set dialogAnswer to display dialog ¬
"Would You Like To Create An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"Yes", "No", "Cancel"} ¬
default button "Yes"
if button returned of dialogAnswer is "Yes" then
do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
tell application "Finder"
set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
set moveZipFile to moveZipFile as alias
set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set number_of_files to count (items of folder moveZipFileTo)
set number_of_files to number_of_files + 1
set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
set name of moveZipFile to new_zip_name
set resultObject to move moveZipFile to moveZipFileTo without replacing
set resultObject2 to duplicate resultObject to moveZipFile2
end tell
set archiveMode to false
else
backupScriptz()
end if
on error number -128 -- userCanceledErr
stopTimeMachine()
return
end try
set archiveMode to false
end if
backupScriptz()
stopTimeMachine()
on backupScriptz()
tell application "Finder"
try
with timeout of 280 seconds
duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath1 whose creation date > creationDate) to destinationOne with replacing
duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath2 whose creation date > creationDate) to destinationOne with replacing
duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath3 whose creation date > creationDate) to destinationOne with replacing
end timeout
with timeout of 280 seconds
duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath1 whose creation date > creationDate) to destinationTwo with replacing
duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath2 whose creation date > creationDate) to destinationTwo with replacing
duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath3 whose creation date > creationDate) to destinationTwo with replacing
end timeout
on error the error_message number the error_number
set the error_text to "Error: " & the error_number & ". " & the error_message
my write_error_log(the error_text)
end try
end tell
end backupScriptz
on write_error_log(this_error)
set the error_log to ((path to desktop) as text) & "Jimz_Time_Capsule_Backup Error Log.txt"
try
open for access file the error_log with write permission
write (this_error & " " & (current date) & return) to file the error_log starting at eof
close access file the error_log
on error
try
close access file the error_log
end try
end try
end write_error_log
on stopTimeMachine()
tell application "System Preferences"
reveal anchor "main" of pane "com.apple.prefs.backup"
tell application "System Events"
perform action "AXPress" of checkbox "Back Up Automatically" of window "Time Machine" of application process "System Preferences"
end tell
end tell
tell application "System Preferences" to quit
end stopTimeMachine
UPDATE: Here Is The Version Of The Revised Code. This Updated Code Will Indeed Perform The Action Located Within The Conditional Clause On Every Fifth Launch Of The Application.
The approach I took was to use the “write to file” command. Every time the script application gets launched, it writes the word “Launched” to an external text file. Then I used the “read file” command to count the number of the words “Launched” in the text file. Next I copied that count to a variable and if that variable = 5 it triggers the appropriate actions and deletes the external text file, which starts the cycle back from the beginning
property inputPath1 : alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
property inputPath2 : alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
property inputPath3 : alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
property destinationOne : "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz"
property destinationTwo : "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"
property modiedDate : (current date) - (days * 3)
property creationDate : (current date) - (days * 3)
property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_files : 0
property launchCount : 0
writeToFile()
readFile()
if launchCount = 5 then
set archiveMode to true
tell application "Finder"
delete alias "Macintosh HD:private:tmp:Launch_Count.txt"
end tell
end if
stopTimeMachine()
set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"
if archiveMode is true then
try
set dialogAnswer to display dialog ¬
"Creating An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"OK", "NO"} ¬
default button "OK"
if button returned of dialogAnswer is "OK" then
do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
tell application "Finder"
set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
set moveZipFile to moveZipFile as alias
set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set number_of_files to count (items of folder moveZipFileTo)
set number_of_files to number_of_files + 1
set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
set name of moveZipFile to new_zip_name
set resultObject to move moveZipFile to moveZipFileTo with replacing
set resultObject2 to duplicate resultObject to moveZipFile2
end tell
set archiveMode to false
else
backupScriptz()
end if
end try
set archiveMode to false
end if
backupScriptz()
stopTimeMachine()
on backupScriptz()
tell application "Finder"
try
with timeout of 280 seconds
duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath1 whose creation date > creationDate) to destinationOne with replacing
duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath2 whose creation date > creationDate) to destinationOne with replacing
duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationOne with replacing
duplicate (every item of inputPath3 whose creation date > creationDate) to destinationOne with replacing
end timeout
with timeout of 280 seconds
duplicate (every item of inputPath1 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath1 whose creation date > creationDate) to destinationTwo with replacing
duplicate (every item of inputPath2 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath2 whose creation date > creationDate) to destinationTwo with replacing
duplicate (every item of inputPath3 whose modification date > modiedDate) to destinationTwo with replacing
duplicate (every item of inputPath3 whose creation date > creationDate) to destinationTwo with replacing
end timeout
on error the error_message number the error_number
set the error_text to "Error: " & the error_number & ". " & the error_message
my write_error_log(the error_text)
end try
end tell
display notification ¬
"Your Most Recent Script Files Have Been Backed Up" with title ¬
"BACKUP COMPLETED" sound name "Bottle"
end backupScriptz
on write_error_log(this_error)
set the error_log to ((path to desktop) as text) & "Jimz_Time_Capsule_Backup Error Log.txt"
try
open for access file the error_log with write permission
write (this_error & " " & (current date) & return) to file the error_log starting at eof
close access file the error_log
on error
try
close access file the error_log
end try
end try
end write_error_log
on stopTimeMachine()
tell application "System Preferences"
reveal anchor "main" of pane "com.apple.prefs.backup"
try
tell application "System Events"
perform action "AXPress" of checkbox "Back Up Automatically" of window "Time Machine" of application process "System Preferences"
end tell
end try
end tell
tell application "System Preferences" to quit
end stopTimeMachine
on writeToFile()
set theFile to "/tmp/Launch_Count.txt"
set theText to "Launched"
try
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
on error errMsg number errNum
close access theFile
set writeToFile to open for access theFile with write permission
write theText & linefeed to writeToFile as text starting at eof
close access theFile
end try
end writeToFile
on readFile()
set theFile1 to alias "Macintosh HD:private:tmp:Launch_Count.txt"
set theCount to count words of (read theFile1)
copy theCount to launchCount
end readFile
UPDATE #2: So I downloaded a scripting addition called Satimage , and reworked my code as a combination of commands from Satimage (mainly the BACKUP command) combined with some of my previous code from the UPDATE post in this topic.
Here is the latest version
Q: Can anybody try explaining to me why this new version takes only 18 seconds to complete, without having to use timeout clauses (not including the “archive command” on the fifth launch of the application), whereas the original solution usually took the better part of 20 minutes? How can a third party scripting addition perform 1000 times quicker than Finder.app scripting commands?
property archiveMode : missing value
property zipMe1 : POSIX path of "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz"
property zipMe2 : POSIX path of "/Volumes/Data_Jimmy's_ATC/Jimz_Filez/JIMZ_Storage_Filez/JIMZ_Scriptz"
property number_of_files : 0
property launchCount : 0
writeToFile()
writeToFile2()
readFile()
if launchCount = 5 then
set archiveMode to true
tell application "Finder"
delete alias "Macintosh HD:private:tmp:Launch_Count.txt"
end tell
end if
if archiveMode is true then
try
set dialogAnswer to display dialog ¬
"Creating An Archive Of Your JIMZ_Scriptz Folders In Both Of Your Time Capsules?" buttons {"OK", "NO"} ¬
default button "OK"
if button returned of dialogAnswer is "OK" then
do shell script "zip -r " & quoted form of zipMe1 & " " & quoted form of zipMe1
tell application "Finder"
set moveZipFile to POSIX file "/Volumes/Data_Smokestack_ATC/JIMZ_Storage_Filez/JIMZ_Scriptz.zip"
set moveZipFile to moveZipFile as alias
set moveZipFile2 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set moveZipFileTo to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:Zipped_Jimz_Scriptz:"
set number_of_files to count (items of folder moveZipFileTo)
set number_of_files to number_of_files + 1
set new_zip_name to "JIMZ_Scriptz " & number_of_files & ".zip"
set name of moveZipFile to new_zip_name
set resultObject to move moveZipFile to moveZipFileTo with replacing
set resultObject2 to duplicate resultObject to moveZipFile2
end tell
set archiveMode to false
else
backupScriptz()
end if
end try
set archiveMode to false
end if
on writeToFile()
set MountdestinationOne to mount volume "afp://Jimmy's AirPort Time Capsule._afpovertcp._tcp.local/Data_Jimmy's_ATC"
set MountdestinationTwo to mount volume "afp://Smokestack AirPort Time Capsule._afpovertcp._tcp.local/Data_Smokestack_ATC"
set theSource to alias "Macintosh HD:Users:Smokestack:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:"
set theSource2 to alias "Macintosh HD:Users:Smokestack:Library:Script Libraries:"
set theSource3 to alias "Macintosh HD:Users:Smokestack:Library:Workflows:Applications:Folder Actions:"
set theDestination2 to alias "Data_Smokestack_ATC:JIMZ_Storage_Filez:JIMZ_Scriptz:"
set theDestination3 to alias "Data_Jimmy's_ATC:Jimz_Filez:JIMZ_Storage_Filez:JIMZ_Scriptz:"
set resultText to backup theSource ¬
onto theDestination2 ¬
level 2 ¬
after ((get current date) - 359200)
set resultText2 to backup theSource2 ¬
onto theDestination2 ¬
level 2 ¬
after ((get current date) - 359200)
set resultText3 to backup theSource3 ¬
onto theDestination2 ¬
level 2 ¬
after ((get current date) - 359200)
set resultText4 to backup theSource ¬
onto theDestination3 ¬
level 2 ¬
after ((get current date) - 359200)
set resultText5 to backup theSource2 ¬
onto theDestination3 ¬
level 2 ¬
after ((get current date) - 359200)
set resultText6 to backup theSource3 ¬
onto theDestination3 ¬
level 2 ¬
after ((get current date) - 359200)
set theFile to "/tmp/Files_To_Backup.txt"
try
set writeToFile to open for access theFile with write permission
set theReports to write resultText & resultText2 & resultText3 & resultText4 & resultText5 & resultText6 & " " & (current date) & return to writeToFile as text starting at eof
close access theFile
on error errMsg number errNum
close access theFile
set writeToFile to open for access theFile with write permission
set theReports to write resultText & resultText2 & resultText3 & resultText4 & resultText5 & resultText6 & " " & (current date) & return to writeToFile as text starting at eof
close access theFile
end try
end writeToFile
on writeToFile2()
set theFile2 to "/tmp/Launch_Count.txt"
set theText to "Launched"
try
set writeToFile2 to open for access theFile2 with write permission
write theText & linefeed to writeToFile2 as text starting at eof
close access theFile2
on error errMsg number errNum
close access theFile2
set writeToFile2 to open for access theFile2 with write permission
write theText & linefeed to writeToFile2 as text starting at eof
close access theFile2
end try
end writeToFile2
on readFile()
set theFile3 to alias "Macintosh HD:private:tmp:Launch_Count.txt"
set theCount to count words of (read theFile3)
copy theCount to launchCount
end readFile