Benches macOS

8

Execution speed of Applescript "script" v…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Might it have something to do with [this hint|http://www.macosxhints.com/article.php?story=2006080109510225] from Mac OS X Hints?

    Something else: does your script run faster than the application even the first time you launch it from the AppleScript Editor after recompiling it?

    Message was edited by: Pierre L.

  2. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  3. 0

    fusion

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hiroto, thank you very much for your comment.

    I used your templates and the execution speed of the application was now practically the same as for the script fun from Applescript Editor.

    Thanks for solving the problem.

  4. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello fusion,

    You're quite welcome. Glad to be of help.

    Hiroto

  5. 0

    fusion

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  6. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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)

  7. 0

    Fred Nelson

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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!

  8. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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