Applescripts MUCH slower as app than running under Script Editor

I was very surprised to find that an applescript I have been working on runs much slower when it runs as an application (bundle) compared with when it runs during development under Script Debugger.

The script deals with a lot of strings and records which I know are notoriously slow but I am curious as to why it runs so much slower as an app. In some cases, it is almost twice as slow. Honestly, I expected the opposite result…

Anyway, I would appreciate any insight you more experienced folks might have about this issue.

Thanks in advance,

JT

Well known behavior.
Try this structure


on run
run script o
end

script o
--your code
end script

Yvan KOENIG (VALLAURIS, France) mercredi 18 janvier 2012 11:10:21

Script applets take slightly longer to run anyway than scripts run in another application, since they have to be started up as applications in their own right.

The usual cause of extreme slowness in a script applet is when it sends lots of commands to another application or applications. There’s a fair bit of overhead in the time it takes each command to reach the target application and for the appllcation’s completion acknowledgement to get back to the script applet. I don’t know why this should be worse with a script running in its own applet than with a script running in an editor or in Script Menu, but it is the case.

If a script really must be run as an applet, you have to find ways to minimise its contact with target applications. Besides writing more efficient code, there’s a trick (which Yvan has suggested) whereby, if you have a section of code containing a lot of commands for another application, you can put it in a script object and ‘run script’ the object. The speed gain appears to be because ‘run script’ is a command in the StandardAdditions OSAX and communication between OSAXen and applications is faster than between applications.

script fred
	tell application "Blah"
		
		-- Lots of commands for application "Blah".
		
	end tell
end script

run script fred

Thanks so much for the quick response. My script has a lot of interaction with Textedit. I will give the suggestion a try.

The good news is that I ended up building a little time test script to find the problem that I am sure I will use in the future. I may post it here if I can clean it up enough to not be too embarrassing…

Now if only we could do timings in ticks like the old days…

Thanks again,

JT

This thread may interest you, particularly posts #1 and #14.