Ruby as AppleScript Alternative – Part 5
In the previous posts on rb-appscript I dug into some code to show how you can use Ruby and rb-appscript to accomplish the same tasks available with AppleScript. Let’s take a few steps back to look closer at some of the workings behind AppleScript, which is important to understand if you want to begin any serious work regardless of the scripting language you choose.
The Open Scripting Architecture (OSA) is designed to allow for automating tasks, inter-application communication and otherwise directing application behavior. Apple events are the backbone of OSA, facilitating messaging and interaction with applications (think interprocess communication).
The structure of an application or a document is defined through the Apple event object model. When an application implements this object model, that application is then considered scriptable, which means you can communicate with the application using AppleScript, rb-appscript or other scripting languages that are OSA compliant.
In regards to the object model, elements refer to attributes such as the paragraphs or words of a document. Elements can be referred to by numerical position or, if they have ‘name’ properties, by name. Objects also have properties, which can hold either a simple attribute (name, position, etc.), or a reference to another application object. An important distinction is that there can be any number of elements (e.g. paragraphs, words, etc) whereas a property has only one defined value (e.g. version number).
The figure below shows the relationships of elements and properties and is from the paper The Open Scripting Architecture: Automating, Integrating, and Customizing Application by Cook and Harris, 1993.
As an example of a property that includes a reference to another object, the text property of the TextEdit application contains a text object, which includes character, word and paragraph elements. Each of these elements can be accessed using various commands as shown in the examples below:
# Set text color tedit = app('TextEdit') tedit.documents.text.paragraphs.color.set('blue') # New paragraph at the end of the document tedit.make(:new => :paragraph, :with_data => "New content here...", :at => tedit.documents.text.paragraphs.end)
Using commands we can interact with objects using rb-appscript. Applications generally support the commands: run, launch, activate, reopen, open, print and quit. Depending on the application, there may be any number of additional commands that are available. For example, TextEdit offers the ‘make’ command (shown above) that can be used to make a new paragraph, create a new document, etc.
Let’s wrap up by showing a few examples. Enter the following commands using irb :
#1 Begin by importing the appscript module: require "appscript" include Appscript #2 - Create an new object specifying the application we want to target tedit = app('TextEdit') #3 - Create a new document using make command: doc = tedit.make(:new => :document) #4 - Set the content of the document: tedit.set(doc.text, :to => 'First paragraph') #5 - Set text color tedit.documents.text.paragraphs.color.set('blue') #6 - Get the document text text = tedit.get(doc.text) #7 - Show text puts text
The TextEdit document should look as follows when you are done:
Hopefully this post has provided a little more insight as to the object model and working with elements, properties and commands. In the next post I’ll show how to use the interactive help built into rb-appscript.