问题
I have the following command line works well in terminal
rename 's/\d+/sprintf("%04d",$&)/e' ~/Downloads/test/*.pdf
but I couldn't escape quotes (have single quote and backslash in the script) in applescript, this is what I tried, applescript didn't give me error but I didn't get my files renamed.
set renamer_command to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
do shell script "/opt/local/bin/rename " & quoted form of renamer_command & " ~/Downloads/test/*.pdf"
This is the output I get from Applescript
tell current application
do shell script "/opt/local/bin/rename ''\\''s/\\d+/sprintf(\"%04d\",$&)/e'\\''' ~/Downloads/test/*.pdf"
--> ""
end tell
Result:
""
This is gives exactly what I want as seen in the output example.txt
set inString to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
do shell script "echo " & quoted form of inString & " > ~/Desktop/example.txt"
result
's/\d+/sprintf("%04d",$&)/e'
Again if I drop the output in my script and just look at the result in applescript then I get backslash in the result
set inString to "'s/\\d+/sprintf(\"%04d\",$&)/e'"
do shell script "echo " & quoted form of inString
result
"'s/\\d+/sprintf(\"%04d\",$&)/e'"
I worked around a lot but I couldn't get working script, please suggest me a solution, thanks
回答1:
You have too many quotes. Try this:
set inString to "s/\\d+/sprintf(\"%04d\",$&)/e"
do shell script "/opt/local/bin/rename " & quoted form of inString & " ~/Downloads/test/*.pdf"
来源:https://stackoverflow.com/questions/18827739/escape-shell-arguments-in-applescript