How would I receive input using "Ask For Text" in MacOS Automator and then pass that data into part of a shell script?
i.e.
[Ask for text/folder path]
[set inputted text to variable "data"]
[run shell script:
chflags hidden (CALL "DATA" HERE)]
I would like the above example to yield the specified folder hidden.
The Run Shell Script action has a "Pass input" pop-up menu in its upper right that lets you select how you want the input from the previous action to be passed to the script. The options are "to stdin" or "as arguments".
For your case, "as arguments" is probably easiest to handle.
Then, you can write your command as:
chflags hidden "$#"
Related
There is an excellent answer for the reverse, pass several variables from AppleScript to Shell Script but I can't find a comprehensive answer for the opposite when there are two or more variables/arguments and or a bash function.
In Automator I am trying to pass variables like so: Run AppleScript > Run Shell Script > Run AppleScript.
Run AppleScript: which passes a URL as an argument
Run Shell Script: which uses "$#" for that argument
/bin/bash serial=$(($RANDOM % 10000)) /usr/local/bin/ffmpeg -i "$#" -c copy bsf:a aac_adtstoasc "/Path/to/file/movie_$serial.mp4" 2>&1 $! exit 0
Run AppleScript: This is where I need to pick up stdout, and the PID of the last executed process ffmpeg from Run Shell Script above. I can't seem to get anything. I have tried adding an automator "Storage Variable" but it's not receiving.
Using AppleScript's Do Shell Script command I couldn't get serial=$(($RANDOM % 10000)) to actually put a serial number in the file name movie_$serial.mp4. The file name was literally output as "movie_$serial.mp4", instead of "movie_1234.mp4".
serial=$(($RANDOM % 10000)) works perfectly in Terminal and in Run Shell Script. Not sure what I am missing to make it work with "Do Shell Script".
do shell script "/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i " & link_ & ffmpegOpt & "'" & sPath & "$serial.mp4" & "'"
Which returns the following for the "do shell script" call:
"/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i urlofmovie -c copy -bsf:a aac_adtstoasc '/Path/to/file/movie_$serial.mp4'"
When using ffmpeg the path on the command line the save path has to be in quotes.
If I read your OP correctly, you actually have two different issue here.
Not knowing how to provide input to a Run AppleScript action from a Run Shell Script action.
Variable parameter expansion is not occurring for you with: $serial
Issue 1:
To return something from a Run Shell Script action to another action. e.g. a Run AppleScript action, set the last line of the Run Shell Script action to, e.g.:
echo "foobar"
Or:
printf "foobar"
For multiple items use, e.g.:
echo "foobar
barfoo"
Or:
printf "foobar\nbarfoo"
Issue 2:
I am not in the position to replicate your do shell script command at the moment; however, the reason variable parameter expansion is not occurring is because the variable has single-quotes around it.
... '/Path/to/file/movie_$serial.mp4'"
Expansion will not take place when a variable has single-quotes around it, so you need to formulate your command so it can be expanded. Or in a separate step, process what's necessary to to accomplish the goal.
For example:
set sPath to "/path/to/file/movie_"
set serial to ((random number from 0 to 32727) mod 10000) as string
set pathFilename to sPath & serial & ".mp4"
Then you can use, e.g.:
... & pathFilename's quoted form
In your do shell script command while adjusting the entire command to work for you.
In other words, you can get rid of, e.g.:
/bin/bash serial=$(($RANDOM % 10000));
And:
& "'" & sPath & "$serial.mp4" & "'"
When running a shell script from Script Editor and wanting to return more than one argument as input; and assign those arguments to variables in your Apple Script:
One method I discovered:
Example shell script:
SHELL_VAR1=$(date)
SHELL_VAR2=$(whoami)
echo "$SHELL_VAR1","$SHELL_VAR2"
The echo command at the end, with a comma for delimiter, will output to Apple Script in this format:
{"January 21, 2022", "john"}
In the Apple Script:
set input to (do shell script "script.sh")
set the text item delimiters to ","
set {var1, var2} to {text item 1, text item 2} of the input
{var1, var2}
If there is another, simpler, method I would love to learn it.
Is there a special notation for multiple arguments that Apple Script can use for input?
i.e. $1 $2 or something similar
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"
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
Is it possible to pass an Automator variable into a shell script?
To complement #Ned Deily's answer:
(Written as of OS X 10.9.2, still current as of OSX 10.10)
It is often not necessary to create and use explicit variables in Automator (using the Set Value of Variable and Get Value of Variable actions).
The previous action's output is automatically passed to a Run Shell Script action.
By default, the data is passed via stdin, however.
If you want it passed as arguments ($1, $2, ... - also accessible as an array via $#) instead, select as arguments from the Pass input: list on the right, as illustrated here:
In this example, the selected Finder items are passed as POSIX-style paths to the shell script.
That said, having a shell script process the data via stdin (using read -r in a loop) works, too:
Drag & drop
Store path in filename variable
Prompt window for input value in next variable
get filename variable
Store prompt value in rename_to
$1 – rename_to value, $2 – filename value
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