-->

AppleScript的变量传递给JavaScript(AppleScript variables

2019-07-30 19:54发布

我存储从TextEdit文本在一个AppleScript变量,我想将它传递给JavaScript。 我想我这样做是正确的,但我始终无法存储的值。 代码如下:

tell application "TextEdit"
    activate
    set docText to the text of the front document --This works. I've checked.
end tell

tell application "Google Chrome"
    tell window 1
        tell active tab
            execute javascript "function encryptDocument() {
                plainText = '" & docText & "';
                return plainText;
                } encryptDocument();"
                set skurp to the result
                display dialog skurp
            end tell
    end tell
end tell

我一个房子内的JavaScript代码的原因tell application "Google Chrome"命令是因为我得到每次我试图调用它前一次错误tell application "TextEdit"命令。 错误总是说,它预计线的一端,但发现" 。不知道为什么,但我不能找到解决这个问题的方法。

Answer 1:

有没有可能是你想给变量(我的意思是在文本编辑器中的文本)的值中包含一个单引号(')? 如果没有“在文本编辑器,这似乎为我工作。

下面的代码片段可以被用来逃避单引号。 (改编自该代码 )

on escape(this_text)
    set AppleScript's text item delimiters to the "'"
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the "\\'"
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end escape

tell application "TextEdit"
    activate
    set docText to the text of the front document --This works. I've checked.
end tell

set docText to escape(docText)

tell application "Google Chrome"
    tell window 1
        tell active tab
            execute javascript "function encryptDocument() {
                plainText = '" & docText & "';
                return plainText;
                } encryptDocument();"
            set skurp to the result
            display dialog skurp
        end tell
    end tell
end tell


文章来源: AppleScript variables passed to JavaScript