1

NOTE - More clarification of this question has been added. See "UPDATE", below.

Is there a way in BigSur (version 11.6) to query the dimensionsbounds of any window that is showing on the screen?

When running under X Windows in linux, I can run the xwininfo command and click on any open window, and the window's attributes, including its dimensionsbounds, will be output. Is there a corresponding command for MacOS? Note that I am not running X Windows on my Mac.

Thank you very much in advance.

UPDATE: I mentioned the following below in the comments, but I want to add it here also now, for clarity ...

There are cases where I don't know the application name of the window whose dimensionsbounds I want to query. All I see is a window, and sometimes, there is no indication on the window itself as to what application the window belongs to. If I do not know the application name of a given window, how can I query the dimensionsbounds of that particular window?

Also, there is another issue: there are some windows which are not created by a normal *.app type appliction. For example, when I am using the mpv program to view a video, that application is not an *.app; rather, it is simply an executable which has been installed by Homebrew. Because of this, there is no way to query its window's bounds using the following Applescript command ...

tell application "mpv"
    set position to bounds of window 1
end tell

This Applescript command fails with windows created with non-*.app type programs such as mpv.

I am looking for a more general way to query window dimensions, even if I don't know the name of the application controlling the window, and even if the application is not a standard *.app application.

Is there a way to do this?

3 Answers 3

2

Based on the very helpful answer by @jksoegaard, I came up with this python solution. It gives me all the info that I want in JSON format, which then can be parsed and interpreted by any other piece of software.

First of all, it's necessary to install the py-applescript package, as follows:

pip3 install py-applescript

Then, this is a program that I call wininfo ...

#!/usr/local/bin/python3

import os
import sys
import json
import applescript

prog = None
cmd  = 'tell Application "System Events" to get the {name,position,size} of every window of every process'

def main():
    s = applescript.AppleScript(cmd)
    output = s.run()
    stuff = zip(*output)
    info = []
    unnamed = 0
    for row in stuff:
        for item in zip(*row):
            if item[0]:
                name = item[0]
            else:
                name = f'unnamed-{unnamed:0>3}'
                unnamed += 1
            x = item[1][0]
            y = item[1][1]
            w = item[2][0]
            h = item[2][1]
            info.append(
                dict(
                    name=name,
                    x=x,
                    y=y,
                    w=w,
                    h=h
                )
            )
    print(json.dumps(info, indent=2))
    return 0

if __name__ == '__main__':
    prog = os.path.basename(sys.argv[0])
    sys.exit(main())

It outputs data in this format to stdout ...

[
  {
    "name": "MyWindow",
    "x": 27,
    "y": 119,
    "w": 1005,
    "h": 633
  },
  {
    "name": "unnamed-000",
    "x": 0,
    "y": 0,
    "w": 1280,
    "h": 1
  },
  {
    "name": "unnamed-001",
    "x": 0,
    "y": 799,
    "w": 1280,
    "h": 1
  },
  {
    "name": "unnamed-002",
    "x": 1279,
    "y": 0,
    "w": 1,
    "h": 800
  },
  {
    "name": "unnamed-003",
    "x": 0,
    "y": 0,
    "w": 1,
    "h": 800
  },
  {
    "name": "Window",
    "x": 494,
    "y": 75,
    "w": 301,
    "h": 16
  }
]
1
  • This is more suitable for StackOverflow - AskDifferent is not about programming.
    – jksoegaard
    Commented Oct 20, 2021 at 7:42
1

You can use AppleScript to get window information from all processes (no matter if you know the application name, or if it even is an "app" at all):

osascript -e 'tell application "System Events" to get the size of every window of every process'

Note that depending on macOS version, you might need to go to Settings > Security & Privacy and give Accessibility permissions to osascript/Terminal.app.

4
  • Thank you. However, the window created by the mpv program does not show up in this output. This mpv program is something that is installed via Homebrew. Is there something that is more low-level (perhaps a C or Objective C program) which can give me the info for all windows, even those which are created by non *.app programs?
    – HippoMan
    Commented Oct 19, 2021 at 13:50
  • I think your problem can probably be solved in an easier way. Could you tell us what you’re actually trying to do here? I.e. what do you want the dimensions for? Are you going to do screen grabbing, take screenshots, or what is the purpose? Many of these things can be enabled much easier by simple changes to mpv.
    – jksoegaard
    Commented Oct 19, 2021 at 14:57
  • It sounds like you’re trying to reinvent a tiling window manager - have you considered using a tiling window manager?
    – jksoegaard
    Commented Oct 19, 2021 at 17:53
  • I deleted some of my other comments, because I just realized that I made an unrelated error (typographical error). The following indeed worked for me when I typed it correctly: ... get the {name,size,position} for every window of every process, including mpv. As for a tiling window manager, that is not what I need, because I don't want my windows to be tiled. I want to position them myself, using my own algorithm, which might involve certain windows being partially overlapping (instead of tiled). In any case, I now see how to do this. In any case, thank you very much for all your help!
    – HippoMan
    Commented Oct 19, 2021 at 18:07
0

Sure. Applescript can tell you.

Example:

tell application "Google Chrome"
    set position to bounds of window 1
end tell

And the result is:

Result:
{180, 29, 1620, 1217}
2
  • Oh, thank you. But what if I don't know the name of the application which is controlling the window? There could be a window open on the screen with no name in the title bar. I'd like to find out its dimensions ... and its title and other attributes, for that matter.
    – HippoMan
    Commented Oct 17, 2021 at 23:04
  • PS: Also, one of the programs I use is the mpv video player that was installed via Homebrew. It doesn't run as a standard MacOS "app", and so I haven't been able to get information about its window via Applescript. even if I know the exact window title. I'm hoping that there is some way to get window attribute info (window bounds and other data) from this kind of MacOS app. But am I out of luck?
    – HippoMan
    Commented Oct 18, 2021 at 1:14

You must log in to answer this question.

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