I have a simple AppleScript that opens certain "housekeeping" applications that I have created.
Can I update it so the applications are opened only if they haven't been opened in the past 24 hours?
Many thanks!
tell application "Finder"
open application file "clean-exports.app" of folder "Applications" of startup disk
open application file "clean-queue.app" of folder "Applications" of startup disk
open application file "clean-dropbox.app" of folder "Applications" of startup disk
end tell
A possible solution is to write a timestamp to UserDefaults
. Then compare it with the current timestamp and run the applications if the difference is greater than 86400 which is 24 * 3600 seconds
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
on runApplications(timestamp)
tell application "Finder"
open application file "clean-exports.app" of folder "Applications" of startup disk
open application file "clean-queue.app" of folder "Applications" of startup disk
open application file "clean-dropbox.app" of folder "Applications" of startup disk
end tell
do shell script "defaults write com.paul.clean 'timestamp' -float " & timestamp
end runApplications
set currentTimestamp to current application's NSDate's timeIntervalSinceReferenceDate()
try
set savedTimestamp to (do shell script "defaults read com.paul.clean timestamp") as real
if currentTimestamp - savedTimestamp > 8.64E+4 then
runApplications(currentTimestamp)
end if
on error
runApplications(currentTimestamp)
end try
After running the applications write the current timestamp to disk.
The file in UserDefaults is located in ~/Library/Preferences/com.paul.clean
(~ represents your home folder). The name com.paul.clean
is arbitrary but reverse dns form is recommended