0

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

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.

1 Answer 1

3

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.

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
Sign up to request clarification or add additional context in comments.

3 Comments

You know, I never though about that, but that is the exact reason. Thanks! It seems there are some things in my real code that I need to change.
Do you know of a way to escape single quotes though?
I've edited the answer to include some applescript to escape single quotes. I didn't immediately find a builtin function that can do the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.