Applescript shell script to folder with space - shell

I am trying to write a text file to a folder that contains space in the name. eg: folder 2
The first script that writes to folder works correctly
The second script that writes to folder 2 does not work correctly.
Edit:
I tried to use single quotes around the path and it works, if I hardcode the path. Is there a way to write to the folder without losing the ~ symbol?
--this works
do shell script "echo " & "the text" & " >> ~/Desktop/folder/text.txt"
--this does not work
do shell script "echo " & "the text" & " >> ~/Desktop/folder 2/text.txt"

This will work:
do shell script "echo " & "the text" & " >> ~/Desktop/folder\\ 2/text.txt"
Note: When using ~, which is the same as $HOME, in a do shell script and there is a space in the pathname, use a double backslash \\ to escape the space and do not quote the pathname containing a ~. In bash the space would only need a single backslash \ to escape the space.
This works too:
do shell script "echo " & "the text" & " >> \"$HOME/Desktop/folder 2/text.txt\""

Related

How to pass several variables from Shell Script to AppleScript in Automator?

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

How to deal with white spaces in osascript [duplicate]

This question already has answers here:
Combining variables in Bash to form a command sent to AppleScript using the osascript command
(2 answers)
Closed 5 years ago.
I'm in need of your assistance on how to deal with white space in a path.
In my example below, I'm setting some variables in a Terminal bash shell and executing, "osascript" from command line, utilizing these variables to run ['do shell script'] & ['display dialog']. When I run these from command line, I'm getting different results. My ultimate goal here is to be able to execute ['do shell script'] and it execute the bash shell script based on the path. In this case, the path has spaces in it.
NOTE: The INSTALLER_PATH variable defined below is set like this because the path is generated from an Apple Script I wrote that basically takes a path [ with white spaces in it ] and combines that path with another variable. Because this path has spaces in the name, I'm using the [ to quoted form of ] setting that puts the path in quotes.
APPLE SCRIPT EXAMPLE:
set pathToApp to POSIX path of ((path to me) as text)
set dragonFrame to ("_DRAGONFRAME/")
set INSTALLER_PATH to quoted form of pathToApp & dragonFrame
display dialog INSTALLER_PATH
GENERATES THIS PATH {notice the ticks}:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
Instead of testing this through the Apple Script to find the exact syntax I would need, I figured it would be easier to test this from command line. This is exactly what I'm setting and executing from my Terminal command line. In the examples below, executing [ 'display dialog' ] works and [ 'do shell script' ] fails. I think if there's a way to enclose the entire path in double quotation marks, my problem would be solved although all attempts at getting the path enclosed in double quotation marks has failed:
%> INSTALLER_PATH='/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
%> ADMIN_USER_PROC="_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh"
%> osascript -e 'do shell script ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS ERROR:
0:217: execution error: sh: /Volumes/Free.Space/Shotgun: No such file or directory (127)
%> osascript -e 'display dialog ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS PATH:
/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh
Thank you in advance for any help you can offer.
I think you're looking for something similar to this syntax:
set dragonFrame to ("/_DRAGONFRAME/")
set INSTALLER_PATH to path of (pathToApp & dragonFrame)
Result:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/'
If you need double-quotes around the path you can use:
("\"" & pathToApp & dragonFrame & "\"")
and get rid of the quoted form argument leaving you with:
set INSTALLER_PATH to ("\"" & pathToApp & dragonFrame & "\"")

How do I pass more than two variables in applescript do shell script?

I tried this:
do shell script "curl -A " & quoted form of useragent & " quoted form of urlst"
However, this thinks the quoted form of urlst is shell scripting.
I also tried this:
do shell script "curl -A " & quoted form of useragent & quoted form of urlst
But now it just says that there was no URL specified, so it can't read the variable.
How do I fix this?
Short answer what you should do is:
do shell script "curl -A " & quoted form of useragent & space & quoted form of urlst
The shell has an unique way of handling text. Normally bash reads data until it meets a separator (default: space, tab or return). The quote is a switch that turns on or off how the remaining data should be interpreted. When it reads a quote it switches its substitution mode on or off. When it's on (by default) special characters keep their meaning and when it is off special characters has no meaning at all. So 'hello world!' and hello' 'world! is the same string in bash because only the space is a special character where the first quote turns substitution off and the second quote turns it back on. Before reaching the end of the line the substitution mode needs to be turned on again so it can find the end of the line. On your case the string 'safari' directly followed by 'http://www.stackoverflow.com' will be considered as one complete string. So when passing multiple arguments to bash you need make sure it is separated by a space when the substitution mode is on.

Double Quotes in batch string redux

I'm trying to understand how the ^ Caret escape works in a Batch CMD. Lets say I type this line in a CMD Window, I would expect it to send the DIR listing to "c:\my text.txt"
CMD.EXE "/C > ^"c:\my text.txt^" DIR C:\*.*"
Instead I get the error:
'txt.txt" DIR *.*"' is not recognized as an internal or external command,
operable program or batch file.
If I do something as simple as this:
ECHO "^"^""
Expected: """"
Actual : "^"""
Even if I try double quoting like this:
ECHO " "" "" "
Expected: " " " "
Actual : " "" "" "
Can someone 'splain how this works and what is a reliable way is to escape double quotes in a command line?
Thanks.
Additional Example:
Why does this work:
cmd.exe "/C sqlcmd.exe -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
but this gives error "The system cannot find the path specified." and does not create the txt file.
cmd.exe "/C "sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
I know it is not needed in this case, but how would I enclose the command sqlcmd.exe in quotes?
I also tried removing the quotes from the whole line with the same results, i.e.,
cmd.exe /C "sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt"
Thanks again.
Quotes are a state machine within cmd.exe. The first quote turns quoting semantics on, the next quote turns it off, the next back on, etc.
It is possible to escape a quote such that it does not turn quote semantics on.
For example, the following will send a quote character to test.txt
echo ^" >test.txt
Without the escape, the string " >test.txt is simply printed to the screen.
Unfortunately, it is not possible to escape a quote that turns off quote semantics.
For example, suppose you want to print the string "hello world" >"test.txt" to the screen. None of the techniques you tried will work because an internal quote cannot be escaped. The ^" form treats the ^ as a literal, and the output will still be redirected to a file. The "" for properly quotes the >, so there is no redirection, but now you have two quotes instead of one.
You have two options.
1) escape all "poison" characters that are not quoted
echo "hello world" ^>"test.txt"
2) hide the quote literal within a delayed expansion variable
I'm assuming this code is in a batch script.
setlocal enableDelayedExpansion
set "quote=""
echo "hello world!quote! >!quote!test.txt"
Note that using this technique can become tricky if dealing with FOR /F ... IN('command') or CMD /C because delayed expansion will probably be off within the new CMD.EXE context.
Here is a way to do your original command
cmd /c dir c:\*.* ^>"c:\my text.txt"
Note that the entire command string after /c does not have to be enclosed within quotes. In this case, it is easier if it is not.
I believe your new command at the end can be written as follows:
cmd.exe /C ""sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
If used within a parenthesized block, then the right paren will need to be escaped.
(
cmd.exe /C ""sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name(^)" > "c:\temp\test 1.txt""
)

applescript with shell script having % "

I have a long apple script that does conversion of files, moving files, compression etc.
I have to include a renaming script in-between the script that does rename files e.g. file1.pdf to file0001, file2 to file0002 and so on.
I found the following script works in terminal, but how can I include this in-between my applescript since it has percentage %, quotes " inside of the command.
works in terminal
rename 's/\d+/sprintf("%04d",$&)/e' ~/Downloads/test/*.pdf
I can't do shell script like this one
do shell script "rename 's/\d+/sprintf("%04d",$&)/e' " & theFolder & "*.pdf"
this will end up with error since it has percentage %, quotes ".
How can I implement this into my applescript, thanks.
do shell script quoted form of "rename 's/\\d+/sprintf(\"%04d\",$&)/e' ~/Downloads/test/*.pdf"

Resources