0

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"エラーは常に、行末​​が必要であるが見つかったことを示しています"。理由はよくわかりませんが、この問題を回避する方法が見つかりません。

4

1 に答える 1

3

変数に与えたい値 (つまり、テキスト エディターのテキスト) に一重引用符 (') が含まれている可能性はありますか? テキストエディタで ' がなければ、これはうまくいくようです。

次のコード スニペットは、一重引用符をエスケープするために使用できます。(このコードから適応)

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
于 2012-08-07T14:10:10.180 に答える