How to List All Open Windows on a Desktop

Hey August,

Not as far as I know.

Keyboard Maestro can list apps and the windows of the front app, but I don't think it can list windows of arbitrary apps.

System Events can though.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/09/17 17:11
# dMod: 2021/09/17 17:11 
# Appl: System Events
# Task: List All Windows of All Applications in the Current Desktop (Space).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @List, @Windows, @Desktop
--------------------------------------------------------

tell application "System Events"
   tell (application processes whose background only is false)
      set appNameList to name
      set appWindowNameList to name of windows
   end tell
end tell

set _cntr to 1

repeat with appName in appNameList
   set contents of appName to {"# " & appName & " #" & linefeed} & item _cntr of appWindowNameList & ""
   set _cntr to _cntr + 1
end repeat

set AppleScript's text item delimiters to linefeed

return appNameList as text

--------------------------------------------------------

-Chris


Addendum – 2022/10/03 15:01 CDT


Please Note – the script lists only lists windows from foreground apps.

application processes whose background only is false

If you want to catch windows from non-normal apps then:

application processes whose background only is true

Or you can talk to a specific app directly:

tell application "System Events"
   tell application process "Keyboard Maestro Engine"
      set winList to every window
   end tell
end tell

Or you can try to get every possible window:

tell application "System Events"
   tell (every application process)
      set appNameList to name
      set appWindowNameList to name of windows
   end tell
end tell

set _cntr to 1

repeat with appName in appNameList
   set contents of appName to {"# " & appName & " #" & linefeed} & item _cntr of appWindowNameList & ""
   set _cntr to _cntr + 1
end repeat

set AppleScript's text item delimiters to linefeed

return appNameList as text

Note that some apps (particularly background apps) may not have proper names for their windows. LaunchBar is one such app.

-ccs


2 Likes