2

I can't figure out how to adapt this script, which highlights text in Safari, to get it to work with Google Chrome:

set myList to {"AppleScript", "2018", "demo@apple.com"}

tell application "Safari"
    'window.find()' command change the scroll position when it select the founded string
    set scrollPos to do JavaScript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]" in document 1
    repeat with thisText in myList
        do JavaScript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()" in document 1
    end repeat

    -- restore the scroll position
    do JavaScript "document.designMode = 'off';  window.scrollTo(" & (item 1 of scrollPos) & ", " & (item 2 of scrollPos) & ")" in document 1
end tell

Here is my Google Chrome version :

set myList to {"AppleScript", "2018", "CLOSED"}

tell application "Google Chrome"
    tell tab 3 of window 1 to set scrollPos to execute javascript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]"

    repeat with thisText in myList
        execute javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
    end repeat
    execute javascript "document.designMode = 'off';  window.scrollTo(" & (item 1 of scrollPos) & ", " & (item 2 of scrollPos) & ")"
end tell

Reply :

tell application "Google Chrome"
    execute tab 3 of window 1 javascript "document.designMode = 'on'; [window.pageXOffset.toString(),window.pageYOffset.toString()]"
        --> {"0", "0"}
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('AppleScript', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --> missing value
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('2018', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --> missing value
    execute current application javascript "var sel = window.getSelection();\n                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text\n                while (window.find('CLOSED', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}\n                sel.collapseToEnd()"
        --> missing value
    execute current application javascript "document.designMode = 'off';  window.scrollTo(0, 0)"
        --> missing value
end tell
Result:
missing value
  • For what it's worth, I can't get that Safari one to work either because of the ‘unknown token’ at the start of the 'window.find()' command line. Was that line meant to be a comment? Where did you get that script from? – grg Jun 10 '18 at 10:41
  • Just curious, what is the purpose with this script? – d-b Jun 10 '18 at 18:47
  • Hard to explain, but basically I get variable such name, IPs, and other data, and I look for this informations across different tabs, so highlighting them it's really helpfully to spot them quickly – Kevin Jun 11 '18 at 6:24
5
+50

Make sure you've enabled JavaScript from AppleScript in Google Chrome

Executing JavaScript through AppleScript is turned off. To turn it on, from the menu bar, go to View > Developer > Allow JavaScript from Apple Events.

I've made a few changes to your script for it to work, with the changes highlighted in bold.

  • The scrollPos variable and its associated handling of scrolling by getting and setting window position was superfluous, so all references to it have been removed.
  • All execute verbs need to tell window 1, not just one verb. I've added to tell window 1 to the enclosing tell of the application.
  • The execute verb takes a reference and a JavaScript string, not just JavaScript. You are missing the reference on all your execute verbs.
set myList to {"AppleScript", "2018", "CLOSED"}

tell application "Google Chrome" to tell window 1
    execute active tab javascript "document.designMode = 'on';" -- scrollPos and page offset removed

    repeat with thisText in myList
        execute active tab javascript "var sel = window.getSelection();
                    sel.collapse(document.body, 0);
                    while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}
                    sel.collapseToEnd()"
    end repeat
    execute active tab javascript "document.designMode = 'off';" -- scrollPos and page offset removed
end tell

To run it on all tabs, wrap it with repeat with atab in tabs:

tell application "Google Chrome" to tell window 1
    repeat with atab in tabs
        execute atab javascript "document.designMode = 'on';" -- scrollPos and page offset removed

        repeat with thisText in myList
            execute atab javascript "var sel = window.getSelection();
                    sel.collapse(document.body, 0);
                    while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#5cdf64');}
                    sel.collapseToEnd()"
        end repeat
        execute atab javascript "document.designMode = 'off';" -- scrollPos and page offset removed
    end repeat
end tell
  • That's perfect thank you ! do you know if I can run this highlight to every tab? I might click on different links within tabs or possibly open new one , should I run a repeat mode ? – Kevin Jun 10 '18 at 11:18
  • @Kevin repeat with atab in tabs, see edit – grg Jun 10 '18 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .