Reduction

A script is a means of reduction: it combines multiple steps into a single operation. Instead of doing one thing, then another, then another, you do just one thing—run the script. I particularly like using AppleScript for reduction. Having worked out a series of steps to accomplish a task, I often realize that not only do I not want to have to go through them all again later, but also I fear I won’t even remember them again later! My AppleScript program remembers the steps for me.

Here’s an example. From time to time I have to reboot my computer into Mac OS 9. The drill is: open System Preferences, switch to the Startup Disk pane, click the Mac OS 9 System folder, hold the Option key, click Restart. Too much work! Too many steps, too much waiting, too much hunting for the right thing to click on, too much clicking. Here’s the script that does it for me:[1]

try
        do shell script ¬
                "bless -folder9 'Volumes/main/System Folder' -setOF" ¬
                        password "myPassword" with administrator privileges
        tell application "System Events" to restart
end try

I’ve got that script saved as an applet, which is a little application written with AppleScript. To run the script in an applet, you just open the applet like any application. So this applet is sitting right on my desktop, where I can’t miss it. To restart into Mac OS 9, I just double-click it. Now that’s something I can remember.

Here’s another example. A journal for which I occasionally write articles requires me to submit those articles in XML format. It happens that the XML editor I use inserts line breaks; the magazine doesn’t want those. In practice the problem arises only between <Body> tags. So this BBEdit script post-processes my XML output, removing the line breaks between <Body> tags just before I send an article off to the magazine:

tell application "BBEdit"
        activate
        repeat
                find "<Body>([\\s\\S]*?)</Body>" ¬
                        searching in text 1 of text window 1 ¬
                        options {search mode:grep, starting at top:false, 
                        wrap around:false, reverse:false, case sensitive:false, ¬
                        match words:false, extend selection:false} ¬
                        with selecting match
                if not found of the result then
                        exit repeat
                end if
                remove line breaks selection of text window 1
        end repeat
end tell

There’s nothing very complicated about that script, and I don’t use it very often, but when I do use it, it’s tremendously helpful. For one thing, it saves me from having to remember the regular expression used to do the find. For another, it takes over the repetition of finding, then removing line breaks, then finding again, then removing line breaks again, and so forth. (Clearly the notions of reduction and repetition can be closely allied.) The example may seem very specialized, but that’s fine, because the whole point of AppleScript is that you are the programmer and can write the code that solves your own real-life problems. And it does illustrate some important general principles, such as using a scriptable application to process text more powerfully than AppleScript alone can easily do it.

The next example is about URLs. Often, working in some application or other, one sees a URL and wants to click on it and have the right thing happen: if it’s an http URL, one’s default browser should open and fetch it; if it’s an email address, one’s email program should create a new message to that address; and so forth. In some applications, such as a web browser, URLs are automatically clickable in this way; but in other applications you sometimes have to deal with the URL manually. This involves starting up the right helper program yourself, and then doing something with the URL: in a browser, paste it and press Return; in an email program, make a new message and paste it into the address field. With this AppleScript solution, I just copy the URL and let the computer do the rest:

tell application "System Events"
        set theProc to (get process 1 where it is frontmost)
        tell application "Finder"
                activate
                delay 1 -- give time for clip to convert from Classic
                copy (the clipboard) to theURL
                ignoring application responses
                        try
                                open location theURL
                        end try
                end ignoring
        end tell
        set the frontmost of theProc to true
end tell

The switch to the Finder is to force the clipboard contents to convert themselves to a usable form (and the delay is to give this time to happen); this seems to be needed particularly when working in a Classic application. System Events is called upon at the end to switch back to the application I was using at the outset. The heart of the script is the magic open location command, which does the “right thing” with the URL.



[1] This code is inspired by an original from Thomas Neveu; see http://www.nonamescriptware.com.

Get AppleScript: The Definitive Guide 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.