Advertisement
Radzinsky

AppleScript loop to embed web search filter for all Safari Google Searches

May 22nd, 2024
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --20240522
  2. --simple script to loop every 4 seconds an add the UDM=14 argument into any google search on the active Safari tab. UDM=14 applies web search which gets rid of a lot of Google cruft
  3.  
  4. set udmImage to "&udm=2" --udm of image search
  5. set udmWeb to "&udm=14" --udm of web search
  6. set tbmVideo to "&tbm=vid" --argument for video search (I believe)
  7. set tbmNews to "&tbm=nws" --argument for news search
  8. set approvedUDMs to {tbmVideo, tbmNews, udmImage, udmWeb} --the list of search types that are OK
  9. --can add more UDMs that are approved, but I don't use them so I haven't investigated
  10. set lengthOfApprovedUDMs to length of approvedUDMs
  11.  
  12. repeat
  13.     delay 4 --check every 4 seconds (can be changed, but 4 secs is low impact)
  14.     tell application "System Events" to set frontmostProcess to first process whose frontmost is true
  15.     if name of frontmostProcess is "Safari" then
  16.         tell application "Safari"
  17.             set theURL to URL of current tab of window 1
  18.  
  19. --embed the udm=14 argument into a google search URL in safari as long as it's not already there and only if Safari is the frontmost app
  20.             if theURL contains "google.com/search" and theURL does not contain "&udm=14" then
  21.                 set theURLisApproved to false
  22.                 set i to 1
  23.                 repeat while i ≀ lengthOfApprovedUDMs and theURLisApproved is false
  24.                     if theURL contains item i of approvedUDMs then set theURLisApproved to true
  25.                     set i to i + 1
  26.                 end repeat
  27.                 if theURLisApproved is false then
  28.                     set theNewURL to theURL & "&udm=14"
  29.                     set URL of current tab of window 1 to theNewURL
  30.                 end if
  31.             end if
  32.         end tell
  33.     end if
  34. end repeat
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement