2

Is there a way in OSX to get a list of all applications that are visible on the screen? Can I get a list of all windows of each application, their sizes and position?

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113

1 Answers1

4

You can achieve that with a tool called wmctrl. It may not be installed on Mac OSX but you can get it with brew install homebrew/x11/wmctrl (first, get brew or any package manager that knows where to find wmctrl if you don't want to mess with git repos and errors).

Then, you should be able to get active windows with something like this:

BASH

wmctrl -l

You may utilize the output of that command into something else afterwards:

BASH

./myProgram $(wmctrl -l)

Hope this helps!


UPDATE:

In case your window manager isn't compatible with wmctrl, your best way out is to use AppleScript for that. There is pretty simple ways of doing it, like suggested in this answer. I think this approach will make it easier for you to get the window attributes.

AppleScript

tell application "System Events"
    repeat with theProcess in processes
        if not background only of theProcess then

[...]

Have fun and good luck :)

Community
  • 1
  • 1
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • 1
    running `wmctrl -l` gives no output, and `wmctrl -d` gives me ` Cannot get number of desktops properties. (_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)` – Filip Haglund Nov 15 '15 at 18:49
  • wmctrl -m Name: N/A Class: N/A PID: N/A Window manager's "showing the desktop" mode: N/A – Filip Haglund Nov 15 '15 at 18:49
  • @FilipHaglund I think this happens because wmctrl assumes that you are using a given windows manager which you are not. I updated my answer to add an alternative that is more OSX-friendly. – Frederik.L Nov 15 '15 at 18:59
  • Is there a faster way? Looping through the 6 windows I have open takes about a second, so I'm assuming that's the time it takes for the runtime to start. – Filip Haglund Nov 15 '15 at 19:17
  • I honestly don't know about a faster way. You could manage to use a lower-level system call to get the processes running and window information. However, it could get you involved into some harder to maintain mechanics along with incompatibility issues whenever you install a new OSX. – Frederik.L Nov 15 '15 at 20:07