Benches macOS

4

call another applescript

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

i need to move a section of code from an existing applescript into a new script, and then call that script file from within the original script - if that isnt too confusing...!! 😉

but more generally, it would be good to know how to call another applescript from inside an applescript. have tried "do script "path/to/file.scpt" and "run script "/path/to/file.scpt"

regards

jingo_man

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    'do script' isn't a valid command, so that won't work. You might be thinking of 'do shell script', but that runs a shell script, not an AppleScript.

    You have several options.

    do shell script

    You can use 'do shell script', using
    osascript, which is the command-line method to run an AppleScript:

    <pre class=command>do shell script "osascript /path/to/file.scpt"</pre>

    That is cumbersome, though - AppleScript launching a shell, to run an AppleScript...

    run script

    You can run a script directly, but you need to provide a Mac-style path to the script, not a unix-style path:

    <pre class=command>run script "HD:path:to:file.scpt"</pre>

    (you can use POSIX file to coerce a Unix path into a Mac path/alias)

    load script

    You can also load the script into memory and call it within your script. For example, this snippet will load a script from disk and then call the 'handlerName()' function within it (as opposed to the others that only call its run handler):

    set myScript to load script "HD:path:to:file.scpt"

    set scriptResult to myScript's handlerName()

  2. 0

    jingo_man

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks for the suggestions - this looks like what i am attempting to do

    i created start.scpt:

    run script "Macintosh HD:Users:<username>:Documents:woohoo.scpt"

    woohoo.scpt contains:

    display dialog "woohoo"

    but i get the following error message when i run start.scpt:

    A identifier can’t go after this identifier.

  3. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    You get that error message because you're actually trying to run the path string itself.

    E.g.

    The next line works because command's argument is text consisting of valid script source:

    run script "display dialog "woohoo""

    But the next line won't work because command's argument is not valid script source:

    run script "Macintosh HD:Users:<username>:Documents:woohoo.scpt"

    You should have written:

    run script file "Macintosh HD:Users:<username>:Documents:woohoo.scpt"

    or:

    run script ("/Users/<username>/Documents/woohoo.scpt" as POSIX file)

    Cheers,

    H

  4. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    PS.

    The 'run script' command can handle both script's source text and compiled script.

    Thought I should mention this plainly. 😉