Mac Productivity: AppleScript and Writing Scripts

For most Mac users, writing scripts for the Mac is a step too far. To be honest it’s actually a journey too far, because it’s a bit more than a step. I understand and I’m not going to argue the point, but I think it’s worth your while understanding what scripts are and what you can do with them.

You might have a friend, or even a teenage son, who’s a geek and will work for pizza. In which case it’s possible that you could find them a task or two that would keep them in mozzarella for a while. Alternatively, you may work for one of those companies that’s throwing the PC away and you want to know to what level a Mac can be automated.

Here’s the thing: On a PC there’s VBA (Visual Basic for Applications) which makes it possible to directly automate Micrsoft Office applications. So if you want specific things to have when a file is saved, or you want to link word files to spreadsheets directly, then VBA is your language. On the Mac the language is AppleScript and it isn’t confined to just a small set of applications. It works with every app. In fact:

Pretty much anything you can do from the keyboard or the mouse in OS X can be achieved with AppleScript scripts!

You can automate just about anything that you do – but that doesn’t mean you should. Or to be more precise, there’s an intelligent order to how you automate a Mac and you do not begin by scripting. Here’s the way that I look at it:

  1. Quicksilver coupled with an intelligent use of hot keys will automate many things (see This Posting)
  2. You can further automate by adding more sophisticated hot keys with iKey (see This posting).
  3. You can go half-way to programming things with AppleScript (because it’s half a programming language)
  4. If you need to go further then my advice is start using Ruby and write real programs.

If you’re wondering why I’ve not mentioned Automator, the automation program that’s been available with the Mac since the Tiger release of OS X, it’s because Automator is really a wizard capability for assembling scripts. If you’re going to go as far as using Automator, you may as well bite the bullet and write AppleScript scripts. If you really have no idea about programming, Automator may give you an idea of what a scripting language can do – so it can work as a primer. But bear in mind that Automator is quite limited compared to AppleScript.

What is a scripting language?

A script is normally a set of executable instructions that bind together chunks of executable code. A scripting language doesn’t need to be as extensive as a programming language because mostly it’s going to connect a few things together and then add a little bit here and there to create a useful capability. Of course, technically, there is no definitive difference between a scripting language and a programming language. Scripting languages get extended to do things that programming languages do and programming languages, like Ruby for example, can be used to write scripts.

The distinction is in what scripting languages are used for. AppleScript is used to thread together libraries of routines that OS X applications provide, either explicitly or implicitly. I have no intention of trying to describe AppleScript in depth. As far as languages go, it’s nothing to write home about. Those who have never programmed before, but would like to make the leap, may like it. It has a kind of Algol/Pascal type of block structure and it pretends to be English. People who have programmed a great deal know that pretending to be like English isn’t really that clever. It’s long-winded and can be confusing, but if you’re new to programming, it’s not a bad place to dive in.

On OS X, for no cost at all, you can download a sophisticated and advanced development environment called Xcode, which you can use for AppleScript development. You only need it if you’re going to do a great deal of scripting – the native script editor is all you need if you just want to write a script or two. Nevertheless it’s worth knowing it’s there and it’s free.

If you want to have a sense of the power of AppleScript then download UI Builder. This is software you have to pay for, but you can use it free for a month or so and it helps you understand what AppleScript can do.

UI Builder

pl044UIB.gif UI Builder is pretty much all you need to write sophisticated scripts for any application or group of applications in OS X. The problem is this: App developers on OS X could build and provide a library of “callable” functions that accompany their applications and provide interface points that AppleScript could use. But most app developers don’t, possibly because it’s extra work for no pay. Apps don’t fail to sell on OS because they don’t have good AppleScript libraries.

So it’s UI Builder to the rescue. This neat and well built program can examine a running OS X application and identify how to include useful parts of any application in AppleScript scripts. It even writes the AppleScript calls for you. If you intend to do anything ambitious with AppleScript then this is the utility you need to help you. In my book, it’s worth the money. It will pay for itself very quickly.

———————————————————

This is almost the last posting in this initial series on Mac productivity. The level of interest in this has exceeded my expectation, so I have set up a separate web site for this topic: PDQ Mac.

This entry was posted in Apple and tagged , , , . Bookmark the permalink.
  1. Martin Michel posted the following on Tuesday, September 23, 2008 at 2:42 am.

    Hi Robin,

    Thank you very much for your great article, which should really water the mouth of everybody who is interested in Mac automation to get rid of those annoying repetitive routine tasks. I really appreciate your efforts to spread the word about my favourite language of automation :-)

    I agree that the AppleScript syntax seems weird to experienced programmers (it’s even called a ‘mutant language from the moon’ by Apple’s own engineers…). But there is an excellent free Apple event bridge named appscript, which allows to write AppleScripts in Ruby, Python or Obj-C (http://appscript.sourceforge.net). Thanks to Hamish Sanderson for developing and maintaining it!

    Moreover I would like to add that one can see all AppleScript commands and variables supported by a scriptable Mac application by simply dragging and dropping the application icon onto the Script Editor’s icon. My experience is that many people don’t know this.

    There is no need to buy a book to get an introduction into AppleScript. Apple offers a very good and free guide to their weird programming language, the so called AppleScript Language Guide:

    http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf

    In my opinion using the built-in AppleScript dictionaries for scripting applications is less error-prone than GUI scripting, as Apple may modify certain GUI elements in the next update, which always breaks the written scripts. But of course, as you already mentioned, GUI scripting is great when an app doesn’t provide built-in scripting support.

    Scripting the Mac is really pure fun and can boost productivity tremendously, as the Mac scripting environment is now so rich (especially on Leopard). You don’t only have AppleScript. All major scripting languages (Python/Ruby/Perl) are preinstalled and ready-to-use. And Apple provides very cool additions like PyObjC, allowing you to access the Cocoa frameworks from your scripts: That is scripting reloaded. Think on-the-fly PDF creation, Dictionary access and QuickTime movie and audio manipulation.

    Last but not least I would like to recommend the AppleScript forums at MacScripter.net for everybody who wants to start scripting his or her Mac. It’s a great resource for everything AppleScript and Mac automation. And yes: You can meet me there :-)

    Best regards from cold and rainy Germany,

    Martin Michel | The Joy of Scripting

  2. Bill posted the following on Saturday, September 27, 2008 at 8:09 pm.

    So what’s the link for this “UI Builder” application you speak of?

  3. Bloor Robin posted the following on Saturday, September 27, 2008 at 10:00 pm.

    Sorry for not adding it to the above posting. I’ve fixed that now. And for the record, it’s http://prefabsoftware.com/uibrowser/


Leave a reply