javascript - AppleScript 变量传递给 JavaScript

标签 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 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 应用程序“Google Chrome” 命令中的原因是,每次我尝试在之前的 tell 应用程序“TextEdit”中调用它时都会收到错误 命令。该错误总是说它需要行尾,但发现了 "。不太确定为什么,但我找不到解决此问题的方法。

最佳答案

您想要赋予变量的值(我的意思是文本编辑器中的文本)是否可能包含单引号(')? 在文本编辑器中没有 ' 这似乎对我有用。

以下代码片段可用于转义单引号。 (改编自this code)

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

关于javascript - AppleScript 变量传递给 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847594/

相关文章:

javascript - Angular : Set conditional min and max value for validation in number input field

macos - Applescript 更改 osx 桌面(空格)不起作用

当大量填充时,Android TextEdit 会扩展到屏幕边缘

c# - 为什么 textedit 验证处理程序被调用两次?

javascript - ReactJS - 在子级中使用父级状态的上下文

javascript - 使用 jQuery :contains 时如何忽略后代元素中的匹配项

javascript - 在 google-map-react 上动态添加标记

macos - 使用applescript发送群组iMessage

applescript - “点击”返回 "System events got an error..."-Applescript

c# - Mask 不允许在文本框中留空。我怎样才能改变这个?