In Development
Devlog Archives 2008-2010
Devlog Archives 2002-2007
Tutorials

Devlog

Automating Periodic Scripts With Launchd

22nd May 2016 | Programming

The Goal

Create a reoccurring 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 using launchd, instead for Mac OS X.
  • launchdlaunchd 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.

References