Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 593
T Varcor Avatar asked Mar 25 '15 14:03

T Varcor


People also ask

How do you access arguments within a script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

Can bash scripts take arguments?

A command line argument is a parameter that we can supply to our Bash script at execution. They allow a user to dynamically affect the actions your script will perform or the output it will generate.

What script does Apple use?

AppleScript is a scripting language created by Apple. It allows users to directly control scriptable Macintosh applications, as well as parts of macOS itself.


1 Answers

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
like image 82
dj bazzie wazzie Avatar answered Sep 23 '22 02:09

dj bazzie wazzie