0
votes

A newbie at Applescript here. I'm trying to make a script that scans text in a text editor online and replaces words with other words.

In my specific case, it's replacing a few html snippets with nothing "" — i.e. deleting them.

My current script gets the following error message… "Brave Browser got an error: window 1 doesn’t understand the “execute” message." number -1708 from window 1

Here's the script below. The line that is problematic seems to be where the EditorField variable comes in.

Any pointers or ideas out there to get this working? Thanks.

    tell application "Brave Browser"
        activate
        my replaceWordWithStringInBodyText(" ", "")
        my replaceWordWithStringInBodyText("<span class=\"Apple-converted-space\">", "")
        my replaceWordWithStringInBodyText("<b>", "<strong>")
    end tell

    on replaceWordWithStringInBodyText(searchWord, replacementString)
        tell application "Brave Browser"
            activate
            tell window 1
                set EditorField to (execute javascript "document.getElementById('\"wp-content-editor-container\"')")
                tell EditorField
                    -- start at the end and go to the beginning
                    repeat with i from the (count of paragraphs) to 1 by -1
                        tell paragraph i
                            repeat
                                try
                                    if exists searchWord then
                                        set (last word where it is searchWord) to replacementString
                                    else
                                        exit repeat
                                    end if
                                on error errorMessage
                                    exit repeat
                                end try
                            end repeat
                        end tell
                    end repeat
                end tell
            end tell
            return true
        end tell
    end replaceWordWithStringInBodyText
1
BTW, and this is not the cause of the error you're getting, however change ('\"wp-content-editor-container\"') to ('wp-content-editor-container') as there is no need to use escaped double-quotes inside of the single-quotes. Additionally, it's always a good idea to include the URL of the target page, assuming it's accessible to everyone, so we can test the code against the same as you. - user3439894

1 Answers

0
votes

I'm assuming in the Brave browser you've clicked Allow JavaScript from Apple Events, to have it checked, on the View > Developer menu.

With that said and since you have not included a URL to test your code with, let me put forth an example of how I'd click the Ask Question button, in the upper right-hand corner, if I was at this question in the Brave browser:

tell application "Brave Browser" to tell active tab of front window to execute javascript ¬
    "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"

As you can see, one must also communicate with the target tab as well as the target window. So you'll need to adjust your code accordingly.