escape shell arguments in AppleScript?

风流意气都作罢 提交于 2019-12-12 03:29:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!