Load... and unload script?

What happens to a script loaded with the “load script” command? Does it stay in RAM until the main script quits? If so, should it be unloaded when it is no longer needed – and, if yes, how?


Note that AppleScript’s Standard Additions (where “load script” lives) does not include an “unload script” instruction. When you load a script is is compiled in with the script that loaded it and dies when the script does.

I have a question that is somewhat related to the above one. I created a subroutine that contains the following “run script” line :

“if run script ((thisInstancePosition as string) & findPositionQualifier) then set properties of thisInstance to changeAttributes”

where thisInstancePosition is an integer and findPositionQualifier is a mathematical symbol followed by a number (eg. “≥12”)

The subroutine containing the above line is currently called a few dozen times by the loading script and will be called many more times as I further expand its functionality. My question is, what happens each time this line is executed? Does a new instance of the run script get loaded into memory everytime it’s executed (causing a small increase in memory usage each time) or does this run script get expunged from memory immediately after execution?

My concern is that while each instance of the script might not consume much memory, the documents it is being applied to can be monstrously huge so it is possible I could run into memory problems while the script is running.

Does anyone know what will happen in this case?

I think (but don’t know for certain) that when you compile your caller an instance of the external script is included in the compilation so there shouldn’t be a steady growth. To check, you might run your script against a ‘humongous’ example and see whether it grows in the Activity Monitor.

Anyway, keep in your mind that you won’t use lotsa RAM at all, unless you enclose in a variable for example 50MBs of text or you’re loading a 100MB image in your Studio project, etc. In a quick test, for example, loading a 32Kb-code script (that is lots of code) takes about 2 extra MBs (from 20 to 22, aprox., of “real memory”), which is not a real issue when Safari is actually filling 70MB in this session.

If you’re worried, though, you can allways “reset” the variable assigning it some dummy value after you use it (ie, “set var to 5”).

Here is a little FAQ about this issue: http://applescriptsourcebook.com/articles/228_0_17_0_C/


When you load script you usually set it to a variable. The value of the variable stays in memory. You can reset the variable to another value like missing value or declare the variable as local. In the later case, the variable is undefined on quit. Here’s an example:

property first_run : true
– local s
if first_run then
set s to load script alias “Macintosh HD:Users:kel:Desktop:s”
set first_run to false
end if
run script s

If you uncomment the local declaration, then an error occurs because s is undefined on the second run.