Answer: Pass parameters to script from command line/shell

I’ve seen this question out on the net a lot, and it took a while to find it myself: “How do you pass parameters to an AppleScript from the command line/shell/terminal?”

Most responses seem to be “osascript can’t do that”. But I found something else: “osasubr”. It does pass parameters to the script, and receives a response.

You can download it here:

http://www.turbozen.com/mac/osasubr/

Have fun!

– Clive

Wouldn’t


do shell script "some script"
set shellscriptresult to result

do it?

What Typonaut is simply saying is that there are time when you want to call a handler in a script and pass that handler a parameter or two.
Osasubr is that answer. Read the man page on osascript, and it tells you that a bug in the app is that it will not pass arguments to a script.
An example would be to pass information to a script handler and use that handlers functions with it parameters.
Usage: osasubr scriptfile subr [arg1 … argn]

on mesg1(alert_message, “button1”, “button2”)
set result to display dialog alert_message buttons {button1, button2}
button returned of result
end mesg1

CLI:
osasubr /path/to/script mesg1 “A message from osasubr” “OK” “Cancel”

Caveats

  1. use posix path to the script file works best
  2. Watch you handler naming convention, my expirience has been mixed when using uppercase mixed with numbers.

There is another way of dealing with osascript and script handlers and that is to use the >>end
osascript -e >>END insert script <<END

osascript <<END
tell application “SystemUIServer”
activate
display dialog “#{question}” default answer “#{answer}”
set answername to text returned of the result
do shell script ("/bin/echo " & answername)
end tell
END

I may be wrong, but in my opinion, wrong or right, I do like the osasubr solution the best when dealing with CLI and applescript solutions. Also, the developer is responsive and knowledgeable of not just his app but of *nix, OS X and applescript.

Hi,

You can pass parameters to a compiled script by using the AppleScript command ‘run script’. For instance, suppose I have a Compiled Script on my desktop:

on run p
set t to item 1 of p
tell application “Finder”
activate
display dialog t
end tell
end run

The Finder is activated just to display the dialog because some app capable of this is need. Then, I can run this script from the terminal with:

osascript -e ‘run script file “Macintosh HD:Users:kel:Desktop:ParamScript” with parameters {“hi”}’

It’s like a middle man.

gl,

i don’t quite understand this yet. how does the “hi” come in?

lets say i want to make a simple applescript that pops up a window that says whatever is in the param. we’ll call it dispScript, something like:

display dialog "text"

except in place of “text” i want it to display whatever is passed in the command line.
so if i run (from the same directory)

osascript -e 'run script file "dispScript" with parameters {"hi"}'.

what do i change in dispScript to make it display “hi”? does the p in your script have something to do with that?

You would run


display dialog hi

To show what’s in the’ hi’ variable.

so do we defind what hi is in the command line? like

osascript -e 'run script file "dispScript" with parameters {hi="display this!"}'.

or something…

p is just a list of parameters. You do this for consistancy. The list can contain 1 or more parameters. Here’s a slightly different Compiled Script:

on run param_list
tell application “Finder”
activate
repeat with this_param in param_list
– should check for class of object for coercion
– set param_class to (class of (contents of this_param))
display dialog this_param
end repeat
end tell
end run

You can run this from the terminal script with:

osascript -e ‘run script file “Macintosh HD:Users:kel:Desktop:ParamScript” with parameters {“hi”,7,“bye”}’

Here, the variable p or in this script, param_list = {“hi”,7,“bye”}.

Note that 7 will be an integer. Your script should error check for the class of the parameters, but ‘display dialog’ coerces integer to string here.

gl,

i get an error when i run that code:

0:84: execution error: Can't make some data into the expected type. (-1700)

this is what i’m running from iTerm:

[:~/Desktop] userName% osascript -e 'run script file "a.app" with parameters {"hi"}'

my AS is directly copied from yours and its named a.app and it is on the desktop. this should be working? right?

Hi Duffman,

The current durectory in AppleScript is the folder for the current application that’s running the statements of the script. If I type this in the terminal (and I’m a newbie with this app :-)):

osascript -e ‘run script “path to me”’

I get this:

alias Macintosh HD:usr:bin:osascript

When you use just the name of a file in the reference like:

file “a.app”

this is called a partial reference. When AppleScript gets a partial reference, it tries to complete that reference by looking to the current application. By theory, it should look in:

alias Macintosh HD:usr:bin:

for the script a.app. Anyway, if you placed the script there in the bin folder, then it should work. Hope I’m getting this right with the osascript thing.

There are several ways to make a complete reference so that AppleScript knows where to look for the script. In AppleScript, the path to the file begins with the startup disk (Macintosh HD in my case) and ends with the file or folder. Items are separated by colons. So the path to my script on the desktop would be:

“Macintosh HD:Users:kel:Desktop:ParamScript”

and adding file or alias at the beginning makes it a reference:

file “Macintosh HD:Users:kel:Desktop:ParamScript”

Note that "ParamScript is the name of my script file.

Other ways to make the reference are use the ‘path to’ command with concatenation, use the Finder, use ‘posix path’, use colon, etc. It gets deeper when you use Finder references. Here’s one for your script which is located on the desktop and you can use in the terminal besides the reference mentioned above.

osascript -e ‘run script file (“” & (path to desktop) & “ParamScript”) with parameters {“hi”}’

gl,

awesome, got it working. thanks for all your help!

http://developer.apple.com/releasenotes/AppleScript/AppleScript.html

The osascript command line tool can now call scripts that take parameters. Any arguments following the script will be passed as a list of strings to the direct parameter of the “run” handler. [3165225][/url]