Saturday, March 23, 2019
  • Index
  •  » Code Exchange
  •  » Safari CookieCleaner - full featured white/junk list based cleaner

#1 2018-06-03 09:26:48 pm

Ron_Pinkas
Member
Registered: 2018-06-04
Posts: 2

Safari CookieCleaner - full featured white/junk list based cleaner

Safari-CookieCleaner is a full featured applet allowing automated quick and safe cleanup of Safari's Cookie/Cache/Local Storage/Databases.

The optional Whitelist allows intuitive definition of domains to be protected, supporting patterns:
   starts with
   ends with
   contains
   exact

The optional Junk list (intersected against the Whitelist) allows quick and safe definition of common/frequent privacy threats. Whitelisted domains will be protected even if matched. If the first item of the Junk list is "*" then all EXCEPT the Whitelisted domains will be deleted, but ONLY if a Whitelist is indeed defined.

The applet will make sure to first find, and move to Trash, respective Databases (of matched non whitelisted junk), so as to circumvent the common difficulty of removal of cookies from domains with database.

The applet is a small single script which can be run through Script Editor or saved as an application and run like any other app.

The only required setup is to enable Assistive Accessibility:
(System Preferences->Security & Privacy->Privacy->Accessibility)
to Script Editor (if not enabled yet) and/or to the generated application file, if desired to be run as a stand alone.

The script is maintained and available at: https://github.com/ronpinkas/Safari-CookieCleaner

Your input is very welcome.

Applescript:


(*
Author: Ron Pinkas

You are free to use this code in any way permitable by the GNU General Public License V3.0
*)


on main()
   set debugLog to false
   
   # Item starting with "*" will match domain names ENDING with its subssequent text
   # Item endiding with "*" will match domain names BEGINING with its preceding text
   # Items starting and ending with "*" will match domain names CONTAINING its enclosed text    
   # Otherwise NON GRIDY so use full name INCLUDING .com etc.!
   set whiteList to {"amazon.com", ¬
       "ebay.com", ¬
       "facebook.com", "facebook.net", ¬
       "github.com", "*.githubusercontent.com", ¬
       "google.com", "google-analytics.com", "*.googleapis.com", "googleusercontent.com", "gstatic.com", ¬
       "instagram.com", "cdninstagram.com", ¬
       "netflix.com", ¬
       "paypal.com", ¬
       "twitter.com", "twimg.com", ¬
       "visualstudio.com", ¬
       "whatsapp.*", ¬
       "youtube.com"}
   
   # Items in this list will be fed AS-IS to Safari's cookie search field, which is GRIDY, i.e
   # it uses CONTAINS logic to search for matches - so be careful or better yet
   # protect valuable domains using the above whiteList!
   # IF FIRST Element is "*" then the junkList will be ignored and ALL EXCEPT whiteList will be deleted!
   set junkList to {"*", ".co.", "addthis", "adobe", "affirm", "akc", "allure", "akamai", "azureedge", ¬
       "barron", "beaver", "bam-x", "bing", "bkrtx", "blue", "bold", "boot", "bounce", "btt", "b-cdn", ¬
       "cdn", "chartbeat", "chimp", "click", "cloudflare", "cloudfront", "cnbc", "cohesion", "cookie", "count", "coust", "crazy", "createjs", "criteo", "crsspxl", "crwd", ¬
       "dazz", "demdex", "dental", "desk", "digitech", "disqus", "docashop", ¬
       "exelator", "expose", "ggpht", "facebook", "fbcdn", "google-analytics", "googleadservices", "googletag", "googleusercontent", "gravatar", "gstatic", ¬
       "home", "imgur", "jeeng", "link", "local", "mac", "networks", "newrelic", "omny", "opentok", "optimizer", "parsely", "petametric", "pir.fm", "porn", "quantserve", ¬
       "reddit", "resources", "score", "sekindo", "sstatic", "stack", "taboola", "track", "truspilot", "twimg", "user", "visistat", "ytimg"}
   
   set homePath to (path to home folder)
   set databasesPath to alias ((homePath as text) & "Library:Safari:Databases")
   set indexedDBPath to alias ((databasesPath as text) & "___IndexedDB")
   
   tell application "Safari" to activate
   
   tell application "System Events"
       tell process "Safari"
           set frontmost to true
           
           set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
           click menu item "Preferences…" of myMenu
           # WAIT till opened.
           repeat until (exists button "Privacy" of toolbar 1) of window 1
               delay 0.01
           end repeat
           
           set windowPreferences to window 1
           
           tell windowPreferences
               click button "Privacy" of toolbar 1
               # WAIT till opened.
               repeat until (exists button "Manage Website Data…" of group 1 of group 1)
                   delay 0.01
               end repeat
               
               click button "Manage Website Data…" of group 1 of group 1
               # WAIT till opened.
               repeat until (exists table 1 of scroll area 1 of sheet 1)
                   delay 0.01
               end repeat
               
               set myTable to table 1 of scroll area 1 of sheet 1
               set searchField to text field 1 of sheet 1
               
               -- Allow ALL except WHITE list to be considered JUNK by setting FIRST Specifier to "*"!
               try
                   if (count of whiteList) > 0 and first item of junkList is equal to "*" then
                       # will never get here if whiteList is undefined or empty!
                       # Simulate an ALL is JUNK mode - ignoring the rest of junkList.
                       set junkList to {""}
                   end if
               end try
               
               # SEARCH MATCHES for ALL Specifiers of junkList
               repeat with theJunkSpecifier in junkList
                   if theJunkSpecifier is equal to "" and theJunkSpecifier is not equal to first item of junkList then
                       display notification "Invalid junk specifier \"\""
                       set theJunkSpecifier to "*empty specifier ignored*"
                   end if
                   
                   select searchField
                   set value of searchField to theJunkSpecifier
                   
                   -- WAIT UNTIL Search finalizes, with either some rows OR "No Saved Website Data" is displayed!
                   set prospectiveMatches to true
                   repeat while prospectiveMatches
                       delay 0.01
                       
                       if (count of (rows of myTable)) > 0 then
                           --display notification "Found prospective junk for: '" & theJunkSpecifier & "'"
                           exit repeat
                       else
                           try
                               set uiChildren to entire contents of sheet 1
                               
                               repeat with theUIChild in uiChildren
                                   if class of theUIChild is static text and value of theUIChild contains "No Saved Website Data" then
                                       set prospectiveMatches to false
                                       exit repeat
                                   end if
                               end repeat
                           end try
                       end if
                   end repeat
                   -- END WAIT (repeat while prospectiveMatches                    
                   
                   set prospectiveJunkRows to count of (rows of myTable)
                   set whiteListed to false
                   set rowSite to ""
                   set rowIndex to 1
                   
                   #Reset Scroll bar to TOP, incase Windos was alreasdy opened, and scrolled by User
                   set value of scroll bar 1 of scroll area 1 of sheet 1 to 0
                   
                   # PROCESS ALL Lines MATCHING the junk specifier
                   repeat while prospectiveJunkRows ≥ rowIndex
                       try
                           set selectedRow to row rowIndex of myTable
                           select selectedRow
                           set rowSite to description of first UI element of selectedRow
                           set domainName to first item of my textTokens(rowSite, space)
                           
                           if debugLog then
                               log "-------- TOP"
                               log "Cookie: '" & rowSite & "'"
                               log "Domain: '" & domainName & "'"
                               log "rowIndex: " & rowIndex as text
                               log "prospectiveJunkRows: " & prospectiveJunkRows as text
                               log "count of rows: " & (count of (rows of myTable)) as text
                           end if
                           
                           -- AppleScript indexing is 1 based BUT item 0 maps to 1 so it can be confusing!
                           -- SCROLL so selected row (as well as at least the prior and next rows) are in the VISIBLE Scroll port.
                           repeat while (count of value of attribute "AXVisibleChildren" of row (my min(rowIndex + 1, prospectiveJunkRows)) of myTable) = 0
                               --tell scroll area 1 of sheet 1 to perform action "AXScrollUpByPage"
                               click button 1 of scroll bar 1 of scroll area 1 of sheet 1
                               delay 0.01
                           end repeat
                           repeat while (count of value of attribute "AXVisibleChildren" of row (my max(rowIndex - 1, 1)) of myTable) = 0
                               --tell scroll area 1 of sheet 1 to perform action "AXScrollDownByPage"
                               click button 2 of scroll bar 1 of scroll area 1 of sheet 1
                               delay 0.01
                           end repeat
                           -- END SCROLL
                           
                           -- SEARCH whiteList
                           set whiteListed to false
                           repeat with whiteSite in whiteList
                               --log "White: '" & whiteSite & "'"
                               
                               # whiteSite is a REFERENCE which must be DEreferenced to be correctly compared!
                               --log "Equals: " & (domainName is equal to whiteSite) as text
                               --log "Equals: " & (domainName is equal to whiteSite as text) as text
                               
                               # INTENTIONALLY not comparing equality when "*" found!
                               if first character of whiteSite = "*" and last character of whiteSite = "*" then
                                   if domainName contains (text 2 through -2 of whiteSite) then
                                       log "Contains: " & (text 2 through -2 of whiteSite)
                                       set whiteListed to true
                                       exit repeat
                                   end if
                               else if first character of whiteSite = "*" then
                                   if domainName ends with text 2 through -1 of whiteSite then
                                       log "Ends with: " & (text 2 through -1 of whiteSite)
                                       set whiteListed to true
                                       exit repeat
                                   end if
                               else if last character of whiteSite = "*" then
                                   if domainName begins with text 1 through -2 of whiteSite then
                                       log "Begins with: " & (text 1 through -2 of whiteSite)
                                       set whiteListed to true
                                       exit repeat
                                   end if
                               else if domainName is equal to whiteSite as text then
                                   log "Equals to: " & whiteSite
                                   set whiteListed to true
                                   exit repeat
                               end if
                           end repeat
                           -- END SEARCH whiteList
                           
                           if whiteListed then
                               log "White: " & rowSite
                               set rowIndex to rowIndex + 1
                           else
                               log "Delete: " & rowSite
                               
                               # DELETE DATABASE first if present.
                               if rowSite contains "Databases" then
                                   -- FIND & DELETE DATABASE file
                                   log domainName & ": Has Databases!"
                                   
                                   # Limiting GRIDINESS of conatins by prefixing a "." and "_"                                    
                                   set myFiles to (files of application "System Events"'s folder (databasesPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))
                                   set myFiles to myFiles & (folders of application "System Events"'s folder (indexedDBPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))
                                   
                                   repeat with theFile in myFiles
                                       log "Trash: " & name of theFile
                                       move theFile to application "System Events"'s trash
                                   end repeat
                                   -- END FIND & DELETE
                               end if
                               -- END DELETE DATABASE
                               
                               # DELETE COOCKIE!
                               set repeatCount to 0
                               repeat until (repeatCount > 10) or (prospectiveJunkRows > (count of (rows of myTable)))
                                   set repeatCount to repeatCount + 1
                                   
                                   # Because row may be last row and might have been deleted just after above <until...> was evaluated.
                                   try
                                       # Row must be SELECTED (even if was) to enable the Remove button!
                                       repeat until selected of selectedRow
                                           select selectedRow
                                           delay 0.01
                                       end repeat
                                       click button "Remove" of sheet 1
                                       delay 0.1
                                   end try
                               end repeat
                               
                               if prospectiveJunkRows = (count of (rows of myTable)) then
                                   log "Failed deleting: " & rowSite
                               end if
                               -- END DELETE COOCKIE
                           end if
                       on error errorMessage number errorNumber from f to t partial result p
                           --log "*** ERROR! " & errorMessage
                           error errorMessage number errorNumber from f to t partial result p
                       end try
                       
                       # Here instead of just after DELETE because it might also change outside of our control!
                       set prospectiveJunkRows to count of (rows of myTable)
                       
                       if debugLog then
                           log "-------- BOTTOM"
                           log "Cookie: '" & rowSite & "'"
                           log "Domain: '" & domainName & "'"
                           log "rowIndex: " & rowIndex as text
                           log "prospectiveJunkRows: " & prospectiveJunkRows as text
                           log "count of rows: " & (count of (rows of myTable)) as text
                       end if
                   end repeat #while prospectiveJunkRows > rowIndex    
                   -- END PROCESS ALL Lines MATCHING
               end repeat #with theJunkSpecifier in junkList
               -- ENND SEARCH
               
               click button "Done" of sheet 1
               
               # Don't like this style.
               --keystroke "w" using command down
               
               # This is hard coded but works
               --click button 1
               
               # WARNING: this line will FAIL upon removal of the () around <butons where...>
               # because the <where ...> clause will be wrongly applied the implied
               # <of windowPreferences> target instead of its <buttons> collection!    
               click button 1 of (buttons where role description = "close button")
           end tell #windowPreferences
       end tell
   end tell
end main

on textTokens(textToParse, parseDelimiters)
   set listTokens to missing value
   set asTID to AppleScript's text item delimiters
   
   try
       set AppleScript's text item delimiters to parseDelimiters
       set listTokens to text items of textToParse
   end try
   
   set AppleScript's text item delimiters to asTID
   return listTokens
end textTokens

on min(n1, n2)
   if n1 ≤ n2 then
       return n1
   end if
   
   return n2
end min

on max(n1, n2)
   if n1 ≥ n2 then
       return n1
   end if
   
   return n2
end max

on run {}
   main()
end run

* Revised to reflect 2 suggestions by Nigel.
* Revised 2 <repeat while not ...> to <repeat until ...>

Last edited by Ron_Pinkas (2018-06-05 08:42:35 am)


Filed under: GUI, safari, Cleaner, cookie

Offline

 

#2 2018-06-04 03:01:55 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Hi Ron.

Welcome to MacScripter and thanks for posting your code. I haven't got round to trying it yet, but just looking at the beginning of the System Events block:

Applescript:

tell application "System Events"
   tell process "Safari"
       activate
       set frontmost to true
       
       set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
       click menu item "Preferences…" of myMenu
       
       repeat while not (exists window 1)
           delay 0.01
       end repeat
       set windowPreferences to window 1
       
       -- etc. …
   end tell
end tell



'activate' is an application command, so here it's being applied to the application "System Events", not to the process "Safari". If you want to activate Safari, the command must go to the Safari application.

Repeating while window 1 doesn't exist assumes that Safari has no windows open at all when the "Preferences…" menu item's clicked. It would be safer to count the windows first, then click the menu item and wait until the number of windows is more than it was:

Applescript:

tell application "Safari" to activate

tell application "System Events"
   tell process "Safari"
       set frontmost to true
       
       set windowCount to (count windows)
       
       set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
       click menu item "Preferences…" of myMenu
       
       repeat until ((count windows) > windowCount)
           delay 0.01
       end repeat
       set windowPreferences to window 1
   end tell
   
   -- etc. …
end tell


NG

Offline

 

#3 2018-06-04 08:15:42 am

Ron_Pinkas
Member
Registered: 2018-06-04
Posts: 2

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Hi Nigel,

Many thanks indeed.

Strangely somehow in all the samples I saw, Safari was listed only as a process rather than an application, and I was myself curious about the distinction. I somehow missed the fact that process and application are not mutually exclusive.

Right after posting I did notice the weakness of <while not exists window 1> but decided to postpone the revision to today. I was wondering if someone will notice before I got to it. smile

But, your suggested correction also assumes that the preferncesWindow is not already open smile

The correct fix is:

Applescript:


           set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
           click menu item "Preferences…" of myMenu
           # WAIT till opened.
           repeat until (exists button "Privacy" of toolbar 1) of window 1
               delay 0.01
           end repeat

And while at it, I of course corrected the equally important omission of "macsripter.net" from whiteList. smile

Thanks again.

Ron

Last edited by Ron_Pinkas (2018-06-04 01:16:45 pm)

Offline

 

#4 2019-03-05 03:17:53 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 14

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Hi, Nigel.

Thanks for your post. This script is amazing, but not complete - it won't work for Safari menu items on other languages.

For example on greek Safari "Privacy" is "Απόρρητο", "Preferences" is "Προτιμήσεις…", "Manage Website Data…" is "Διαχείριση δεδομένων ιστότοπων…" and so on. After replacing english with greek terms script works fine:)

Maybe exists any way for getting Safari menu item's names directly from current user's Safari (Not hardcoding names)?

Last edited by KniazidisR (2019-03-05 03:34:57 am)

Offline

 

#5 2019-03-06 05:28:00 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

Re: Safari CookieCleaner - full featured white/junk list based cleaner

KniazidisR wrote:

Maybe exists any way for getting Safari menu item's names directly from current user's Safari (Not hardcoding names)?


This is the best I can do so far, opening the sheet without knowing the menu item and button names. I don't know how well it works with non-English localisations:

Applescript:

-- Open the "Manage Website Data" sheet in Safari's Preferences window.

-- This has only been tested in English in Mojave and assumes:
-- • That no one would be stupid enough to run it with anything other than Safari's documents or Preferences windows open.
-- • That the first item with an associated keystroke in the application menu is "Preferences…" in the local language.
-- • That the "Privacy" button is the seventh in the Preferences window toolbar.
-- • That the "Privacy" pane has seven UI elements in group 1 of group 1 and that it's the only pane with this many in the group.
-- • That the first named button in the group is "Manage Website Data…".

tell application "Safari"
   activate
   set windowCount to (count windows) -- Safari only counts its document windows
end tell

tell application "System Events"
   tell process "Safari"
       set frontmost to true
       
       -- Click the first menu item in the "Safari" menu which has a keyboard shortcut. This is hopefully "Preferences…" in the local language.
       set myMenu to menu 1 of menu bar item 2 of menu bar 1
       click (first menu item of myMenu where class of value of attribute "AXMenuItemCmdChar" is not missing value)
       
       -- Delay until the number of Safari windows is more than the number of its document windows.
       -- This assumes that that the only non-document window likely to be open at the time is the Preferences window itself.
       repeat until ((count windows) > windowCount)
           delay 0.1
       end repeat
       -- Use a 'tell' statement with an index reference to the window, because setting a variable would result in a name reference — and the window's name may change when the button's clicked.
       tell window 1
           -- Cllck the seventh button in the window's toolbar, delay until there are seven UI elements in group 1 of group 1 (only true for the "Privacy" pane), then click the first named button in that group.
           click button 7 of toolbar 1
           tell group 1 of group 1
               repeat until ((count UI elements) is 7)
                   delay 0.1
               end repeat
               click button (first text of (get name of buttons))
           end tell
           
           -- Delay until the drop-down sheet appears
           repeat until (sheet 1 exists)
               delay 0.1
           end repeat
           -- Then delay until the "Loading Website Data…" message disappears.
           repeat while ((count sheet 1's static texts) > 1)
               delay 0.5
           end repeat
           
           set myTable to table 1 of scroll area 1 of sheet 1
           set searchField to text field 1 of sheet 1
           
       end tell
   end tell
   
   -- etc. …
end tell

Last edited by Nigel Garvey (2019-03-06 07:33:07 am)


NG

Offline

 

#6 2019-03-09 01:25:38 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 14

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Hi, Nigel. A tried your solution, but it throw error and script failes. One bug found: if in cookies-window exists less than 7 sites, scroll area don't exists. Exists only static list without scroll. So, referring to scroll area in such case throws the next error (other type error, not with localization):

error "System Events got an error: Can’t get scroll bar 1 of scroll area 1 of sheet 1 of window \"Privacy\" of application process \"Safari\". Invalid index." number -1719 from scroll bar 1 of scroll area 1 of sheet 1 of window "Privacy" of application process "Safari"

Now, I found nice solution for clicking buttons without hardcoding names and script below works, (with problem,descripted above). I add one more handler also for timing. My changes has BOLD chars. now I am looking for replace 2 hardcode names yet (with BOLD too, "Δεν υπάρχουν αποθηκευμένα δεδομένα ιστοτόπων and  "Βάσεις δεδομένων". Sorry for my English

(*
Author: Ron Pinkas (edited by KniazidisR)

You are free to use this code in any way permitable by the GNU General Public License V3.0
*)

on main()
    set debugLog to false
   
    # Item starting with "*" will match domain names ENDING with its subssequent text
    # Item endiding with "*" will match domain names BEGINING with its preceding text
    # Items starting and ending with "*" will match domain names CONTAINING its enclosed text   
    # Otherwise NON GRIDY so use full name INCLUDING .com etc.!
    set whiteList to {"amazon.com", ¬
        "ebay.com", ¬
        "facebook.com", "facebook.net", ¬
        "github.com", "*.githubusercontent.com", ¬
        "google.com", "google-analytics.com", "*.googleapis.com", "googleusercontent.com", "gstatic.com", ¬
        "instagram.com", "cdninstagram.com", ¬
        "netflix.com", ¬
        "paypal.com", ¬
        "twitter.com", "twimg.com", ¬
        "visualstudio.com", ¬
        "whatsapp.*", ¬
        "youtube.com"}
   
    # Items in this list will be fed AS-IS to Safari's cookie search field, which is GRIDY, i.e
    # it uses CONTAINS logic to search for matches - so be careful or better yet
    # protect valuable domains using the above whiteList!
    # IF FIRST Element is "*" then the junkList will be ignored and ALL EXCEPT whiteList will be deleted!
    set junkList to {"*", "adroll.com", ".co.", "addthis", "adobe", "affirm", "akc", "allure", "akamai", "azureedge", ¬
        "barron", "beaver", "bam-x", "bing", "bkrtx", "blue", "bold", "boot", "bounce", "btt", "b-cdn", ¬
        "cdn", "chartbeat", "chimp", "click", "cloudflare", "cloudfront", "cnbc", "cohesion", "cookie", "count", "coust", "crazy", "createjs", "criteo", "crsspxl", "crwd", ¬
        "dazz", "demdex", "dental", "desk", "digitech", "disqus", "docashop", ¬
        "exelator", "expose", "ggpht", "facebook", "fbcdn", "google-analytics", "googleadservices", "googletag", "googleusercontent", "gravatar", "gstatic", ¬
        "home", "imgur", "jeeng", "link", "local", "mac", "networks", "newrelic", "omny", "opentok", "optimizer", "parsely", "petametric", "pir.fm", "porn", "quantserve", ¬
        "reddit", "resources", "score", "sekindo", "sstatic", "stack", "taboola", "track", "truspilot", "twimg", "user", "visistat", "ytimg"}
   
    set homePath to (path to home folder)
    set databasesPath to alias ((homePath as text) & "Library:Safari:Databases")
    set indexedDBPath to alias ((databasesPath as text) & "___IndexedDB")
    set timeoutSeconds to 2.0
   
    -- Open application "Safari" via Dock
    set uiScript to "click UI Element \"Safari\" of list 1 of application process \"Dock\""
    my doWithTimeout(uiScript, timeoutSeconds)
   
    -- Click menu item "Safari" of Safari menu bar
    set uiScript to "click menu item 4 of menu 1 of menu bar item \"Safari\" of menu bar 1 of application process \"Safari\""
    my doWithTimeout(uiScript, timeoutSeconds)
   
    -- Click menu item "Preferences" of menu item "Safari"
    set uiScript to "click menu item 4 of menu 1 of menu bar item \"Safari\" of menu bar 1 of application process \"Safari\""
    my doWithTimeout(uiScript, timeoutSeconds)
   
    -- Click UI element "Privacy"
    set uiScript to "click UI Element 7 of tool bar 1 of window 1 of application process \"Safari\""
    my doWithTimeout(uiScript, timeoutSeconds)
   
    -- Click button "Manage Website Data…"
    set uiScript to "click UI Element 8 of group 1 of group 1 of window 1 of application process \"Safari\""
    my doWithTimeout(uiScript, timeoutSeconds)

   
   
    tell application "System Events"
        tell process "Safari"
            set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
            set windowPreferences to window 1
            tell windowPreferences
               
                repeat until (exists table 1 of scroll area 1 of sheet 1)
                    delay 0.01
                end repeat
               
                set myTable to table 1 of scroll area 1 of sheet 1
                set searchField to text field 1 of sheet 1
               
               
                -- Allow ALL except WHITE list to be considered JUNK by setting FIRST Specifier to "*"!
                try
                    if (count of whiteList) > 0 and first item of junkList is equal to "*" then
                        # will never get here if whiteList is undefined or empty!
                        # Simulate an ALL is JUNK mode - ignoring the rest of junkList.
                        set junkList to {""}
                    end if
                end try
               
                # SEARCH MATCHES for ALL Specifiers of junkList
                repeat with theJunkSpecifier in junkList
                    if theJunkSpecifier is equal to "" and theJunkSpecifier is not equal to first item of junkList then
                        display notification "Invalid junk specifier \"\""
                        set theJunkSpecifier to "*empty specifier ignored*"
                    end if
                   
                    select searchField
                    set value of searchField to theJunkSpecifier
                   
                    -- WAIT UNTIL Search finalizes, with either some rows OR "No Saved Website Data" is displayed!
                    set prospectiveMatches to true
                    repeat while prospectiveMatches
                        delay 0.01
                       
                        if (count of (rows of myTable)) > 0 then
                            --display notification "Found prospective junk for: '" & theJunkSpecifier & "'"
                            exit repeat
                        else
                            try
                                set uiChildren to entire contents of sheet 1
                               
                                repeat with theUIChild in uiChildren
                                    if class of theUIChild is static text and value of theUIChild contains "Δεν υπάρχουν αποθηκευμένα δεδομένα ιστοτόπων" then
                                        set prospectiveMatches to false
                                        exit repeat
                                    end if
                                end repeat
                            end try
                        end if
                    end repeat
                    -- END WAIT (repeat while prospectiveMatches                   
                   
                    set prospectiveJunkRows to count of (rows of myTable)
                    set whiteListed to false
                    set rowSite to ""
                    set rowIndex to 1
                   
                    #Reset Scroll bar to TOP, incase Windos was alreasdy opened, and scrolled by User
                    set value of scroll bar 1 of scroll area 1 of sheet 1 to 0
                   
                    # PROCESS ALL Lines MATCHING the junk specifier
                    repeat while prospectiveJunkRows ≥ rowIndex
                        try
                            set selectedRow to row rowIndex of myTable
                            select selectedRow
                            set rowSite to description of first UI element of selectedRow
                            set domainName to first item of my textTokens(rowSite, space)
                           
                            if debugLog then
                                log "-------- TOP"
                                log "Cookie: '" & rowSite & "'"
                                log "Domain: '" & domainName & "'"
                                log "rowIndex: " & rowIndex as text
                                log "prospectiveJunkRows: " & prospectiveJunkRows as text
                                log "count of rows: " & (count of (rows of myTable)) as text
                            end if
                           
                            -- AppleScript indexing is 1 based BUT item 0 maps to 1 so it can be confusing!
                            -- SCROLL so selected row (as well as at least the prior and next rows) are in the VISIBLE Scroll port.
                            repeat while (count of value of attribute "AXVisibleChildren" of row (my min(rowIndex + 1, prospectiveJunkRows)) of myTable) = 0
                                --tell scroll area 1 of sheet 1 to perform action "AXScrollUpByPage"
                                click button 1 of scroll bar 1 of scroll area 1 of sheet 1
                                delay 0.01
                            end repeat
                            repeat while (count of value of attribute "AXVisibleChildren" of row (my max(rowIndex - 1, 1)) of myTable) = 0
                                --tell scroll area 1 of sheet 1 to perform action "AXScrollDownByPage"
                                click button 2 of scroll bar 1 of scroll area 1 of sheet 1
                                delay 0.01
                            end repeat
                            -- END SCROLL
                           
                            -- SEARCH whiteList
                            set whiteListed to false
                            repeat with whiteSite in whiteList
                                --log "White: '" & whiteSite & "'"
                               
                                # whiteSite is a REFERENCE which must be DEreferenced to be correctly compared!
                                --log "Equals: " & (domainName is equal to whiteSite) as text
                                --log "Equals: " & (domainName is equal to whiteSite as text) as text
                               
                                # INTENTIONALLY not comparing equality when "*" found!
                                if first character of whiteSite = "*" and last character of whiteSite = "*" then
                                    if domainName contains (text 2 through -2 of whiteSite) then
                                        log "Contains: " & (text 2 through -2 of whiteSite)
                                        set whiteListed to true
                                        exit repeat
                                    end if
                                else if first character of whiteSite = "*" then
                                    if domainName ends with text 2 through -1 of whiteSite then
                                        log "Ends with: " & (text 2 through -1 of whiteSite)
                                        set whiteListed to true
                                        exit repeat
                                    end if
                                else if last character of whiteSite = "*" then
                                    if domainName begins with text 1 through -2 of whiteSite then
                                        log "Begins with: " & (text 1 through -2 of whiteSite)
                                        set whiteListed to true
                                        exit repeat
                                    end if
                                else if domainName is equal to whiteSite as text then
                                    log "Equals to: " & whiteSite
                                    set whiteListed to true
                                    exit repeat
                                end if
                            end repeat
                            -- END SEARCH whiteList
                           
                            if whiteListed then
                                log "White: " & rowSite
                                set rowIndex to rowIndex + 1
                            else
                                log "Delete: " & rowSite
                               
                                # DELETE DATABASE first if present.
                                if rowSite contains "Βάσεις δεδομένων" then
                                    -- FIND & DELETE DATABASE file
                                    log domainName & ": Has Databases!"
                                   
                                    # Limiting GRIDINESS of conatins by prefixing a "." and "_"                                   
                                    set myFiles to (files of application "System Events"'s folder (databasesPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))
                                    set myFiles to myFiles & (folders of application "System Events"'s folder (indexedDBPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))
                                   
                                    repeat with theFile in myFiles
                                        log "Trash: " & name of theFile
                                        move theFile to application "System Events"'s trash
                                    end repeat
                                    -- END FIND & DELETE
                                end if
                                -- END DELETE DATABASE
                               
                                # DELETE COOCKIE!
                                set repeatCount to 0
                                repeat until (repeatCount > 10) or (prospectiveJunkRows > (count of (rows of myTable)))
                                    set repeatCount to repeatCount + 1
                                   
                                    # Because row may be last row and might have been deleted just after above <until...> was evaluated.
                                    try
                                        # Row must be SELECTED (even if was) to enable the Remove button!
                                        repeat until selected of selectedRow
                                            select selectedRow
                                            delay 0.01
                                        end repeat
                                                                               
                                                                                -- Click button "Remove"
                                        set uiScript to "click UI Element 1 of sheet 1                of window 1 of application process \"Safari\""
                                                                                my doWithTimeout(uiScript, timeoutSeconds)

                                                                                end try
                                end repeat
                               
                                if prospectiveJunkRows = (count of (rows of myTable)) then
                                    log "Failed deleting: " & rowSite
                                end if
                                -- END DELETE COOCKIE
                            end if
                        on error errorMessage number errorNumber from f to t partial result p
                            --log "*** ERROR! " & errorMessage
                            error errorMessage number errorNumber from f to t partial result p
                        end try
                       
                        # Here instead of just after DELETE because it might also change outside of our control!
                        set prospectiveJunkRows to count of (rows of myTable)
                       
                        if debugLog then
                            log "-------- BOTTOM"
                            log "Cookie: '" & rowSite & "'"
                            log "Domain: '" & domainName & "'"
                            log "rowIndex: " & rowIndex as text
                            log "prospectiveJunkRows: " & prospectiveJunkRows as text
                            log "count of rows: " & (count of (rows of myTable)) as text
                        end if
                    end repeat #while prospectiveJunkRows > rowIndex   
                    -- END PROCESS ALL Lines MATCHING
                end repeat #with theJunkSpecifier in junkList
                -- ENND SEARCH
               
                                -- Click button "Done"
                set uiScript to "click UI Element 3 of sheet 1 of window 1 of application process \"Safari\""
    my doWithTimeout(uiScript, timeoutSeconds)

               
               
                # Don't like this style.
                --keystroke "w" using command down
               
                # This is hard coded but works
                click button 1 of (buttons where role description = "close button")
               
                # WARNING: this line will FAIL upon removal of the () around <butons where...>
                # because the <where ...> clause will be wrongly applied the implied
                # <of windowPreferences> target instead of its <buttons> collection!   
                --click button 1 of (buttons where role description = "close button")
            end tell #windowPreferences
        end tell
    end tell
   
   
end main

on textTokens(textToParse, parseDelimiters)
    set listTokens to missing value
    set asTID to AppleScript's text item delimiters
   
    try
        set AppleScript's text item delimiters to parseDelimiters
        set listTokens to text items of textToParse
    end try
   
    set AppleScript's text item delimiters to asTID
    return listTokens
end textTokens

on min(n1, n2)
    if n1 ≤ n2 then
        return n1
    end if
   
    return n2
end min

on max(n1, n2)
    if n1 ≥ n2 then
        return n1
    end if
   
    return n2
end max

on run {}
    main()
end run

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script "tell application \"System Events\"
" & uiScript & "
end tell"
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout

Last edited by KniazidisR (2019-03-09 03:53:00 am)

Offline

 

#7 2019-03-10 04:13:01 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

Re: Safari CookieCleaner - full featured white/junk list based cleaner

"KniazidisR wrote:

A tried your solution, but it throw error and script failes. One bug found: if in cookies-window exists less than 7 sites, scroll area don't exists. Exists only static list without scroll. So, referring to scroll area in such case throws the next error


Hi KniazidisR.

Just to clear up any misunderstanding, the script in post #1 was written and posted by Ron_Pinkas, not by me.

My contributions in posts #2 and #5 are just different code for opening the Privacy pane in Safari's Preferences window. They don't themselves delete any cookies or caches.

I've not been able to reproduce the error you describe. If there are fewer than seven entries in the scroll area, the scroll area still exists; it just doesn't have a scroll bar. However, I did find a problem yesterday whereby if there were no entries at all, the script would wait forever for the text saying there were no cookies to be replaced with the cookie list! I've now modified the code in post #5 above to get round this.

I also found that in Safari 11.1.2 on my El Capitan machine, the Privacy pane has a different number of UI elements from that in Safari 12.0.3 on my Mojave one. This caused an infinite repeat when I ran the script on the El Capitan machine. I've now made allowances for this too in post #5.

The code has been tested and works in English, French, and Greek environments on my Mojave system and in an English environment on my El Capitan machine.

As I said above, my code doesn't go as far as deleting any cookies or caches. In fact I've never tried running Ron's deletion code. But reading through it yesterday, I noticed that it searches for and deletes certain Safari databases from the disk. In Mojave, the folders containing these databases — and all the container folders in the chain up to and including the "Safari" folder in the user's "Library" folder — are unsearchable by System Events. It just returns an empty list for each search. ('list folder' also returns an empty list and the "find" shell script complains that the operation isn't permitted. The Finder, though, has no problem returning the folders' contents!  hmm )  System Events can access the folders and the databases if it's given the exact paths to them, but it can't find them for itself. So these two lines are unlikely to work in Mojave:

Applescript:

set myFiles to (files of application "System Events"'s folder (databasesPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))
set myFiles to myFiles & (folders of application "System Events"'s folder (indexedDBPath as text) where (name contains ("." & domainName)) or name contains ("_" & domainName))

I may look into this further later on.

Edit: References to "post #4" corrected to "post #5"!

Last edited by Nigel Garvey (2019-03-12 04:01:59 am)


NG

Offline

 

#8 2019-03-11 12:39:09 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 14

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Nigel Garvey wrote:



I've not been able to reproduce the error you describe. If there are fewer than seven entries in the scroll area, the scroll area still exists; it just doesn't have a scroll bar. However, I did find a problem yesterday whereby if there were no entries at all, the script would wait forever for the text saying there were no cookies to be replaced with the cookie list! I've now modified the code in post #4 above to get round this.

I also found that in Safari 11.1.2 on my El Capitan machine, the Privacy pane has a different number of UI elements from that in Safari 12.0.3 on my Mojave one. This caused an infinite repeat when I ran the script on the El Capitan machine. I've now made allowances for this too in post #4.

The code has been tested and works in English, French, and Greek environments on my Mojave system and in an English environment on my El Capitan machine.



Нi, Nigel. Thanks for your reply post.

Exactly, as I can understand, problematic is code lines, which refers to scroll bar 1 (If there are fewer than seven entries in the scroll area, it doesn't have a scroll bar). So, referring to scroll bar 1 is referring to object, which doesn't exist in that case, and throws error. I have Greek Mac OS X EI Captain.

Your latest solution for databases deletion is interesting and I can try later and reply later, as I am busy for now.

P.S. Founded 1 another problem. This code snippet doesn't work stable and throws sometimes error:

tell application "System Events"
        tell process "Safari"
            set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
            click button 1 of (buttons where role description = "close button")
        end tell
end tell


Need replace it with this snippet, which is tested and works stable:

tell application "System Events"
        tell process "Safari"
            set myMenu to menu "Safari" of menu bar item "Safari" of menu bar 1
            end tell
end tell
set uiScript to "click UI Element 2 of window 1 of application process \"Safari\""
my doWithTimeout(uiScript, timeoutSeconds)

Last edited by KniazidisR (2019-03-11 02:29:46 pm)

Offline

 

#9 2019-03-11 11:57:57 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 14

Re: Safari CookieCleaner - full featured white/junk list based cleaner

So, as I said earlier, a reference to a nonexistent scrollbar raises an error on my machine. The error is raised when there are less than seven entries in the site list window. This happens specifically in this line of code:

#Reset Scroll bar to TOP, incase Windos was alreasdy opened, and scrolled by User
                    set value of scroll bar 1 of scroll area 1 of sheet 1 to 0

As you can see from the comment, this line of code is intended to scroll the list of sites to the top position. But! When the list of sites is less than seven, he is already at the top. This fact can be and should be used.

Simply replacing the unstable line of code with the next one solves this problem with scrolling the window completely:

#Reset Scroll bar to TOP, incase Windos was alreasdy opened, and scrolled by User
                    if prospectiveJunkRows > 6 then set value of scroll bar 1 of scroll area 1 of sheet 1 to 0

Last edited by KniazidisR (2019-03-12 12:10:52 am)

Offline

 

#10 2019-03-12 04:43:06 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

Re: Safari CookieCleaner - full featured white/junk list based cleaner

KniazidisR wrote:

So, as I said earlier, a reference to a nonexistent scrollbar raises an error on my machine.


This explains why I couldn't reproduce the error. What you actually reported was:

Hi, Nigel. A tried your solution, but it throw error and script failes. One bug found: if in cookies-window exists less than 7 sites, scroll area don't exists. Exists only static list without scroll. So, referring to scroll area in such case throws the next error


1. The scroll area does exist, regardless of the number of cookie sites listed in it.
2. My code contains no mention of a scroll bar. As I said earlier, my scripts above are just alternative ways to open the cookie window — the one in post #5 doing so without relying on the UI being in English. I've only ever glanced cursorily at Ron's code for deleting the cookies and caches and have never tried running it. This is principally because I'd want to be absolutely sure it was safe before trying it on my own computer! This in turn would require extensive testing in a user other than the main one on the machine — and I've not yet found time to do this. The scroll bar problem should be easy to cure in the first instance, but there are two further mentions of "scroll bar" later in the code and I'd need to check their significance before offering a solution.


NG

Offline

 

#11 2019-03-12 02:48:07 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 14

Re: Safari CookieCleaner - full featured white/junk list based cleaner

Greetings to all.

One of my last responses about this script. It concerns how to improve the script significantly.

How to increase the speed of the script several times, and, at the same time, avoid the use of hard coding of headers like "Databases"?

In its original form, the script compares with white/black lists in the repetition cycle all the records of the site window without diferency, which makes its work slow. What can be done? The answer is taking into account what kind of data the sites contribute when they visit. So. There are 4 types of input data:

1) Cash
2) Cash, Cookies
3) Cash, Cookies, Local storage
4) Cash, Cookies, Local storage, Databases

90% or more of the sites contribute data type 1). But, just a minute. we are not interested in clearing Cash, but cleaning Cookies, Local storage, Databases! Type 1 sites do not need to be processed in a repetition loop for comparison with the “white” and “black” list. Their script should just leave. This would make the script 10-20 times faster.

Now, how to avoid hard coding of headings in buttons and windows, working in English Mac OS and helpless with non-English systems?

Above, I have already shown how to fix it with buttons. And what can be done with the header type "Database"? The answer is simple: do not use it - everything that does not fall under cases 1) and 2) is Local storage and Databases, which need to be cleaned. Checking whether the site leaves data of type 1) or 2) is simple, since it does not differ on computers with different languages and is always written only in English (since these 2 words are a pure English invention). The other 2 words (Local storage and Databases) can not be hard-coded into conditional sentences, since they change from localization to localization.

The solution is simple - use a conditional sentence: all that is not of type 1), 2) is of type 3), 4). And what is of type 1) and 2) you can define and can hardcord. Other way is comparing with template "Cash, Cookies, *, *"

You will not force the user to abandon its localization. It will simply send your script to the trash.

My wishes are better read the author of the script to redo it for getting it really shone.

Last edited by KniazidisR (2019-03-12 03:17:42 pm)

Offline

 
  • Index
  •  » Code Exchange
  •  » Safari CookieCleaner - full featured white/junk list based cleaner

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)