2

I'm not sure if this is possible but let's say I have a few variables :

var1 "text1'
var2 "2018"
var3 "demo@apple.com"

Can I make Applescript to found every instance of this text on Safari (any tab) and change the colour of the text? (

"document.getElementById("foundhtmlhere").style.color = "#ff0000";"

or

document.querySelector("td > a").style.backgroundColor = "red";

--

I'm sure there is a better option, perhaps using JavaScript String search() / selector but I'm still have to go through this. I guess I can search within the body using this, then maybe using text delimiter ? I'm a bit lost

 tell application "Safari"
    tell document 1
        set body to do JavaScript "document.body.innerHTML"
        set body to do JavaScript "document.getElementsByTagName('body').item(0).innerHTML"
    end tell
end tell
return body
1

You can use the window.find('someString', false or true) command to search some string in the text of the document.

The second parameter of the command, if true, specifies a case-sensitive search.

Use the document.execCommand('HiliteColor', false, someColor) command to change the background color of the founded text.


Here's the sample script, tested on Sierra.

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

tell application "Safari"
    -- to change the color,  set the 'document.designMode' to on
    -- get the scroll position of the document, because the '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();
                sel.collapse(document.body, 0);//------    To start at the beginning of the document, not after the selectioned text
                while (window.find('" & thisText & "', true)) {document.execCommand('HiliteColor', false, '#ff0000');}
                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
0

If you mean on a normal page you visit, as you would doing a find (cmd-f) that highlights the text, with AppleScript the answer is no (the AppleScript dictionary for Safari does not give you that option).

If, on the other hand, you mean to find text in an html page that you are editing to then upload, then yes, you could change the color automatically with your html editor, but I think that is not what you are describing.

  • I'm pretty sure you can use Javascript to search the HML content of the page and once the variable is found , change the colour of the text. a bit like Multi Highlight for chrome chrome.google.com/webstore/detail/multi-highlight/… – Kevin May 16 '18 at 20:15
  • Instead of voting it down, why don't you show us how to do it with Applescript, as the original question requested? The Applescript dictionary does not give you the option to retrieve or or set colors in Safari. If I'm wrong, please show me where the dictionary shows that, because as far as I can tell, that is necessary for Safari to change a text color via AppleScript. Of course, you can search the content of the page using AppleScript, as you can with Javascript. But changing the color of a page in Safari that you are not editing, is another thing. – MBUST May 17 '18 at 7:17
  • My last comment on this (I'm giving up my points to set up a bounty, so I won't be able to comment for a while): perhaps this is doable in Chrome; I have not checked (though I doubt it). But I was answering your question about Safari using AppleScript and assuming you are talking about a page that you are reading, not editing. Perhaps those assumptions are wrong. If not, voting it down makes not sense to me, IMO. – MBUST May 17 '18 at 7:23
  • MBUST, I don't know why I voted down, I guess it's accidental, anyway, shorted, also I might have find my answer here stackoverflow.com/questions/13799154/… UPDATE I can't remove my vote now I'll change it a few hours – Kevin May 17 '18 at 9:26
  • You last voted on this answer 19 hours ago. Your vote is now locked in unless this answer is edited. – Kevin May 17 '18 at 15:44

You must log in to answer this question.

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