1

I created this script to highlight some data points in Safari.

This was working fine but I had to restart the script often as shorting the data in a different order remove all the highlight ...

I added some repeat statement, this is working ok but that's seems totally wrong.

Here is my Code :

set theIDFromPlist to ""
set theNameOneFromPlist to ""
set theNameTwoFromPlist to ""
set thethe_last_threeFromPlist to ""
set theGUIDFromPlist to ""
set theIPAddressFromPlist to ""
set thetheDomainNameFromPlist to ""

    set myNewList to {}
    set theList to {}

    set the plistfile_path to "~/Desktop/MY_DATA.plist"

    tell application "System Events"
        set p_list to property list file (plistfile_path)
        -- read the plist data
        try
            set theIDFromPlist to value of property list item "theID" of p_list
            set end of theList to theIDFromPlist as Unicode text
        end try
        try
            set theNameOneFromPlist to value of property list item "NameOne" of p_list
            set end of theList to theNameOneFromPlist as Unicode text
        end try
        try
            set theNameTwoFromPlist to value of property list item "NameTow" of p_list
            set end of theList to theNameTwoFromPlist as Unicode text
        end try
        try
            set thethe_last_threeFromPlist to value of property list item "the_last_three" of p_list
            set end of theList to thethe_last_threeFromPlist as Unicode text
        end try
        try
            set theGUIDFromPlist to value of property list item "GUID" of p_list
            set end of theList to theGUIDFromPlist as Unicode text
        end try
        try
            set theIPAddressFromPlist to value of property list item "IPAddress" of p_list
            set end of theList to theIPAddressFromPlist as Unicode text
        end try
        try
            set thetheDomainNameFromPlist to value of property list item "theDomainName"
            set end of theList to thetheDomainNameFromPlist as Unicode text
        end try
    end tell


    set ColortheList to {"42f489", "5cdf64", "FFFF00", "41f4d0", "4191f4", "4286f4", "4286f4", "f44192", "f44192", "f44192", "42f989", "5cdf54", "FFFF40", "41f4d5", "4191f2", "4386f5", "4246f4", "f55192", "f46192", "f24192"}

    repeat with a from 1 to length of theList
        copy ({item a of theList, item a of ColortheList}) to the end of the |myNewList|
        ## copy ({item a of theList, some item of ColortheList}) to the end of the |myNewList| ---->>>> random

    end repeat

    repeat

        tell application "Safari Technology Preview"
            ## activate
            set theWindow to front window
            tell theWindow
                tell current tab
                    repeat with colourPair in myNewList
                        do JavaScript "document.designMode = 'on'"
                        do JavaScript "var sel = window.getSelection(); sel.collapse(document.body, 0); while (window.find('" & (item 1 of colourPair) & "', true)) {document.execCommand('HiliteColor', false, '" & (item 2 of colourPair) & "');}"
                        do JavaScript "document.designMode = 'off'"
                    end repeat
                end tell

            end tell
        end tell
    end repeat

If you want to give it a try you might want to try this instead (clipboard content as datapoint :

set myNewList to {}

set myClipboard to the clipboard
set theList to words of myClipboard
##set theLength to (get length of mylist)


set ColortheList to {"42f489", "5cdf64", "FFFF00", "41f4d0", "4191f4", "4286f4", "4286f4", "f44192", "f44192", "f44192", "42f989", "5cdf54", "FFFF40", "41f4d5", "4191f2", "4386f5", "4246f4", "f55192", "f46192", "f24192"}

repeat with a from 1 to length of theList
    copy ({item a of theList, some item of ColortheList}) to the end of the |myNewList|

end repeat


Repeat
tell application "Safari Technology Preview"
    ## activate
    set theWindow to front window
    tell theWindow
        tell current tab
            repeat with colourPair in myNewList
                do JavaScript "document.designMode = 'on'"
                do JavaScript "var sel = window.getSelection(); sel.collapse(document.body, 0); while (window.find('" & (item 1 of colourPair) & "', true)) {document.execCommand('HiliteColor', false, '" & (item 2 of colourPair) & "');}"
                do JavaScript "document.designMode = 'off'"
            end repeat
        end tell

    end tell
end tell

End repeat

I know is a bit specific, but do anyone have a better approach to keep the data highlighted, if possible on whatever tab your one (current tab), whatever your change the tab, or refresh the URL ?

  • I lot will depend on how the web page is doing it's thing. Your sorting from the web page? I'd wouldn't expect your changes to last past the resort. The sort is probably re-writing the data which would leave out your changes. You can write some javascript to monitor the page then re-add your changes. – historystamp May 11 at 19:13

You must log in to answer this question.

Browse other questions tagged .