AppleScript的变量传递给JavaScript [英] AppleScript variables passed to JavaScript

查看:180
本文介绍了AppleScript的变量传递给JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储从文本编辑文本的AppleScript变量,我想将它传递给JavaScript。我想我这样做是正确的,但我依然无法被存储的数据。在code是如下:

I am storing text from TextEdit in an AppleScript variable, and I want to pass it to JavaScript. I thought I was doing it right, but I still can't get the value to be stored. The code is as follows:

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的code previous 告诉应用程序文字编辑命令。错误总是说,它预计线的一端,但发现。真的不知道为什么,但我不能找到解决这个问题的方法。

The reason I house the JavaScript code within a tell application "Google Chrome" command is because I get an error every time I try to call it in the previous tell application "TextEdit" command. The error always says that it expects an end of line but found ". Not really sure why, but I can't find a way around this problem.

推荐答案

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

Is it possible that the value you want to give to the variable (I mean the text in Text Editor) contains a single quote (')? Without ' in Text Editor this seems to work for me.

以下code片段可以用来逃跑单引号。 (改编自这个code

The following code snippet may be used to escape single quotes. (adapted from 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

这篇关于AppleScript的变量传递给JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆