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 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

This worked! I would upvote this post, but apparently I don't have enough stackoverflow points :( But thank you very much!
@mmilutinovic1313: You don't need reputation points to accept an answer to a question of yours (click the checkmark).
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.
And, it's helpful to remember "quoted form of shellScript" to handle spaces and quote characters.
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: stackoverflow.com/questions/28840814/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.