Skip to Content
AppleScript: The Definitive Guide, 2nd Edition
book

AppleScript: The Definitive Guide, 2nd Edition

by Matt Neuburg
January 2006
Beginner
592 pages
16h 55m
English
O'Reilly Media, Inc.
Content preview from AppleScript: The Definitive Guide, 2nd Edition

Context

The context or environment from which a script is executed can make a huge difference to its speed. This is in large measure because Apple events are expensive. In particular, what makes them expensive is the context switch involved in communicating between one application 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AppleScript: The Definitive Guide

AppleScript: The Definitive Guide

Matt Neuburg

Publisher Resources

ISBN: 0596102119Errata Page