AppleScript to search the contents of Safari Windows & Tabs

I have too many windows open in Safari (416 tabs in 133 windows currently – yah, I wrote an AppleScript to count those 🙂 ).

Got tired of not being able to find something I knew I had open so I wrote an AppleScript to search the text content of all Safari tabs in all Safari windows that aren’t minimized (even across Spaces).  Does not currently search tabs in windows that are minimized to the dock.

I’ve now found this useful enough that I thought I’d share it.  Disclaimer: I’m not an AppleScript programmer and don’t really like AppleScript (probably because I don’t know the language and tricks!).  There are likely things that could be done better or smarter.  Contributions welcome.

That said, this seems to generally work and, thanks to me learning about using references, fast enough that I wasn’t sure it was actually working at first 🙂

Source is on GitHub here.

Instructions for installation are in the Read Me there.

Advertisement

Faster to write a script than to deal with so many browser windows? Well, certainly more fun :)

AppleScript to move Safari windows up as much as the window closest to the top of the screen will allow.

There’s a bug in Safari 15.0 where it sometimes re-positions open windows when waking from sleep by moving all the windows down an amount such that the bottom of the lowest one is at the bottom of the monitor (or something like that? They all move down a lot).

If happen to place your Safari windows up enough so that another app’s window can be visible on-screen under them (chat in my case), and you have a few too many windows open in Safari (ahem!), then you might be very annoyed to have to over a hundred windows all back up again.

Now, you could go through your heap of open Safari windows, but if you’re a programmer than you might (likely incorrectly ;)) think it’d be faster to write an AppleScript to move them all back up again. Not that I’d do such a thing, you understand, but, purely for educational purposes, here’s the kind of script one might write (or the second version if the first one took 6 minutes process the > 100 Safari windows you had open) :). This one moves all Safari windows up the same amount to keep their relative positioning and moves them up as much as possible while still keeping the window positioned highest on the screen below the menu bar.

Disclaimer: I write zero to two AppleScripts a year and never really learned AppleScript. There’s almost certainly better ways to do this. But it works for me and fixes my windows after Safari munges them again. So GoodEnough™ 🙂

-- Put in the public domain with zero warranty of any kind
--
-- only tested on macOS 11.6, with Safari 13.0; ymmv

use scripting additions

tell application "Safari"
  set windlist to windows
  
  log "Examining " & length of windlist & " windows..."
  
  set lowestYWindow to first item of windlist
  set lowestYValue to get second item of (get bounds of lowestYWindow)
  
  repeat with wind in windlist
    set curValue to second item of (get bounds of wind)
    if curValue < lowestYValue then
      copy wind to lowestYWindow
      set lowestYValue to curValue
    end if
  end repeat
  
  -- subtract out the title bar height (hard coded - bad dog!)
  set yOffset to lowestYValue - 25
  
  if yOffset > 0 then
    log "moving " & length of windlist & " windows up " & yOffset & " pixels"
    repeat with aWind in windlist
      set aBounds to (get the bounds of aWind)
      set the second item of aBounds to ((get the second item of aBounds as integer) - yOffset)
      set the fourth item of aBounds to ((get the fourth item of aBounds as integer) - yOffset)
      set the bounds of aWind to aBounds
    end repeat
  else
    display alert ("The highest window already at the top!  Not moving any windows!")
  end if
  log "done"
end tell

Here it is in a gist for easier copy/edit, etc.