Advanced AppleScript: Cal Simone, Main Event Software

By: Mike Kimball - Revised: 2006-06-20 devin

Introduction

This is a supersub-doc article summary. This is where you should put your introduction paragraph that briefly describes the subject matter and scope of the article.  Copy this paragraph and paste it into the 'Article Summary' field in the Page Properties dialog.  To access Page Properties, click on the 'Properties' button in the upper right corner of this page in AdminCentral.


Handlers

Handlers are pieces of code (presumably used by multiple scripts) that can be called by other scripts, saving having to type them over (or cut-n-paste, basically avoids really long scripts). Good rule of thumb is to make handlers less than 20 lines long.

to ComputeTheSquareRoot for x
    return x ^ 0.5
end ComputeTheSquareRoot

to ComputeTheCubeRoot(x)
    return x ^ (1/3)
end ComputeTheCubeRoot

Save this as an application, so that it can be called as a handler...

Other code fragments

Calling an app on a machine remotely...

tell application "Root computer" of machine "eppc://128.110.124.120"
    ComputeTheSquareRoot for 100
end tell

Beeping when idle (save this as app with "stay open" selected)...

on idle
beep
return 5
end idle

Beeping number of items in list

on open itemList
    beep (the number of items in the itemList)
end open

on run
    display dialog "Drop things on me, you nerdbucket."
end run

on quit
    display dialog "bye"
    continue quit
end quit

on reopen
    display dialog "I'm already open you dill-weed"
end reopen

Libraries

One trick to free memory:

set lib to load script alias "my computer:desktop folder:...."
tell the lib
ComputeTheSquareRoot for 49
end tell
set lib to "" -- sets the variable blank, frees its memory usage

CGIs

Just applets that use special handlers, using apple events sent by the web server (look in standard additions in 'internet' suite).

Error Handling

Set y to 5
set z to 0
try
set x to y/z
on error m number n
if n is -2701 then
display dialog m
else
display dialog "Bummer, dude."
end if
end try
display dialog the result

Debugging

"Buy a debugger." That was his resounding solution, "you need a debugger." (And, this guy wrote the PhotoScripter (Photoshop AppleScriptability) plugin). (link dead)

Mac OS X scripting

The engine and editor will be there ... but not everything that is scriptable in 9 will be so in X ... especially in the Finder. Also, there are many items that are either absent or so different that they won't retain scriptability.