I have an extremely large (over 10,000 lines long) script that I’m having problems saving.
When I attempt to save it in the script editor, I get the following error
At first I thought it was a problem with the script editor, but when I attempted to use the osacompile command, i got the following error
$ osacompile -c "aplt" -t "APPL" -o blah.scpt applescripttest.txt
## Component Manager: attempting to find symbols in a component alias of type (regR/carP/x!bt)
storage error: Stack overflow. (-2706)
So, figuring it was a problem with the length of the script, I kept removing lines until I could successfully compile the script.
A file that was 225,022 bytes in size would not compile.
A file that was 224,926 bytes in size would compile.
Is this a built in limitation of AppleScript, or is something screwy going on with my machine. Short of rejiggering my script to spread the code around, does anyone know of a solution?
Yes. There are certain limits in the compiler. Eg:
set verylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablenameverylongvariablename to 4
I don’t know exact data, but you can’t compile more than x Kb, depending on a bunch of factors: number of handlers, variables with large names, etc.
The typical workaround is splitting the script in various pieces which you can load and use from the “main” script.
Yes. Both compiler and interpreter have all sorts of built-in limitations. It’s a pretty basic language and not very scalable.
10,000 lines sounds suspiciously big though. Does a lot of it consist of lines that have been cut-and-pasted and then tweaked slightly? If so, go read up on how to use structures like repeat loops and handlers to eliminate this unnecessary duplication. You can also post blocks of code here if you need advice on structuring them more efficiently.
For example, instead of writing hundreds of lines that all look like:
use a repeat loop to perform the same operation in a fraction of the code:
tell app "Some Text App"
repeat with anItem in {{"&", "&"}, {"<", "<"}, {">", ">"}, ...}
set {findStr, replaceStr} to anItem
find findStr in document 1 replacing with replaceStr
end repeat
end tell
If the same code crops up in more than one part of your script, wrap it in a handler that can be called each time it’s needed:
on textToHTML(documentRef)
tell app "Some Text App"
repeat with anItem in {{"&", "&"}, {"<", "<"}, {">", ">"}, ...}
set {findStr, replaceStr} to anItem
find findStr in documentRef replacing with replaceStr
end repeat
end tell
end textToHTML