T Varcor
T Varcor

Reputation: 507

How Can I Open AppleScript App with Arguments

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

Upvotes: 19

Views: 25333

Answers (2)

ShooTerKo
ShooTerKo

Reputation: 2282

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

Upvotes: 10

dj bazzie wazzie
dj bazzie wazzie

Reputation: 3542

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

Upvotes: 23

Related Questions