2

I have been trying to figure out how to pass multiple parameters from an Applescript to a Terminal Command Script. For example when running a terminal command file you are able to receive parameters programatically like so:

#!/bin/bash

var=$1

var=$2

The Applescript Code that I have been working with is below for reference:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me))

set thisFile to "Dev"

set testTarget to "/Users/lab/Desktop/TestTarget/"

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges

Where I think I have gone wrong is the input of the second parameter. When I only had one parameter it went through just fine:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges

I am curious as to what the correct syntax would be for passing in this second parameter. If anybody has any suggestions please let me know! Also if you need more information I would be happy to provide it!

1 Answers1

5

You just need to add a space between your arguments. Right now, there is no space being added between thisFile and testTarget. Your command looks like this:

/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/

Change your shell script line to:

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges

Something that I find helpful when building a script is to make sure my shell commands are correct before running them. So instead of building it directly, store the command in a variable and log it. Later, replace the logging statement with the do shell script command.

set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
log shellScript
-- do shell script shellScript
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • This worked! I would upvote this post, but apparently I don't have enough stackoverflow points :( But thank you very much! – mmilutinovic1313 Mar 03 '15 at 14:56
  • @mmilutinovic1313: You don't need reputation points to _accept_ an answer to a question of _yours_ (click the checkmark). – mklement0 Mar 03 '15 at 17:10
  • 1
    Another tip worth mentioning: to ensure that strings such as (literal) filenames are passed to the shell unmodified (to protect strings from shell expansions), prefix them with `quoted form of`. – mklement0 Mar 03 '15 at 17:13
  • And, it's helpful to remember "quoted form of shellScript" to handle spaces and quote characters. – jweaks Mar 03 '15 at 19:14
  • Thank you guys. Would you possibly also have any advice on trying to implement this type of scripting in a cocoa app also? I got this one working here, but when I switched it over to objective-c I have been running into some syntax errors. Here is the other post if you have time: http://stackoverflow.com/questions/28840814/applescript-syntax-multiple-args-inside-objective-c-file – mmilutinovic1313 Mar 04 '15 at 11:46