How can I escape shell arguments in AppleScript?

后端 未结 3 1202
悲&欢浪女
悲&欢浪女 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"
    
    0 讨论(0)
  • 2021-01-01 02:55

    Use "quoted form of". In general in applescript we are dealing with a "mac" style path so we would do something like this to pass it to the shell...

    set theFile to choose file
    set dirname to do shell script "dirname " & quoted form of POSIX path of theFile
    
    0 讨论(0)
  • 2021-01-01 03:01

    No, there is no extra ' added in 'funky-!@#'\''#"chars'.

    As already indicated by 17510427541297, AppleScript's quoted form of idiom is meant for use in Unix shells, and strings in Unix shells get concatenated if they are placed directly next to each other.

    AppleScript's quoted form of abc just does this: it creates a string enclosed by single quotes, but replaces every single quote ' withing that string with '\''.

    This, in fact, creates three separate strings, but the three separate strings are subject to the following string concetanation mechanism in (most) Unix shells:

    "funky-!@#'#\"chars" becomes 'funky-!@#' + \' + '#"chars'

    The resulting string is fit to get interpreted by Unix shells as a single literal string (without causing parameter expansion issues and the like).

    # in Terminal.app
    # note the escaping in: osascript -e '...'\''...'
    quotedsrt="$(osascript -e '
    set abc to "funky-!@#'\''#\"chars"
    return quoted form of abc
    ')"
    
    echo "$quotedsrt"                 # 'funky-!@#'\''#"chars'
    eval echo "$quotedsrt"            # funky-!@#'#"chars
    echo echo "$quotedsrt" | sh
    
    
    # escaping mechanism for Bash shell
    set +H
    esc="'\''"
    str="funky-!@#'#\"chars"
    str="'${str//\'/${esc}}'"
    set -H
    
    echo "$str"                  # 'funky-!@#'\''#"chars'
    eval echo "$str"             # funky-!@#'#"chars
    echo echo "$str" | sh
    
    0 讨论(0)
提交回复
热议问题