4.5 Script Text File
The Script Editor offers the option to
save a script as text (or "script
text," depending what version of the Script Editor
you're using). A script text file is simply a text
file, such as can be opened by any word processor. No bytecode is
saved into the file. (But the Script Editor does also try to compile
the script even when you save it as text, which seems unnecessary.)
|
A script text file consists of ordinary text in the default system
encoding (usually MacRoman). It has file extension
.applescript on Mac OS X and is of type
'TEXT' on Mac OS 9 and earlier. The Script Editor
saves such a file with both features, and it can be opened on both
platforms.
|
|
A script text file can be opened with a dedicated editor such as the
Script Editor. The situation is then exactly as if you had just typed
the code into the Script Editor: if the code is valid and all
external referents can be found, the code can be compiled and run.
A script text file cannot generally be run from a script runner
application, such as the Script Menu, because the script is not
compiled and the application is not prepared to do the compilation
for you.
Since a compiled script file can be decompiled and edited further, as
well as executed directly, what's the good of a
script text file? Apple's documentation implies that
it isn't much good, and calls it a
"last resort" format;
Apple's advice would seem to be that a compiled
script file is the standard and most readily usable and communicable
format, the format you would use when sending a script to someone
else to be run on another machine.
However, I'm not so sure that a compiled script is
so very communicable, or that it is better for such purposes than a
script text file. There are various formats of compiled script file,
and the new ones aren't backward-compatible: a
compiled script file saved by the current version of the Script
Editor can't be opened by some older versions of the
Script Editor, and a compiled script bundle isn't
backward-compatible to any system before Mac OS X 10.3
("Panther"). Plus, as mentioned in
the previous section, it is possible for a compiled script to face
difficulties with regard to external referents that it
can't locate; if these external referents
can't be located at all, the script
can't even be opened for editing, let alone
executed. By contrast, a text file can always be opened under any
system and on any machine; AppleScript and the Script Editor are
present on every machine, so it is always possible, with valid code,
to compile the script afresh.
Not infrequently I have seen
AppleScript utterly confused about a reference to an application when
a compiled script is moved to a different computer. For example,
recently I downloaded from the Internet a compiled script that, when
I tried to run it, generated a mysterious error message and
didn't work, so I opened it and found references in
it to an application that wasn't an application at
all (it was just some file buried deep in
/usr/share/emacs). Clearly what had happened was
that the compiled script had lost track of the application it was
supposed to be pointing at, but it was so confused that it
didn't even realize this, and instead of asking me
for the application's location, had substituted for
the name of this application the name of a completely different file.
The really devilish part of this situation was that there was no way
for me to learn the name of the application the script was really
supposed to be pointing to (since the name had been changed in the
course of decompilation). Had this script been text, there would have
been no difficulty at all, because the name of the application in the
text would have been preserved, and I would have been able to
compile, save, and execute the script on my own machine without any
problem.
(In this regard, a valuable feature of Late Night
Software's Script Debugger is that when it saves a
compiled script file it also saves the original text into the file,
as a TEXT resource. In a pinch, therefore, the
text can be recovered and used to help fix problems with external
referents.)
|