Passing Multiple Parameters from Applescript to Terminal Command Script - macos

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!

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

Related

POSIX path in AppleScript

I am trying to run a bash script as sudo from AppleScript and I want to fill in the user name and password, I have googled out this solution:
on run {input, parameters}
do shell script ("sudo /Applications/XAMPP/xamppfiles/run_mysql_sudo '" & POSIX path of input & "'") user name "username" password "pass" with administrator privileges
end run
but I get an error saying: Can’t make POSIX path into type Unicode text.
Does anyone know how to fix it?
Most likely input is a list of items. Even if it only has one item in it, it's still a list so you have to get the first item of the list to use it. Your error is because you can't get the posix path of a list.
Second, in applescript we use "with administrator privileges" instead of sudo. We don't use both. Last applescript has "quoted form of" to properly quote things.
As such, give this a try...
set firstItemOfInput to item 1 of input
do shell script ("/Applications/XAMPP/xamppfiles/run_mysql_sudo " & quoted form of POSIX path of firstItemOfInput) user name "username" password "pass" with administrator privileges

How to make "which some_command" work from in an Applescript?

I am writing an Applescript that uses sox, which is not available by default in MacOS X. I have everything working on my system, but I want the script to work for anyone else. Because the sox command is not where Applescript can automatically locate it, my hard coded 'do shell script' string must start with "/usr/local/bin/sox" instead of just "sox."
So, I need a way to locate sox, via Applescript, on any system. In terminal, "which sox" returns "/usr/local/bin/sox" without issue. In Applescript, "which sox" returns "sh: which sox: command not found." This is true for any command, not just sox.
I have tried every variation I can think of, without success. I will list every syntax I have used and the result I get. If anyone knows another/successful route to getting the path to sox from within an Applescript, PLEASE let me know.
Note that I have other shell commands (e.g. rm, mv, etc.) working in the script.
Lines that return "sh: which sox: command not found": (Note: I have also tried each with "which sox" replaced by which_sox, and a prior line to define which_sox as "which sox")
do shell script quoted form of "which sox"
do shell script (quoted form of "which sox") as text
do shell script (quoted form of "which sox") as string
tell current application to do shell script quoted form of "which sox"
tell current application to do shell script (quoted form of "which sox") as text
tell current application to do shell script (quoted form of "which sox") as string
Lines that return "sh: /bin/sh which sox: No such file or directory"
do shell script quoted form of "/bin/sh which sox"
do shell script (quoted form of "/bin/sh which sox") as text
do shell script (quoted form of "/bin/sh which sox") as string
tell current application to do shell script quoted form of "/bin/sh which sox"
tell current application to do shell script (quoted form of "/bin/sh which sox") as text
tell current application to do shell script (quoted form of "/bin/sh which sox") as string
(Note: I have also tried /bin/bash, /usr/local/bin/sh, and /usr/local/bin/bash both in "quotes" and as a defined which_sox variable.)
Yes, that is a LOT of methodical trial and (all) error to simply get the path of an installed command. Please forego any answers/suggestions for methods outside of Applescript as this must run as a script from within iTunes. Also, please attempt any proposal before posting it. I see a lot of 'answers' on this site that get replied to as 'your suggestion didn't work.'
I finally found a solution. The following returns the path to an installed BASH command (sox in this case):
tell me to set sox_path to (do shell script "eval $(/usr/libexec/path_helper -s); which sox")
If you want to know why this works, read AppleScript : error "sh: lame: command not found" number 127
quoted form of is only for variables. Since you have a literal command to execute, you simply need
do shell script "which sox"

How can I use AppleScript to make Terminal.app run a shell script without evaluating the output as a script?

I'm using AppleScript to launch a quick-and-dirty shell script:
tell application "Terminal"
activate
do script "$(" & quoted form of MyScriptPath & ")"
end tell
Which properly launches a Terminal window and inputs what I would expect:
~$ $('/my script path/myscript.sh')
However, it seems that anything outputted to STDOUT (via echo) is evaluated as if it was inside the $( ) when evaluating/calling the script in the first place:
#!/bin/sh
echo "foobar"
produces:
-bash: foobar: command not found
I've searched far and wide and have not really found a suitable way to escape spaces in the path (rather than using "quoted form of") in AppleScript before sending the script location to Terminal, but I'd much prefer that. I'm using "do script" rather than "do shell script" because the script launching in Terminal is interactive and needs to be focused.
How can I echo to STDOUT when calling the script through $( )?
You don't need $(...) to run a command, only to include the output of that command in another string. You simply need
tell application "Terminal"
activate
do script "/my script path/myscript.sh"
end tell

How to change directory with spaces in applescript terminal?

I'm new to applescripts and I'm trying to automate a process, but how do you change directory through the script when there are spaces inside the directory? My commands should be correct but a syntax error keeps popping up:
Expected “"” but found unknown token.
Here is my script:
tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\ Library"
end tell
I don't understand where it is wrong. It works fine on my terminal.
Thanks a bunch guys!!
UPDATE: this worked best!!
# surround in single quotes
tell application "Terminal"
activate
do script "cd '/Users/username/Pictures/iPhoto Library'"
end tell
The are a few ways.
# escape the quotes with a backslash. AND Escape the first backslash for Applescript to accept it.
tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\\ Library"
end tell
# surround in double quotes and escape the quotes with a backslash.
tell application "Terminal"
activate
do script "cd \"/Users/username/Pictures/iPhoto Library\""
end tell
# surround in single quotes using quoted form of
tell application "Terminal"
activate
do script "cd " & quoted form of "/Users/username/Pictures/iPhoto Library"
end tell
# surround in single quotes
tell application "Terminal"
activate
do script "cd '/Users/username/Pictures/iPhoto Library'"
end tell
Also I do not thing the tild will expand when you use the quotes on the whole path.
So you will need to get the user name another way.
Examples:
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
set whoami to do shell script "/usr/bin/whoami"
tell application "Terminal"
activate
do script "cd /Users/" & quoted form of whoami & "/Pictures/iPhoto\\ Library"
end tell
tell application "System Events" to set whoami to name of current user
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
tell application "Terminal"
activate
do script "cd /Users/" & quoted form of (whoami & "/Pictures/iPhoto Library")
end tell
As you can see there is more than one way to do any of this.
Or just quote the directory part.
Example.
tell application "Terminal"
activate
do script "cd ~" & quoted form of "/Pictures/iPhoto Library"
end tell

How to pass a variable FROM applescript TO a shell script?

I have the following script
#!/bin/bash
/usr/bin/osascript << EOT
set myfile to choose file
EOT
no_ext=$(python -c "print '$myfile'.split('.')[0]")
### this works - just need to know how to pass the arg
R CMD Sweave no_ext.Rnw
pdflatex no_ext.tex
open no_ext.pdf
Can anyone point me to "how to pass the variable myfile correctly" ?
EDIT
Thx for all the suggestions!
Don't know what to accept, all of your answers really helped me since I learned a lot from everybody.
The following problems exist in your script:
A variable set in the AppleScript section does become defined in the enclosing shell script. You have to do the data exchange with the shell script by using command substitution.
AppleScripts invoked from a shell script aren't allowed to do user interaction because they do not have an application context. You can use the helper application "AppleScript Runner" to run user interaction commands.
Here is a revised version of your script where those problems are fixed:
#!/bin/bash
myfile=$(/usr/bin/osascript << EOT
tell app "AppleScript Runner"
activate
return posix path of (choose file)
end
EOT)
if [ $? -eq 0 ]
then
echo $myfile
else
echo "User canceled"
fi
First, you need to get the contents of the myfile variable from Applescript to bash. I don't know Applescript, so I'll make a shot in the dark as to how to write to its standard output. Then the python part is just unnecessary complexity (and likely wrong anyway, you were throwing away everything after the first . rather than the last). Next you need a $ before the variable name in bash syntax. I think the following script does what you want:
#!/bin/sh
set -e
myfile=$(osascript <<EOT
set myfile to choose file
write myfile to stdout
EOT
)
no_ext="${myfile%.*}"
R CMD Sweave "$no_ext.Rnw"
pdflatex "$no_ext.tex"
open "$no_ext.pdf"
(set -e at the beginning makes the shell exit immediately if an error occurs, instead of trying to execute pdflatex even though no .tex file has been produced or somesuch.)
Realize that applescript paths are colon ":" delimited. You need slash delimited in bash so in applescript terms that's the "posix path". Also, when using osascript it can't open dialog windows. You must tell an application to open the window. Next, you "return" something from the applescript... that's what goes to bash. Finally, in bash to execute a command and assign the result to a variable use `` around the command. So knowing this here's a shell script to use an applescript to get the myFile variable.
#!/bin/bash
myFile=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfile to choose file with prompt "Select the file to use in bash!"
end tell
return (posix path of myfile)
EOT`
echo $myFile

Resources