Using double quotes in Applescript command Do Shell Script Echo

谁都会走 提交于 2021-02-10 05:05:58

问题


I'm trying to use applescript to run a command line process. A simplified version of the Applescript looks like this

do shell script "echo bwfmetaedit --INAM=\"name\" --IART=\"artist\" --ICRD=\"date\" /desktop/filepath.wav"

with the expected result being

bwfmetaedit --INAM="name" --IART="artist" --ICRD="date" /desktop/filepath.wav

If I were to just to run that command in Terminal, I get the correct output. However with applescript I get the following result. Note the missing double quotes around the values.

"bwfmetaedit --INAM=name --IART=artist --ICRD=date /desktop/filepath.wav"

What am I missing here? I need the double quotes around the values or else the command wont run properly.

Thanks, Morgan


回答1:


Try:

do shell script "echo bwfmetaedit --INAM=\\\"name\\\" --IART=\\\"artist\\\" --ICRD=\\\"date\\\" /desktop/filepath.wav"



回答2:


The quotes are being passed properly, it’s just that the shell doesn’t echo them because they are part of the shell syntax.

If you try this AppleScript that prints each argument on its own line:

do shell script "sh -c 'for F in \"${@}\"; do echo \"${F}\"; done' \"${0}\" echo bwfmetaedit --INAM=\"name with spaces\" --IART=\"artist with spaces\" --ICRD=\"date with spaces\" /desktop/filepath.wav"

Then you will see that the output is:

"echo
bwfmetaedit
--INAM=name with spaces
--IART=artist with spaces
--ICRD=date with spaces
/desktop/filepath.wav"

Each argument passed to echo is parsed properly as if it were quoted. The quotation marks are at the beginning and end because it is an AppleScript string with embedded newlines.




回答3:


For me my challenge was

osascript -e 'tell application "Simulator" to quit'

So my solution was

do shell script osascript -e 'tell application \"Simulator\" to quit'"

You have to escape before the first double-quote and then again before it. Thanks for tips everyone!




回答4:


To call this inside code, try this

set myStr to "/bin/echo Hello here is \\\"Quoted String\\\"

The do script call will write it with double quote(s).

do script myStr in front window



来源:https://stackoverflow.com/questions/14737414/using-double-quotes-in-applescript-command-do-shell-script-echo

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