Disposable AppleScripts

If you didn’t notice the trailing s in the title, you might think this post is going to be about Apple’s attitude toward AppleScript. That may be a topic for another day, but in this post I’m going to encourage you to think about AppleScripting the same way you think about shell scripting or scripting in languages like Python, Ruby, and (especially) Perl. Not just as a way to create permanent programs you use over and over again, but also as a way of getting particular one-time tasks done with a quick and dirty set of commands.

I do this quite often. Most of the time, it involves making wholesale changes to a set of Calendar events that I’ve already entered. Sometimes the starting time has changed, sometimes the location. In more cases than I’d like to admit, I made a mistake in creating the set of events and now I have to fix it. Whatever the problem, the best way to fix it is with a few lines of AppleScript that I’ll never use again. Here’s an example from several years ago.

Although I never use these event-editing scripts again, every one I write has generally the same form as the others, so I keep one called “Change events” in the ~/Dropbox/bin folder to use as a sort of template. Every time I need a new throwaway calendar script, I open “Change events” and edit it as necessary. Here’s what it looks like:

Change events template script

The last time I used it was to shorten the name of a set of events for an online continuing education class I’ll be taking this summer.

This morning I decided to make a wholesale change to Contacts. My contacts database started in the early 90s as a HyperCard stack. When I switched to Linux in 1996, I turned it into a plain text file. And when I returned to the Mac in 2004 or 2005, I put it in Address Book, which has since been renamed Contacts. Throughout these iterations, the database has included a title—Mr., Ms., Dr.— for each person. I did this to make it easy to write scripts for generating traditional correspondence—letters on paper that started with a salutation like “Dear Ms. Client:”.

In the 90s and through the 00s, this made sense because the great majority of my business correspondence was done on paper, and this format for a letter was expected. Now, virtually none of my correspondence is printed on paper, and having a title associated with each contact clutters up the To: and CC: fields of my email. It’s long past time to delete the titles. Here’s the script that did it:1

Title deletion AppleScript

The source is

tell application "Contacts"
  set title of every person to missing value
end tell

This is almost a one-liner. I first tried

tell application "Contacts" to set title of every person to missing value

but without the save, nothing in Contacts changed.

Maybe I’ll save this script as template for any later Contacts-altering scripts. I’ve been thinking about deleting all the fax numbers.

AppleScript is easy to read, but it isn’t especially easy to write, partly because it’s verbose but mostly because the application dictionaries are inconsistent in how they name items and actions. This acts, I think, as a deterrent to writing short, disposable AppleScripts. There’s a tendency to believe it’ll take more time to write the script than to just do the work by hand. But repetitive work leads to errors and inconsistencies. The uniformity that comes with a scripted solution is usually worth the effort, even when the script is run once and thrown away.

  1. I’m showing it in the Script Editor because that’s where I write and run these one-off scripts. Permanent AppleScripts are written in the Script Editor but usually run via Keyboard Maestro.