The Goal
Create a recurring maintenance task that will move all screenshots from the Mac's Desktop into a designated folder each day.
The Options
I looked at a number of different options to automate this maintenance task. I needed something non-obtrusive, straightforward and flexible.
- Calendar + Automator — This is the more graphical approach, but the downside is seeing repeating events littering up the Calendar. I prefer to keep the Calendar as clean as possible.
- Folder Actions — I've used Automator to create a Folder Action on other folders before, such as the Downloads folder to place new files in specific locations, but I needed something that would only run once a day, not every time a new file was added to a folder.
- cron — Having coming from a UNIX background, I knew of
cron
. A little research on this topic tended to recommend usinglaunchd
instead for Mac OS X. - launchd —
launchd
fit the bill for what I was looking for. It had the flexibility to allow me to run a given script on a daily basis.
The Code
In this example, I created a plist list file named local.cleanupdesktop.plist
and placed it into the directory ~/Library/LaunchAgents
. This particular plist configuration displays just a small portion of what is possible. In this example, it runs the script cleanupdesktop.sh
each day at 9:30. For a more comprehensive list of what options are available, check the comprehensive resources at launchd.info.
The cleanupdesktop.sh
script is a small bash script which searches for any screenshots which are on the Desktop and places them into the Screenshots folder. This particular script will take screenshots from the keyboard shortcut or from screenshots taken from the iOS Simulator.
Another Example
The next example is even simpler which has everything contained within the plist file. This launch agent will display an alert every weekday at 5 p.m. (17:00) as a handy reminder. The code passes in program arguments, which effectively invokes an Applescript command: osascript -e 'display notification "Update your tickets" with title "Reminder" sound name "Sosumi"'
.
Installing the Launch Agent
To get these new launch agents to run, either log out and then log back in to your account, or the launch agent can be manually loaded from the command line using the launchctl
utility:
launchctl load -w ~/Library/LaunchAgents/local.cleanupdesktop.plist
Likewise, if you want to unload the launch agent, use the command:
launchctl unload -w ~/Library/LaunchAgents/local.cleanupdesktop.plist
References
- Scheduling recurring tasks
- Automator: Scheduling Workflows to Run Automatically
- How can I get a script to run every day on Mac OS X?
- Running the Mac OS X maintenance scripts
- launchd.plist man page
- Task Till Dawn
- Plisterine
- Run a shell script periodically on Mac OS X without root permission
- OSX Maintenance Scripts
- Running Periodic Maintenance Scripts in Mountain Lion
- Add your own periodic scripts, subfolders, and launchd tasks
- Run Mac OS X Maintenance Scripts Manually
- launchd.info
- How to restart launchd OS X without rebooting?
- Remove an Agent from launchd
- Example: Writing a Launch Agent for Apple's launchd
- Creating Launch Daemons and Agents
- Designing Daemons and Services