0

I'm trying to access the windows of running applications through my application. Currently, I am grabbing the data of all running applications, and the kCGWindowNumber mapped to the applications, but I am having trouble getting the window mapped to that kCGwindowNumber.

How can I reference my windows of other running applications through mine? I have the kCGWindowNumber of those applications, but cannot figure out how to get access to the windows.

This is how I grab my data

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))

Then, I store it into an object, which represents my running application. E.g.

{
    kCGWindowAlpha = 1;
    kCGWindowBounds =     {
        Height = 22;
        Width = 3840;
        X = 5520;
        Y = 0;
    };
    kCGWindowLayer = 24;
    kCGWindowMemoryUsage = 341416;
    kCGWindowNumber = 190;
    kCGWindowOwnerName = "Google Chrome";
    kCGWindowOwnerPID = 272;
    kCGWindowSharingState = 1;
    kCGWindowStoreType = 2;
}

Now that I have the kCGWindowNumber I was trying to access the other applications with this

NSApplication.shared.window(withWindowNumber: kCGWindowNumber_FOR_WINDOW_I_WANT)

but, after reading the documentation I realized that NSApplication.shared returns Returns the application instance, and these windows are not not apart of my application.

Chris
  • 153
  • 2
  • 15
  • I hope you can't. I wouldn't want your application touching my windows. :) – matt Jun 22 '19 at 01:05
  • 1
    @matt I am just trying to be able to access other applications windows so I can resize them or change the position, but I 'm stuck here :-(. I'm trying to be able to resize and position apps the same way as Spectacle. – Chris Jun 22 '19 at 01:24
  • The only way I know of to get the windows of other applications is to use AppleScript to pass thru SystemEvents. – matt Jun 22 '19 at 02:00
  • There used to be some Core Graphics ways of getting all the windows, such as CGSGetWorkspaceWindowList, but I'm pretty sure those are long gone – matt Jun 22 '19 at 02:03
  • For example take a look at this https://github.com/quicksilver/elements.support.window-qsplugin/blob/master/QSWindowHandlingPlugInSource.m I'm pretty sure none of that is possible any more – matt Jun 22 '19 at 02:04
  • SystemEvents uses the Accessibility API. Search SO for "[cocoa] accessibility windows" – Willeke Jun 22 '19 at 11:50
  • @Chris were you able to get the windows ? – Amit Hooda Jun 03 '20 at 10:11

0 Answers0