$window(*,N) does that already; it goes through all open windows, in switchbar order. It is connection-specific, but you can loop through $scon(N) and run an inner loop through $window() for each connection. Just be aware that custom windows will be counted multiple times, once for each connection (a custom window is sort of global).