0

I would like to be able to generate a list, and hopefully access one-by-one, all the open windows of any specific app no matter what desktop they are in,together with which Desktop Workspace (Mission Control Space) each is in.

In general, AppleScript and other Apple tools seem to only be able to list those that are visible in the active Desktop Workspace (which seems pretty dumb and like Apple internally doesn't really know how to use the Mission Control feature).

Many apps have a Windows menu that lists all the open windows of that app, but that seems like I would have to have specific macros to manage the lists for each different app. That's not ideal. And it's not complete, because those in-app lists simply list the window title, file name, or whatever and not the Desktop Workspace.

Specifically, to start, I want to list all the open TextEdit windows and all the open Typora windows. They have very different UIs for showing that list themselves.

Eventually, I would like to have a small app window in the corner of each Desktop Workspace that displays a name or other ID for that desktop space. Then I could change to a different desktop by activating that app window and then putting it in the background.

Most significantly, this would allow me to change to the same working desktop even if I have rearranged the order of desktops in Mission Control (because the target window would move with the whole desktop). Normally, Mission Control changes the numbering of the desktops and the assigned hotkeys then point to whatever desktop is now in that numerical position. IMHO, this is one of the key flaws in Mission Control that has kept people from using it extensively.

So far I have found the Apple JSX method/function $.CGWindowListCopyWindowInfo(16, 0)

The first argument sets some filters on the data returned. Here 16 is the value of the constant $.kCGWindowListExcludeDesktopElements, which excludes desktop icons, the desktop itself, and some other "windows" that can't be seen. The second argument is a window ID to allow you to select windows in front of or behind that specific window, so 0 shows all windows.

That method typically dumps hundreds of items that need further filtering and massaging. Here's one entry as formatted by js-beautify:

$({
    "kCGWindowLayer": $(0),
    "kCGWindowAlpha": $(1),
    "kCGWindowMemoryUsage": $(1248),
    "kCGWindowSharingState": $(1),
    "kCGWindowOwnerPID": $(1410),
    "kCGWindowNumber": $(322),
    "kCGWindowOwnerName": $("TextEdit"),
    "kCGWindowStoreType": $(1),
    "kCGWindowBounds": $({
        "X": $(0),
        "Height": $(679),
        "Y": $(23),
        "Width": $(385)
    }),
    "kCGWindowName": $("Which Apps Have I Used.rtf")
}),

In this example, the kCGWindowBounds place it at the left margin of the screen, at the top (immediately below the toolbar), a bit taller than it is wide. Fine. But it doesn't tell me that in Mission Control it's in Desktop 16.

If I had multiple displays, I could figure out which display it was in because those values treat all the different displays as a single desktop. An X value of over 1440 (the width of my laptop display) would put it in the monitor to the right of my laptop, if my laptop was leftmost.

But HOW do I determine which Mission Control Desktop Workspace that particular app window is in? Especially, how do I determine that for a number of windows, without having to actually activate and go to each window to find out which desktop is now active?

There are some apps that have been able to do something like this in the past, notably HyperSpace and CurrentKey, but HyperSpace didn't work beyond OSX 10.11 and CurrentKey has been withdrawn due to time limitations on supporting a free app and inability to support the Monterey OS.

.

Any ideas?

6
  • Not cool calling names like "pretty dumb". Documentation states: "If the desktop on your Mac gets cluttered with open app windows, you can use Mission Control to create additional desktops, called spaces, to organize the windows. When you work in a space, only the windows that are in that space are shown". So maybe you use wrong tool to solve your problem? Or you use correct tool in a wrong way? Commented Aug 15, 2022 at 4:28
  • 1
    @AivarPaalberg, I appreciate your concern about flinging insults around; there is alway a risk condescension and bullying. In this case, I stand by my statement that Apple's lack of support for Spaces, especially the withdrawing of APIs that used to help, is "pretty dumb". If Apple feels like I am bullying or being condescending towards them, I will publicly apologize. I used to work at Apple, writing documentation for XCode Instruments, so this is not name calling, it's my professional assessment.
    – August
    Commented Aug 15, 2022 at 20:02
  • Do I understand correctly. There is a tool which functionality is described as "the desktop on your Mac gets cluttered with open app windows /.../ create additional desktops, called spaces, to organize the windows /.../ only the windows that are in that space are shown". This tool is addressing the issue of cluttered app windows by separating them into spaces, however you have complaint that after separating them using this tool it's hard or impossible to consolidate/clutter these windows (or information about them) back in a specific way? Commented Aug 16, 2022 at 6:25
  • In a way, yes. But you are mixing levels. I separate my open apps and browser windows into different contexts, Desktop Spaces, so that it's easier for me to switch entire contexts rather than hiding some Finder windows, unhiding others, and doing the same with browsers, editors, and apps. Switching desktops allows me to switch contexts, take care of something or another, and, most importantly, go right back to where I had been, with just a couple of keystrokes.
    – August
    Commented Aug 16, 2022 at 18:49
  • Right now, I have 21 desktops. To make the switching process sooth and easy, I have menu, generated in Keyboard Maestro, dispalyed by they hotkey combination Ctl+Opt+Shift+Z, that lists all my desktops and accepts a single keystroke as input to selects the desktop to switch to. Because the desktops are named, it's much. easier for me to type Q to got to my Projects Queue desktop than it is to remember that that is Desktop 17.
    – August
    Commented Aug 16, 2022 at 18:55

1 Answer 1

1

Related: https://stackoverflow.com/a/67338053/13100156

I found that none of the example code would produce a result when I ran it in Script Editor (on Monterey), but the second example script runs fine under TextMate.

enter image description here

It is interesting that this counts two windows for Todoist, but only one is visible. It counts four windows for Preview, but only two are visible. Not quite sure what that's about. The only visible window on the Space I ran it on was TextMate.

The example does not show the desktops the windows are on, but it would be a start for you.

2
  • Thanks @Marc, for the link to that Stack Overflow post. Yes, it could be a start. Very appreciated. Could you please elaborate on "the second example script runs fine under TextMate"? A pointer to an explanation? That's a new concept for me, TextMate has only ever been an editor for me, not an IDE.
    – August
    Commented Aug 15, 2022 at 20:15
  • Most bundles have a Run menu option. If you're editing an Applescript, you can have TextMate run it. Commented Aug 16, 2022 at 2:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .