Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [276]

By Root 1557 0
[BBEdit]:

Created and installed App glue for 'BBEdit.app, v8.2.3' (BBEdit)

The resulting glue table contains a document in the standard Perl documentation format (POD, for "plain old documentation") expressing the dictionary in Perl syntax. You can read it with perldoc; another tool, gluedoc, provides a shortcut to the desired document:

% gluedoc BBEdit

Using the POD document and the instructions for Mac::Glue, you can usually figure out the Perl analogue to an AppleScript expression. Here's code to generate our model events:

use Mac::Glue;

my $bb = Mac::Glue->new('BBEdit');

$bb->launch( );

$bb->make(new => 'document');

my $doc = $bb->obj(document => 1);

my $text = $bb->obj(property => 'text', $doc);

$bb->set($text, to => 'Hello, world!');

Personally, I find Perl's notion of object-orientation clunky, and Mac::Glue's expression of AppleScript terminology rather inconsistent. Moreover, a Perl script that uses Mac::Glue takes even longer to get running than the corresponding AppleScript code would take to compile. This makes me wonder why it wouldn't be preferable to use osascript and have done with it (Chapter 25). Still, it's Perl, and for people who like this sort of thing, this is the sort of thing they like.

Python


Hamish Sanderson's Appscript brings AppleScript terminology to Python. It comes with a standard installer package; double-click and run the installer and you're good to go. Scripts must be run using pythonw, not plain python. Here's the code for generating our model events:

from appscript import *

bb = app('BBEdit')

bb.make(new=k.document)

bb.documents[1].text.set('Hello, world!')

No glue is necessary; like JavaScript OSA, Appscript reads the dictionary and constitutes an appropriate application object behind the scenes. It does this very quickly, and it's even faster if you run the supplied background application AppscriptTerminologyServer. The Python code is structured in a compact, intuitive, highly legible manner, incorporating all the best features of UserTalk and JavaScript; this is partly due to Python itself and partly due to Appscript's clean and thoughtful implementation. Appscript is also easy to learn. An included tool, htmldoc.py, generates an HTML-formatted display of an application's dictionary with the Python version of its terminology. (Alternatively, you can use the HTMLDictionary standalone application, included with the installer, which does the same thing.) And a particularly nice extra is an interactive help command for exploring either the terminology or the live attributes of an application's objects. So, for example, within the interactive pythonw interpreter:

>>> bb.documents[1].help('-s')

================================================================================

Appscript Help (-s)

Reference: app(u'/Volumes/gromit/Users/matt2/extra/BBEdit 8/BBEdit.app').documents[1]

--------------------------------------------------------------------------------

Current state of referenced object(s)

--- Get reference ---

app(u'/Volumes/gromit/Users/matt2/extra/BBEdit 8/BBEdit.app').text_documents.first

---- Properties ----

properties:

{k.contents: u'Hello, world!',

k.ID_: 7038242,

k.file: k.MissingValue,

k.modifiable: True,

k.state_modified: False,

k.name: u'untitled text 3',

k.modification_date: datetime.datetime(2005, 9, 22, 12, 51, 51),

k.source_language: '',

k.FTP_info: k.MissingValue,

k.line_breaks: k.Unix,

k.container: None,

k.text: u'Hello, world!',

...

And so on. This is like a command-line version of Script Debugger, and greatly eases the trial and error required to develop a script. Appscript clearly attempts to equal and even surpass AppleScript as an environment for creating and sending Apple events. In my view, it succeeds admirably; it is extremely complete and consistent, and takes care of all the heavy lifting for you.

Appendix C. Tools and Resources


This appendix provides sources for software, tools, documentation, and further information on various topics discussed in this book.

Free software from Apple

Return Main Page Previous Page Next Page

®Online Book Reader