2012-08-07 62 views
0

我将TextEdit中的文本存储在AppleScript变量中,我想将它传递给JavaScript。我认为我做得对,但我仍然无法获得存储的价值。代码如下:传递给JavaScript的AppleScript变量

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 

我容纳tell application "Google Chrome"命令中的JavaScript代码的原因是因为我得到每次我尝试调用它以前的tell application "TextEdit"命令时出错。该错误总是表示它期望行结束,但发现"。不知道为什么,但我无法找到解决这个问题的方法。

回答

3

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

以下代码片段可能用于转义单引号。 (改编自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 
+0

你知道,我虽然关于从来没有,但是这是确切的原因。谢谢!看来我需要改变我的真实代码中的一些东西。 – Josh 2012-08-07 14:13:30

+0

你知道一种逃避单引号的方法吗? – Josh 2012-08-07 15:26:52

+0

我编辑过的答案包括一些苹果脚本来逃避单引号。我没有立即发现可以做同样的内建函数。 – Vortexfive 2012-08-07 18:39:47