Free since 2005 · No login required
AT

Academic Tutorials

Learn at your own pace

site-mobile-top-banner · 320x50

Apple Script File Format

Added 29 Jul 2008

You can save scripts in several file formats, including as compiled scripts, text files, applets, and variations of these. Scripts are usually saved as compiled scripts, with the .scpt extension. This format can be edited using AppleScript editors only, not just any text editor. Although you write scripts using text and you can usually see the text when you reopen a saved script, the script is actually saved as special tokens that are interpreted by the operating system’s AppleScript component when the script runs. You usually won’t notice this “what-you-see-is-not-what-you-saved” situation unless the script has some version issue or an application or scripting addition issue. What you have to remember is that the scripts you write, as discussed earlier, will be composed largely of terminology defined by scriptable applications. An application can define hundreds of keywords that you can include in your scripts.



Some of these keywords are the names of commands and classes defined by the application; others are the names of command parameters and object properties, and so on. For a script to compile properly, it needs the applications that are referenced in it to be present, and in some cases it also needs the applications to be open. In fact, if an application referenced in your script isn’t present when you try to open the script, you will either have to give up until you install it or trick AppleScript into thinking some other application is the missing one. If you do trick Apple- Script, you will have a hard time changing the application-specific terms you used in your script, since they will appear in their raw form inside double angle brackets instead of the easy-to-read English-like AppleScript text you’re used to seeing. For instance, a script that used the copy command in InDesign CS2 will show the following code when the application isn’t available: <>.

So how can all that affect you and your script? The main reason why you won’t be able to see your code properly is that the application, scripting addition, or application’s AppleScript plug-in isn’t available.

Note Some applications, mainly from Adobe, don’t incorporate their scripting terminology into the actual application but rather have a plug-in manage all the AppleScript-related activity. This will be a noticeable issue only if the application doesn’t ship with AppleScript enabled but rather leaves you to activate it. Otherwise, it makes no difference. Rest assured, however, that the actual script is fine. The next time you open it with the target application available, it will return to its old self and display properly. As mentioned, when you save a script, you can specify its file format. Three main kinds of script files exist: regular compiled scripts, which are the native AppleScript file format; plain-text files; and applets. On top of that, OS X has a new option for saving compiled scripts and script applets as bundles. Bundles are folders that appear and behave as files, which is great when you want your script to appear as a single unit but actually conceal related files as part of it. For most purposes, your script will start as text entered in Script Editor but will be most likely saved as a compiled script file. At any point you can save the script as an applet and place it anywhere for use. You can use a script in its AppleScript format state (nonapplet files are saved with the .scpt extension) in one of the following ways: if the script is loaded into another script, you can use it in an application’s Script menu where available (InDesign, BBEdit, the Finder, and so on); you can execute it from the Unix command line using the osascript command; or you can simply run it from Script Editor.

Also, two similar application-based formats exist: applet and droplet. A regular applet runs the script when you double-click it, and unless it’s saved as a “stay-open” applet, it quits after the script is done. A droplet can respond to files being dropped on it. This is useful for a script that processes files or folders. When saving a script as an applet, you can’t specify whether you want it to be a droplet or a normal application. Instead, AppleScript determines this based on the contents of your script. Basically, if you tell AppleScript what to do in case some files are dropped on it, it will become a droplet; otherwise, it will be a normal application.

So, why then would you ever save a script as text? Well, you might do this for a few reasons. First, saving and opening scripts as text doesn’t require that the script be compiled. This is useful when something stops you from compiling the script, namely, an error in your AppleScript text. Second, when you send scripts to be used on other Macs, you should try to send them both as compiled scripts and in text format. Getting the script’s text allows the other person to open the text version of the script in Script Editor and compile it on the Mac on which the script will eventually run. This helps iron out issues that may stem from using a slightly different version of an application and gives the script a chance to see where all the applications are located on the new Mac. Another format-related option you have is to save a run-only version of the script. This will allow the script to run, but no one will be able to read it. Distributing a script as a run-only version is useful when you want to protect the rights to your proprietary code.