Before we continue with scripting the Finder, let’s step back and take a look at our code from our last example.
tell application "Finder"
get name of disk 1
end tell
Apple describes AppleScript as a programming language with an English-like syntax. Most people forget the “-like” part, though, and forget that AppleScript code must adhere to strict rules or the code will not work. For example, if you write:
tell "Finder" to tell me the name of the first disk
AppleScript will raise an error. The syntax is close to valid, but not quite, and this can be the source of considerable frustration. What if, instead, we had a syntax like this?
app("Finder").disks.name.get
The syntax in this example is more concise, which means less typing and less room for errors, and it still makes sense: we tell the Finder to get the name of our disks. “But that’s not AppleScript!” I hear you cry. Well, yes and no. Let’s take a closer look at what goes on when we run our AppleScript code.
### Apple Events
When you click the Run button in Script Editor or press Command+R on your keyboard, your Mac takes your AppleScript code and translates the code into Apple Events which get sent to the target application. The target application then executes the Apple Events sent to it and, if asked, returns a result by sending back an Apple Event. In our example, we sent Apple Events to the Finder that asked for the name of the first disk and the Finder sent back an Apple Event to us that contained the name of the first disk. Your Mac keeps track of all of these Apple Events with the aptly-named Apple Events Manager. Apple Events are a subset of a larger framework called the Open Scripting Architecture.
### Open Scripting Architecture
Underneath the covers of AppleScript and Apple Events lies the Open Scripting Architecture, a framework that provides for interapplication communication. The Open Scripting Architecture defines standards for scripting components to facilitate this interapplication communication. Scripting components can be written for any programming language, of which AppleScript is one. You can see this component in /System/Library/Components. Thus, you do not need AppleScript to boss around your applications. You can use any language for which there is a component. There are few components available, but we’re not going to investigate them. Instead, we’re going to build a bridge.
#### Scripting Bridges
As we learned above, your Mac takes your AppleScript code and translates it to Apple Events before sending it to the target application. We also learned that AppleScript is simply a component of the Open Scripting Architecture and is not required to boss around our applications. If we wanted to use another programming language, we would need something like a component that would translate our code into Apple Events before sending it to the target application. We would need a scripting bridge and several exist. The code above is Ruby code and it works with a Ruby-to-Apple Event Manager bridge called rb-appscript.
### Ruby and rb-appscript
Ruby is an open source, object-oriented programming language created with a focus on simplicity and productivity. rb-appscript takes that simplicity and productivity and applies it to scripting your applications. By using rb-appscript, we get all of the advantages of Ruby combined with the ability to boss around our applications with Apple Events. Ruby has several advantages over AppleScript including, most importantly, a module for unit testing. And, as we saw earlier, Ruby syntax is more concise, which means less typing and less room for errors. Ruby also provides more third-party libraries of code, including the Web application framework named Rails, and Ruby has much better text processing capabilities than AppleScript. Books on Ruby are plenty and the documentation is much better than you will find for AppleScript. If you want to try it out in your browser, click here.
### Objections
Many people object to using Ruby (or any other programming language) for fear of using the Terminal application. Like most programming languages, you write your Ruby scripts in a text editor, then run the script from the command-line in the Terminal. What those people miss is that Script Editor acts as a terminal application, only it’s used specifically as a terminal for AppleScript. The fear of Terminal.app is misplaced. You can safely use it for your rb-appscripts just as you use Script Editor for AppleScript. By the way, you can write AppleScript scripts in a text editor, then run the script from the command-line in the Terminal using the “osascript” command. Or you can execute AppleScript code directly from the command-line using the same command. Try it out. Open Terminal and type: osascript -e 'say "Hello from AppleScript"'. Sound familiar?
### Conclusion
Because of the availability of other language syntaxes to boss around our applications, I thought now would be the time to introduce an alternative to the wordy, English-like AppleScript syntax. As an alternative, I chose Ruby because Ruby syntax is more concise and can be just as easy to read. Moreover, Ruby includes unit testing and access to a large variety of third-party libraries. I like Ruby and rb-appscript so much that I decided to convert all of my existing AppleScripts to rb-appscripts.
I’d like your feedback on whether or not we should continue to learn and write with AppleScript syntax or whether we should continue down our path with Ruby and rb-appscript. But before you decide, let me show you an example…in my next post.