Customization

No application in the world can meet everyone's desires and expectations, because whatever the application's features, it is impossible for the developers of that application to anticipate everything that every user will wish to do with it. AppleScript can be a solution to this problem. Scriptability can provide, in essence, an entire alternative user interface: instead of the graphical user interface of buttons and menus, it's a programming interface. An application's scriptability says to you: "Here are all the types of thing this application operates on, and here are the operations you can perform on them; if none of this application's menu items and buttons and other graphical interface items performs just the sequence of operations you desire, feel free to use AppleScript to create a sequence that does."

Here's a real-life example . On the Internet, someone asked about assigning track numbers in iTunes. A track number is an attribute of a song, which can be set in that song's Get Info dialog; it can be made to appear in the playlist display, and you can sort on it. Thus, track numbers can be used to control the order of playback in a playlist. This user wanted to assign track numbers immediately after "ripping" a CD to iTunes , so that the order in which the tracks appeared on the original CD, and in which they appeared in the initial playlist derived from that CD, could easily be restored within iTunes later on. In essence, the user was saying: "The tracks are already in their correct order within this playlist; how can I use that order to assign all the tracks a track number, in a single move?"

My response was: "Use AppleScript." Here's a script that does it:

set i to 1
tell application "iTunes"
    tell (get view of browser window 1)
        repeat with aTrack in (get every track)
            set track number of aTrack to i
            set i to i + 1
        end repeat
    end tell
end tell

An interesting philosophical debate then ensued. The user thanked me, but expressed regret that this was the "only way" to accomplish this task; iTunes, he said, should include this feature natively. My attitude was just the opposite: thanks to scriptability, iTunes does effectively include this feature natively. "Instead of berating the developers of iTunes for not including that one magic menu item that would do just what you want," I said, "you should be applauding them for making the application scriptable and letting you implement the functionality yourself." Through its scriptability, iTunes is customizable; you can give it features that are within its powers but are missing from its graphical user interface. And this, surely, is the way it should be. If iTunes included a menu item for every task every user on earth would like it to perform, it would have thousands of menu items. Instead, it has a graphical user interface for the tasks that most users want to perform, and leaves the rest up to the individual and AppleScript. iTunes even gives you a way to add your scripts to its graphical interface; put them in ~/Library/iTunes/Scripts/ and they show up automatically in iTunes' Script menu .

Similarly, Microsoft Entourage , an email application, lacks any menu item or keyboard shortcut that truly deletes an email message. Deleting a message that's already in the trash folder (called "Deleted Items") does truly delete it, but deleting any other message merely moves it into the trash folder. This is spectacularly annoying when, for example, you are ready to delete spam messages that have been categorized into the Junk E-Mail folder. But Entourage is scriptable, and it has a Script menu to which you can add your own scripts. So I've written a script to delete all currently selected messages:

tell application "Microsoft Entourage"
    display dialog "Really delete selected messages completely?"
    set theMessages to current messages
    try
           delete theMessages
        delete theMessages
    end try
end tell

Some scriptable applications provide a means for customization at an even deeper level, by letting you modify what happens when you choose one of the application's own menu items or perform some other action in that application. For example, the Finder can be set up with Folder Actions that take over automatically when you do things such as move a file into a certain folder. (See "Automatic Location" in Chapter 2 and "Folder Actions" in Chapter 26.)

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.