传递给JavaScript的AppleScript变量
我将TextEdit中的文本存储在AppleScript变量中,并希望将其传递给JavaScript。我认为我做得对,但我仍然无法获得要存储的值。代码如下:传递给JavaScript的AppleScript变量,javascript,applescript,textedit,Javascript,Applescript,Textedit,我将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
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代码放在
告诉应用程序“Google Chrome”
命令中,是因为我每次试图在前面的告诉应用程序“TextEdit”
命令中调用它时都会出错。错误总是说它需要一个行尾,但找到了“”。不确定原因,但我找不到解决此问题的方法。是否可能要给变量的值(我是指文本编辑器中的文本)包含一个单引号(')?
没有“在文本编辑器中”,这似乎适合我
以下代码段可用于转义单引号。(改编自)
您想给变量的值(我是指文本编辑器中的文本)是否可能包含一个单引号(')? 没有“在文本编辑器中”,这似乎适合我 以下代码段可用于转义单引号。(改编自)
你知道,我从来没有想过,但这就是确切的原因。谢谢!我的真实代码中似乎有一些东西需要更改。你知道一种转义单引号的方法吗?我已经编辑了答案,包括一些转义单引号的applescript。我没有立即找到一个可以这样做的内置函数。你知道,我从来没有想过,但这就是确切的原因。谢谢!我的真实代码中似乎有一些东西需要更改。但是你知道一种转义单引号的方法吗?我已经编辑了答案,包括一些转义单引号的applescript。我没有立即找到一个可以这样做的内置函数。
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