Detecting Safari PWA windows in Hammerspoon
I spent more time than I want to admit trying to figure out why my Hammerspoon config wasn’t catching new Safari PWA windows opened via links using Right Click > Open in New Window. So I thought a solution needed to be shared.
My Hammerspoon setup watches for windowCreated
events and auto-centers new standard windows (non-standard windows would be windows like modals, file dialogs, etc) using this logic:
if false == win:isStandard() then return end
This check is critical, it prevents centering things like sheets, popups, and tooltips from being centered (they already are). But it also didn’t work for windows opened this specific way. At first, I thought it was some weird quirk or race condition. Nope.
Turns out win:isStandard()
returns false
for Safari PWA windows opened this way, even though they look and act like normal app windows. Even if you open a new Window via File > New Window, win:isStandard()
is true
, but windows opened via the context menu don’t…I guess?
I started logging more aggressively. Specifically, I dumped win:application():bundleID()
for all windows, standard or not. That’s when I saw this for my PWA application: com.apple.Safari.WebApp.A1B2C3D4...
That’s the bundle ID the PWA (WebApp) uses.
The Fix
I updated my check to this:
if false == win:isStandard() or win:application():bundleID():find( "Safari.WebApp" )
Hope it helps someone…