AppleScript for Complex Repetitive Tasks

ScriptingThere is not always “an app for that.” There are many complex and repetitive tasks we perform in the academic world which depend on technology but which cannot be carried out by a single software application without significant human interaction. With just a little bit of programming knowledge, however, the time and effort required to accomplish these tasks can be significantly reduced, while others that we might never have attempted become relatively trivial to carry out.

The key challenge is getting different applications to talk to each other and pass information or files to each other in a seamless fashion. It is often an ugly affair that requires trial and error and, for those of us who are not trained in the coding arts, not a little poking about in online forums for snippets of code, but the payoff can be huge in terms of time saved. If you use a Mac, the built-in scripting language AppleScript and its simpler and more friendly interface the Automator application provide a powerful way to achieve this aim by acting as a glue between one piece of software and another.

Many, if not most, Mac software applications have a “dictionary” of AppleScript commands and variables that they understand. If the applications you need to work with have a sufficiently equipped vocabulary, you can tell them to do your bidding and, if necessary, pass on the results to the next application or part of your script. There is not space in this one post to teach the AppleScript language itself but by introducing one example, I want to suggest that some readers may find it worth looking into the many tutorials that can be found online.

Images to PDFs: In my earlier guest posts here at ProfHacker, I shared the physical setup that I use to efficiently take high-quality photographs of a large number of archival documents or published materials, and a few of the strategies that have worked for me in organizing those files that I leave in their original form. I mentioned that, while I usually leave more difficult to read and poor quality archive documents as images, in the case of published materials I usually convert the images to PDFs. Unlike commercial PDF scanners or slower consumer scanners, however, the trade-off when using a fast and portable camera is that the imported images usually need to be separated, undergo some processing, and then merged together as a PDF. The challenge then is to minimize my intervention in this process. This is just one of many examples where applescript, automator, and a bit of Photoshop work with “actions” are a perfect fit. So what does my solution do?

1. First, I prepare my collection of photographs by separating them into folders corresponding to source. I dump all these into the folder that the script will operate on.

2. I run my “Mass Convert” script and am free to leave the computer to do the rest. This AppleScript cycles through and performs the following steps on every folder of a source’s images it finds inside its target folder:

3. In order to leave me a pristine copy of the original images in case something goes wrong, the script first copies the files into another temporary working directory.

4. It commands Adobe Photoshop to run an “action” (a collection of recorded Photoshop commands you can run on an image or folder of images) I created upon all the images for that source. In my case, each image will be opened, its brightness and contrast changed to make the text brighter and clearer, the size of the image reduced significantly, and then saved with a level of compression to optimize between image readability and size. It is possible to do this without an expensive application like Photoshop using other means but I like quality of the result better.

5. The resulting processed images, now of improved readability (though not cropped in any way since that is hard to automate across sources; I don’t mind a finger or a bit of dark margin here and there) and significantly compressed are then merged together as a PDF, using one of the built-in Automator commands found on the Mac.

6. The new PDF is renamed to correspond to whatever I named the folder containing the source’s images and the file placed on the desktop. All the files in the working directory are deleted to prepare for the next source.

For the curious, the script, which will not run as is without the other components (setting up the folders, Photoshop actions, and Automator workflow) is available here. Over time I’ve make small modifications as well as a modified version to process the lower quality images images taken with my phone instead of my regular camera. I also sometimes OCR these PDFs which makes the text within searchable. Those who are interested in OCR might want to read this earlier ProfHacker posting by Kathleen Fitzpatrick for some great tips.

This is just one of several ways that AppleScript has made my computer do a little extra work for me. A few of my other creations include

  1. an AppleScript to import all my Zotero entries into DEVONthink and create a folder and note file for each,
  2. a complex daily alarm which sets my computer to awake at a certain time, mute the sound, play a random song from a list of soft background music, slowly increase the volume to ease me awake, download and play an NPR news summary, and then, if I’m still not up, play a random item from a list of loud and obnoxious songs, and
  3. two simple dashboard widgets to convert Korean and Japanese historical dates.

Read more about Applescript over at its Apple homepage, at MacScripter, and Mac OS X Automation.

What are other common complex tasks that could benefit from automation? Anyone else have great scripts to share? Besides raw “shell scripting,” what are good equivalents for the Windows and Linux?

Return to Top