How Can I Open AppleScript App with Arguments

15,544

Solution 1

Why doesn't anyone mention quoted form of? When you want to send arbitrary data as an argument to an application you should use quoted form of. When quotes, spaces and other special characters are in the given path the command will break down in de previous examples.

on run argv
    tell application "Terminal"
        do script "./myscanprogram " & quoted form of (item 1 of argv) & " 2>&1"
    end tell
end run

Since you mentioned you're new to AppleScript does it have to run in the Terminal.app or is a shell enough? AppleScript has the command do shell script which opens a shell, execute the text and return the stdout back to you.

on run argv
   do shell shell script "/path/to/myscanprogram " & quoted form of (item 1 of argv) & " 2>&1"
end run

Last but not least. If you don't want the output of the scan program and don't want AppleScript to wait until it's finished you can use

on run argv
   do script "/path/to/myscanprogram " & quoted form of (item 1 of argv) & " &>/dev/null &"
end run

Solution 2

Wondering why you're using your Terminal to address an AppleScript that uses the Terminal again, but maybe I just don't know your circumstances...

Applescript:

on run argv
    tell application "Terminal"
        do script "./myscanprogram " & (item 1 of argv) & " 2>&1"
    end tell
end run

Call from osascript inside your Terminal:

osascript pathToYourScript.scpt myargument
Share:
15,544

Related videos on Youtube

T Varcor
Author by

T Varcor

Updated on September 23, 2022

Comments

  • T Varcor
    T Varcor over 1 year

    I Have an AppleScript that runs a Scan Program (commandline) that scans to a specific folder. I need to pass arguments to the applescript that in-turn passes the arguments to the terminal.

    In a terminal I want to run open -a /Applications/MyScanApp.app myargument and the AppleScript runs. How can I pass that argument? Thank You for Your Help! I am normally a PHP programmer and this is something completely different to me.

    My AppleScript:

    tell application "Terminal"
        do script "./myscanprogram myargument 2>&1"
    end tell
    
  • ShooTerKo
    ShooTerKo about 9 years
    Yes, you're right. I had a strange fear of quoting the arguments twice, because you have to quote your input if it contains spaces. Otherwise the argument could be parsed as two arguments instead of one. BUT: I tested it and the savest way is the use of quoted form indeed and it works if the shell argument is already quoted, too! ;-) thumbsup
  • ManuelSchneid3r
    ManuelSchneid3r over 2 years
    Can you also provide an example that generalizes the usecase by using the entire argv list?
  • dj bazzie wazzie
    dj bazzie wazzie over 2 years
    I'm aware that my reply doesn't answer the title but it does answer the question which SO is all about. The answer to your question, argv (or any variable name after on run) is always the "the entire argv list". The problem is not argv itself, but how you execute the script and pass the arguments correctly.