How can I escape shell arguments in AppleScript?

后端 未结 3 1216
悲&欢浪女
悲&欢浪女 2021-01-01 02:32

Applescript does not seem to properly escape strings. What am I doing wrong?

Example:

set abc to \"funky-!@#\'#\\\"chars\"
display dialog abc
display         


        
3条回答
  •  既然无缘
    2021-01-01 02:51

    Backslashes aren't usually interpreted inside single quotes in shells.

    Enclosing characters in single quotation marks preserves the literal value of each character within the single quotation marks. A single quotation mark cannot occur within single quotation marks.

    A backslash cannot be used to escape a single quotation mark in a string that is set in single quotation marks. An embedded quotation mark can be created by writing, for example: 'a'\''b', which yields a'b.

    However they are interpreted by echo in sh, which is the shell used by do shell script:

    do shell script "echo " & quoted form of "\\t" --> "\t"
    

    Unsetting xpg_echo makes it behave like the echo in bash:

    do shell script "shopt -u xpg_echo; echo " & quoted form of "\\t" --> "\\t"
    

    Often it's simpler to use HEREDOC redirection instead:

    do shell script "rev <<< " & quoted form of "a\\tb" --> "b\\ta"
    

提交回复
热议问题