This article contains various tips relating to AppleScript, the system-level scripting language on Mac OS.
1 " What those strange «aevt x» commands mean.
Those commands are the actual AppleEvents which are sent when you use any kind of command in a script, or when something happens in a script or application. The AppleEvent codes are usually automatically replaced with proper AppleScript commands, so if you see the AppleEvent codes then you may need to install a particular Scripting Addition.
Make sure you still have all the same Scripting Additions as you did when you wrote the script (if you wrote it yourself), or check the documentation or comments for a script to see if any extra Scripting Additions are required. Also make sure you've got the latest version of all your Scripting Additions. See the Resources section below for links to AppleScript-related websites, where you can find links to lists of Scripting Additions.
You may also have a different version of the application which your script is trying to talk to, or a different application with the same name.
If you're a developer, and your application is recordable as well as scriptable, you may be seeing those strange AppleEvent codes instead of proper AppleScript terminology when you record your application. In this case, it's very likely that the AppleEvents your application is sending to itself are incorrect. Remember that AppleEvent codes are case-sensitive!
By the way, you type « by pressing option-\ (option-backslash), and you type » using option-shift-\ (option-shift-backslash).
2 " How to use icons in dialogs, other than the usual stop, note and caution icons.
You can use any icon which has a name by just specifying the name in your with icon "name" statement. The icon must be 'named' as a resource; you'll need to use a resource-editor like ResEdit to do this. Icons must also be of the 'cicn' type, not icl8 or such.
When running a script, if AppleScript finds an icon name it will look for the icon in the following way: first it looks inside the script file itself, to see if the file has any added icon resources. Then, if it doesn't find the icon, it looks inside the current (frontmost) application. Finally, if it still doesn't find an icon with the proper name, it looks inside the System file in the current System Folder. If no icon is found at this point, your dialog will just display without any icon.
3 " How to disable (comment out) large amounts of code easily.
This one is simple, but will save you endless amounts of time and irritation. Simply put (* before the start of the code, and put *) after it. This will comment everything between those markers, and they can be removed much more easily than all those -- marks.
4 " How to use the "my" keyword properly.
There are really only three rules for this one, and they're not too hard to remember and understand:
- If "my" refers to a property, then it is the property of the item running the script, or the script itself.
- If "my" refers to a variable, then it is being used to access a variable belonging to a parent object or script.
- If "my" refers to a function or subroutine, then it is being used to define that function or subroutine.
5 " How to access nested files and folders... without the hassle.
This is another very, very common source of frustration. For most purposes, you can access all nested items of a particular folder by just replacing contents of with entire contents of. That's it - it really is that easy. No additional osaxen (Scripting Additions) needed, and no complex repeat loops and so on. Here are some examples:
tell application "Finder"
set x to entire contents of folder "System Folder" of startup disk of application "Finder"
end tell
or
tell application "Finder" to set x to entire contents of folder "Macintosh HD:System Folder"
Both of these will return a list of items inside the specified folder, including items nested in subfolders of it, using the finder's very long filepath format, like this:
{file "Beep" of folder "Scripting Additions" of folder "System Folder" of startup disk of application "Finder", file "Choose Application" of folder "Scripting Additions" of folder "System Folder" of startup disk of application "Finder"} etc.
Individual file-paths can then be extracted from x as string, like so:
set apath to item 1 of x as string
which returns:
"Macintosh HD:System Folder:Scripting Additions:Beep"
Note: Getting the entire contents of some large folders (the hard disk folder, the System Folder) may cause 'out of memory' errors, and related problems. Best to use this if you know the amount of files it's going to return.
It's frustrating to find this out after spending a lot of time trying to find Scripting Additions and writing procedures to manually go into each sub-folder, but it at least shows us that it's worth closely examining all of the standard AppleScript dictionaries before looking to any other source.
6 " How to send commands to other scripts.
Let's say you've got a script with a subroutine called showText(), and you want to run that subroutine from another script - how do you do that exactly? Just using:
tell application "showTextApp" to showText()
won't work for your average script saved as an application. The secret is to save the target script as an application set to stay open. Once you've done this, you can use normal AppleScript syntax like this:
tell application "showTextApp"
activate
showText()
end tell
Simple!
7 " How to access variables within a parent object or script.
You've got a variable you've set in QuarkXPress", and you want to work with it inside your script for a moment; how do you access it? Just using its name won't work (you'll get the "variable xxx is not defined" error). The trick is to do either of these two things:
Use a full specifier, including the application or script name, for example:
get theVariable of application "QuarkXPress"
Or, use the most useful keyword in AppleScript: my. Example:
set my theVariable to 3
8 " How to use different OSA languages within AppleScripts.
First, make sure you're talking to an application which understands your other OSA language. ;-) Then, just place your script inside quotes, and pass the whole thing using the do script command, like this:
tell application "QuicKeys Toolbox"
do script "--whatever"
end tell
9 " How to launch an application in the background, and keep it there.
You'll have noticed that an empty tell statement doesn't work; you must send an actual message to the application in order for it to be launched, and activate brings it to the front, which we don't want. So how do you do it? Simple: just send it an event it doesn't understand, and trap the error:
tell application "FileMaker Pro"
try
run -- apps can't receive the run event except at startup, from the Finder, so this causes an error
on error -- when an error happens, do nothing!
end try
end tell
10 " How to launch an application using its creator-code, and avoid having to locate it.
Just use exactly this syntax:
tell application "Finder"
open application file id "8BIM" -- this example will open Photoshop
end tell