Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [206]

By Root 1584 0
and another. Thus it is typically fastest, where possible, to run a script from within the application it targets. If an application has a Script menu, therefore, it is worth putting the script there and executing it from there to see if this makes it faster.

(But also, there are some contexts that are inherently slower than others, for no discernable reason; it has something to do with how these contexts are programmed and that's that.)

Here's a test script:

set x to (get current date)

repeat 500 times

tell application "iTunes"

get name of it

end tell

end repeat

set y to (get current date)

set z to (y - x)

tell application "Finder"

activate

display dialog z

end tell

Table 22-1 shows some rough timings on my machine for running that script from within various contexts.

Table 22-1. Timings for the same script executed in different contexts

Context

Timing

iTunes's Script Menu

0.15 second

Red Sweater FastScripts

0.5 second

Script Debugger

1.5 seconds

Apple's Script Menu

8 seconds

Script Editor

9 seconds

DragThing

Timed out after 500 seconds

Many external factors affect speed , so no absolute lessons can be drawn from a simple, unscientific test such as this; but clearly it can be worthwhile to consider the context in which a script will run when evaluating its speed.

Chapter 23. Scriptable Applications


AppleScript's chief purpose is to let you communicate with scriptable applications. How you target a scriptable application using AppleScript depends on whether the application is local (run on the same computer and by the same user as your script) or remote (run on a different machine, or on the same machine but by a different logged-in user). You might also like to know what scriptable applications are included with a default installation of Tiger. (On creation of scriptable applications using applets, AppleScript Studio, and Cocoa, see Chapter 27.)

This book won't teach you how to script any particular application (see "The Scope of This Book" in the Preface). If the application comes with documentation or examples showing how to script it, start with that. For certain applications, there may be third-party books or web pages devoted to the topic of scripting it. The application will in any case have a dictionary (see Chapter 20).

Targeting Scriptable Applications


To target a scriptable application is to aim Apple events at it, like arrows. The principal linguistic device for targeting an application in AppleScript is the tell block containing an application specifier. Such a tell block actually has two purposes: it determines the target, if no other target is specified within the block, and (at compile time) it also causes a dictionary to be loaded, which may be used in the resolution of terminology. If the target of the tell block is expressed as a variable rather than a literal application specifier, no resolution of terminology is performed and the application is not sought for targeting until runtime when the code is actually encountered. Instead of a tell block, the of operator (or its equivalents) can be used to form a target; this does not cause any resolution of terminology either. Terminology can be resolved independently of any tell block by means of a terms block.

A reference to an object belonging to an application can also be used to target that application. The terminology within the reference has already been resolved (otherwise the reference could not have been formed in the first place); any further terminology accompanying the reference when you actually use it will have to be resolved independently.

(See "Missing External Referents" in Chapter 3; "Target" in Chapter 11; Chapter 12; "Application" in Chapter 13; "Tell" and "Using Terms From" in Chapter 19; and "Resolution of Terminology" in Chapter 20.)

Local Applications


A local application is an application on the same computer and under the same user as the script. The specifier for a local application may consist of a full pathname string (colon-delimited) or simply the name of

Return Main Page Previous Page Next Page

®Online Book Reader