0在AppleScript中传递引号“do shell script”... AgainAgain

时间:2013-10-25 00:03:50

标签: shell applescript quotation-marks

我知道已多次提出此问题,例如herehere

但是使用\“和quote似乎都不适用于我。例如,使用

set msgDate to "05-06-2013"
set quotedmsgDate to "\"" & msgDate & "\"" as string
do shell script "echo " & quoted form of quotedmsgDate

返回

"\"05-06-2013\""

我不明白为什么,如果你能开导我会很棒!如果重要的话,我正在使用10.8.5和德语本地化......

编辑:

这是我试图开始工作的剧本

tell application "BibDesk (original)"
    set thePublications to selection of document 1
    repeat with thePub in thePublications
        tell thePub
            if value of field "Pages" is not "" then
                set the_pages to value of field "Pages"
                set quoted_pages to "\"" & the_pages & "\"" as string
                set the_script to "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p " & quoted form of quoted_pages
                set a to do shell script the_script
                set value of field "Pages" to a
            end if
        end tell
    end repeat
end tell

我收到错误,因为the_script未正确设置,导致

do shell script "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p '\"151-65\"'"

无法正确执行......

1 个答案:

答案 0 :(得分:1)

我认为,AppleScript编辑器中的“结果”窗格正在向您显示一个变量,就像在AppleScript中使用它一样。

因此,当结果在05-06-2013被引号括起时,它会将其解释为字符串。因为,如果你将一个带引号的字符串分配给一个变量,你就必须反斜杠这些引号,它也是这样做的。

通过在脚本中添加一些显示对话框,您可以更好地了解正在发生的事情:

set msgDate to "05-06-2013"
set quotedmsgDate to "\"" & msgDate & "\"" as string
display dialog quotedmsgDate
set echoScript to "echo " & quoted form of quotedmsgDate
display dialog echoScript
do shell script echoScript
display dialog the result

当显示为普通文本时,您会看到日期被引号括起来。

对于新表单,这可能对测试很有用:

set the_pages to "151-65"
--set quoted_pages to "\"" & the_pages & "\"" as string
set quoted_pages to the_pages as string
set the_script to "/usr/bin/python /Users/Januz/Downloads/sanitize_BibDesk_pages.py -p " & quoted form of quoted_pages
display dialog the_script
set a to do shell script the_script
display dialog a