Search

Applescript everywhere

Artificial languages are no different from natural languages. Either you're fluent and you will have fewer problems doing what you want to do, or you're not, and you're going to spend a lot of time on trivial issues that don't seem to make much sense.

I was in the middle of writing a very optimistic article on how automating your tasks could greatly enhance your workflow and your life in general, and how automation on Mac generally meant "Applescript" when I stumbled on some of those issues when trying to work with Terminal.

I will finish the article because computers are automation systems and using them without trying to automate more than what's provided by bundled programs means letting programs govern the way we work. But that episode painfully reminded me how non-trivial it was for me to work in Applescript, even though I am not exactly a beginner anymore, which does not mean that I know much, just that I know where to look for information when I don't know, and that I can more or less make sense of what I find.

With Applescript, I never read the "manual". I started with online examples, I copy pasted a lot, modified a few scripts, checked the errors, modified again until I got where I wanted, and checked the official reference only rarely (only 333 pages for the PDF I have, but still). And I'm saying to myself, now that I can sort of see how things fall in place, that I should definitely spare some time to go through either the reference or one of the excellent books that are out there.

If Applescript is a language, you're most likely going to use one of its "dialects" too, since any application that supports Applescript actually has a list of words that are specific to it and if the words look the same between applications, they are, like we say in French about some English words "false friends". They look the same but don't always mean the same. Applescript is not a language that you will use to make heavy-duty computations (although you could). Applescript was made to work with "Apple Events", which are used to connect applications together. As you'll eventually read in that other article, one of my script creates a Finder group of folders based on a job request I received in Mail that will end up as a Calendar item.

Applications that don't support Applescript can also be talked to: think about the primitive sign language that we use when we don't know a language. But that can be pretty powerful because just as you can show a person how to click the button that will move a robot, you can also tell Applescript to click on any button or menu item of pretty much any application that runs on a Mac. In fact, some parts of Applescript supporting applications are only available through their user interface, for example, asking a Terminal window to enter full-screen mode requires an Applescript that says "click on such menu item of such menu bar" or even better "hit F while pressing Command and Control" to simulate the shortcut. That's the part that bit me last weekend when I was trying to create a tabbed full-screen window in Terminal opened on the folder that was selected in Finder.

And Applescript can also call the services that are hidden under your Mac's hood: the command line applications that you can usually only access through Terminal, and reciprocally, you can call Applescript from the Terminal. I do that when I select an OmegaT project folder in Finder and use Applescript to tell the command line to use that folder as a parameter to launch OmegaT.

All this is very powerful but can be pretty daunting for amateurs. In my next article, I'll try to show you how I got where I'm at today, which is, in all honesty, not very far ahead of any person who's reading about Applescript today for the first time. I'll publish the Applescript code I'm using, with extensive comments, so that you don't have to just blindingly take for granted what I wrote but get to understand why I wrote it that way. And hopefully that will help you being more productive while working on your Mac.

Update (a few minutes after posting):
I clicked on the "Applescript" tag that's in the column on the right side of the articles, which brought me back to November 2007 when I wrote "I have never found a practical way to use AppleScript in my workflows..." Well, things have changed and now I'm pretty sure I could not live without Applescript on my machine...