Improving AppleScript Startup Time with Compilation
Applescript is useful but interpreted scripts are fairly slow to start.
Consider a script that only has the number 1
in it.
$ echo "1" > example.scpt
When run with osascript example.scpt
, this will output 1
.
A timed run of the script shows it takes roughly 430 milliseconds on my machine.
$ time osascript example.scpt
1
real 0m0.429s
user 0m0.053s
sys 0m0.053s
We can't have a much simpler script than this, so it looks like the price of using interpreted Applescript is a minimum of ~430ms. If you have a script that runs frequently, this might be too slow.
You might not be aware that you can compile AppleScript. Not surprisingly, this compilation improves the startup time.
$ osacompile -x -o compiled-example example.scpt
$ time osascript compiled-example
1
real 0m0.252s
user 0m0.034s
sys 0m0.036s
We're down from over 400ms to 250ms. That's still not ideal to run in a tight loop, but the savings might be compelling depending on your usage.