Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Execution speed of Applescript "script" vs "application"

I have a rather long applescriopt which calls several other applications. It has a run handler. It is saved from Applescript Editor as an application myscript.app.

The script executes when run as an application (by double-clicking myscript.app) and also executes (giving the same results) if it is opened in Applescript Editor and run by clicking the Run button in Applescript Editor.

However, when run as an application it takes about 50% longer (940 seconds for my case) than when it is run from the Applescript Editor (660 seconds). (I was surprised - if anything I would have thought it would execute faster when run as an application).

Is this normal and does anyone have an explanation for this? Thanks

Posted on Oct 6, 2009 10:48 AM

Reply
8 replies

Oct 6, 2009 6:57 PM in response to fusion

Hello

If I'm not mistaken, you're experiencing a known issue regarding applet performance.
Short answer is that using 'run script' osax to run your main code will improve the applet speed.
See the following threads for details.

Applet speed
http://discussions.apple.com/thread.jspa?threadID=1633798

GUI scripting question
http://discussions.apple.com/thread.jspa?threadID=1103383

Making applescript run faster...
http://discussions.apple.com/thread.jspa?threadID=1877444

Hope this may help,
H


-- APPLET TEMPLATE
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
*)
script o
-- your main code here
end script
run script o -- use 'run script' osax to run your main code
-- END OF APPLET TEMLPATE



-- APPLET TEMPLATE 2
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
* This revised model will preserve the properties in your main code if any.
*)
script o
-- your main code here
return me -- [1]
end script
set o to run script o -- [2]
(*
[1] This at o's end along with assignment in [2] will let this model to preserve o's properties.
[2] Use 'run script' to run o and update o with the returned object.
*)
-- END OF APPLET TEMPLATE 2



--APPLET TEMPLATE3
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
* This revised model will preserve the properties in your main code if any.
* Also it will guarantee the 'return me' statement to be executed regardless of the contents of your main code
*)
script o
script o1
-- your main code here
end script
try
tell o1 to run
on error --
end try
return me
end script
set o to run script o -- to preserve the properties in o1
--END OF APPLET TEMPLATE3

Oct 9, 2009 8:28 AM in response to Hiroto

Hiroto, you have solved the problem and I hope you don't mind if I suggest a modification to your Template 1 (Template 1A below) to increase usability.

It turns out that the same effect (speeding up the application to be the same speed as if launched from Applescript Editor) is obtained if the script evoked in do script has only one line calling a subroutine which contains all the top level code.

This has the advantage that, if this applescript is loaded into another applescript , its main code and the other subroutines are simply accessible from that other applescript .
e.g. Suppose that the template 1A below (with its user code) is in a file named "applescript2.app" in the folder with full path "path toapplescript2".
Then for example in applescript1 the statements
run
set applescript2to (load script alias ("path toapplescript2" & "applescript2.app")
applescript2's h_sub1()
end
will access the subroutine of applescript2 (of course applescript1 could also be modified according to the template.

Another advantage - all the user code is at the end.

I have also added an open handler for drag-and-drop.

Please look at Template 1A , modify it as you see fit, and post it and modified Template 2 and 3 if you feel that it is appropriate.

-- APPLET TEMPLATE 1A
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
top level code in h_run() and subroutines remain accessible if this script is
loaded into another script via "load script"
*)
global theInputList
on open (theInputList) -- to provide for drag and drop input from Finder
run script o -- use 'run script' osax to run your main code
end open
on run
run script o -- use 'run script' osax to run your main code
end run
script o
h_run()
end script
on h_run()
-- your top level code here
end h_run
on h_sub1()
-- your subroutine code to be called as h_sub1() here
end h_sub1
--other subroutines here
-- END OF APPLET TEMPLATE 1A

Oct 9, 2009 11:23 PM in response to fusion

Hello

We can modify those templates in many ways as we see fit.
The only point is to use 'run script' osax to run the vital code in applet/droplet to improve its performance.

We may define handler anywhere as far as it's in the scope of the caller and it's in the top level of a script object. (Script file itself represents a script object.)
It would be convenient, as you suggest, to place handler definitions in top level of script file if the applet also serves as a library of handlers. (I would make a compiled script of handlers and load it into the applet and others, though.)

Now that you mentioned droplet, here's my versions of droplet templates derived from applet templates above. 😉
(I skipped TEMPLATE 2 because it should be replaced by TEMPLATE 3 for safety.)

Cheers,
H


--DROPLET TEMPLATE 1
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
*)
script o
on run argv
if argv = {} then
-- original run handler code here
else
open argv
end if
end run

on open aa
-- original open handler code here
end open
end script
on run -- wrapper for run handler
run script o with parameters {}
end run
on open aa -- wrapper for open handler
run script o with parameters aa
end open
--END OF DROPLET TEMPLATE 1



--DROPLET TEMPLATE 3
(*
Running script via 'run script' (standard osax) in an applet/droplet
may result in faster execution for some reason.
* This revised model will preserve the properties in your main code if any.
* Also it will gurantee the 'return me' statement to be executed regardless of the contents of your main code
*)
script o
script o1
-- your main code here
on run
-- original run handler code
end run
on open aa
-- original open handler code
end open
end script
on run argv
try
if argv = {} then
tell o1 to run
else
tell o1 to open argv
end if
on error --
end try
return me
end run
end script
on run -- wrapper for run handler
set o to run script o with parameters {}
end run
on open aa -- wrapper for open handler
set o to run script o with parameters aa
end open
--END OF DROPLET TEMPLATE 3


Message was edited by: Hiroto (fixed typo)

Dec 23, 2009 12:53 PM in response to fusion

I think that your "DROPLET TEMPLATE 3" is just what I need to solve my problem. Unfortunately, this is my very first attempt at writing AppleScript code and I do not understand what you mean by "-- original run handler code" and "-- original open handler code". As far as I know, all I have is main code which goes in place of your statement "-- your main code here". What do I put as "run handler code" and "open handler code"?

Thanks for your help. I think I am getting closer to a solution to my problem, but more problems have been handed me!

Dec 24, 2009 11:05 AM in response to Fred Nelson

Hello

Some points.

• If you script does not have open handler, you may use APPLET TEMPLATE 3 in lieu of DROPLET TEMPLATE 3.

• Your script may not have explicit run handler. In such case, the code in the top level of script (except for property, global, script object and handler declarations) is treated as if it is put in an implicit run handler. E.g.,

--SCRIPT1
property m : "Current date = "
display dialog m & curdate()
on curdate()
return current date
end curdate
--END OF SCRIPT1

is equivalent to :

--SCRIPT1a
property m : "Current date = "
on run
display dialog m & curdate()
end run
on curdate()
return current date
end curdate
--END OF SCRIPT1a

cf.
ASLG > About Handlers > Handlers in Script Applications
http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangG uide/

And APPLET TEMPLATE3 applied to SCRIPT1 and SCRIPT1a will yield the following APPLET TEMPLATE 3 E.g.1 and APPLET TEMPLATE 3 E.g.1a respectively.

--APPLET TEMPLATE 3 E.g.1
script o
script o1
property m : "Current date = "
display dialog m & curdate()
on curdate()
return current date
end curdate
end script
try
tell o1 to run
on error --
end try
return me
end script
set o to run script o
--END OF APPLET TEMPLATE 3 E.g.1

--APPLET TEMPLATE 3 E.g.1a
script o
script o1
property m : "Current date = "
on curdate()
return current date
end curdate
on run
display dialog m & curdate()
end run
end script
try
tell o1 to run
on error --
end try
return me
end script
set o to run script o
--END OF APPLET TEMPLATE 3 E.g.1a


• The way to apply the DROPLET TEMPLATES is basically the same except for, obviously, the presence of open handler.

Hope this may help you get the picture.
Hiroto

Execution speed of Applescript "script" vs "application"

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.