I like to show my conclusion of benchmarking AppleScript. When we do compare of some
algorithm or method to use we should also consider what application we use to execute it.
With this simple example to run 100 do shell script. (maybe that was not what you think)
Ps. I have only include 5 different way to execute code. I use seconds and with round to nearest
decimal. If its more closer to half I use that instead. I have also measure some execution with
my mobile and round to the nearest decimal - 0.5 sec
Lets assume you have always thought do shell script is slow. To run 100 do shell script
in a applet or osascript in less 2 seconds I do not think is slow. I have done similar test
with python print function and that was less 1 second.
Why Apple Script Editor take more 1 minute and 30 seconds to execute is very strange.
Benchmark of ‘do shell script’ Running Script
Script to execute:
set x to 0
repeat with i from 1 to 100
set x to x & (do shell script "echo " & i)
end repeat
display dialog (last item of x) as text
So would it be far to compare AppleScript with AppleScriptObjC and JavaScript
when we do not talk how we execute it and only the method or algorithm it use.
Only my conclusions and believe… there is some serious bugs or something else that infect the overall speed in Apple’s Script Editor.
To do more research I run this code in Apple’s Script Editor and its only took 2 sec to complete.
So running 100 do shell script from Script Editor with AppleScriptObjC to execute Automator workflow with input is faster and any Script Editor include Script Debugger (tests above).
To execute below code in Script Debugger give the same time as Apple’s Script Editor.
So could we make the conclusion that run script in Automator is faster and it was
to Run AppleScript action. I’m not sure… if applet and osascript in command-line
was the fastest in this example code I could understand run script was faster and run AppleScript.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "Automator"
use scripting additions
property theResult : missing value
property theError : missing value
set theScript to "
set x to 0
repeat with i from 1 to 100
set x to x & (do shell script \"echo \" & i)
end repeat
display dialog (last item of x) as text
"
on runWorkflow:theObject
set {theURL, theInput} to theObject as list
set {my theResult, my theError} to current application's AMWorkflow's runWorkflowAtURL:(theURL) withInput:theInput |error|:(reference)
end runWorkflow:
set theInput to theScript
set thePath to "/Users/f.gustafsson.user/Desktop/runAS.workflow"
set theURL to current application's NSURL's fileURLWithPath:thePath
my performSelectorOnMainThread:"runWorkflow:" withObject:{theURL, theInput} waitUntilDone:true
if theError is not missing value then error theError's localizedDescription() as text
return theResult as list
Next…
Lets pipe text (action) to run script in Automator and compare to Run AppleScript action…
that was slower but not much. The time to get text and pipe the text string to next action
we lose some speed. The strange part is… that is exactly what we do in Script Editor when
we pipe text string input with AppleScriptObjC but its much faster to display the dialog box.
Lets compare vanilla AppleScript (first script) with pipe workflow AppleScriptObjC (second script)
with Script Geek and every time I run it the vanilla AppleScript was slower.
– do shell script 100 times
Vanilla AppleScript 2,2 sec
AppleScriptObjC 1.8 sec.
Next
Lets save the (first script) to doShellScript.scpt and load it as script object and run script.
And compare it with the AppleScriptObjC workflow script. Now you will see to load a script object
and execute it with run script is slower and runnings a vanilla applescript (first script).
set thePath to load script (path to desktop as text) & "doShellScript.scpt:" as alias
run script thePath
To run it from separate script and execute it is slower (Apple’s Script Editor).
If Apple’s Script Editor and Automator is the only option for you. And you think your code is slow
I would consider to run it as applet or osascript. I would also consider to use Automator with
AppleScriptObjC. All this depends what your algorithm/functions does.
Its very clear to me:
Apple’s Script Editor is the slowest option (in my test) compare to other options to execute AppleScript code. That doesn’t mean its everytime… it could happen and you maybe are thinking its something wrong with your code and that is not the case.
It could be when we measure things we also use other application to execute it for measure.
Same code in different application do different things to reach the result.
So how could we do this in a broken Apple’s Script Editor.
Maybe like this.
do shell script “open ~/my_script_wih_100_doShellScript.app”
or
do shell script “osascript ~/my_script_with_100_doShellScript.scpt”
The execution of 100 do shell script in the repeat loop use applet or osascript and become
much faster to return the result we want in Script Editor. If we know do shell script is slow
and we know applet and osascript is faster why not use that instead.
Now Apple’s Script Editor only take 2 second to execute 100 do shell script.
Or more correct in Script Geek… 0,9 sec to execute.
do shell script “osascript ~/Desktop/doShellScript.scpt” – do shell script in repeat loop (100 times)
Next
Lets compare osascript vs script editor.
As you could see osascript use C++ Standard Library and do not use any security framework.
You also see it doesn’t use OSAKit and if we do the same with applet you will only
find CoreServices framework.
Would it be correct to assume that C++ Standard Library and lacking of security framework
would be much faster and Script Editors implementations to execute source code.
If anyone know more I would be very happy for any respond ??