8

I want to use the Accessibility API to get a list of all windows for a given application (external).

The goal is to check if a certain window is open. First I check that the application is running (using [NSWorkspace runningApplications] and checking each one), and then I want to check the title bar text of each window that is open for that application.

PS

So I can create an element for the app using the PID:

AXUIElementRef app = AXUIElementCreateApplication(pid);

but what do I do with it? Am I even going in the right direction? Can't beleive it's so hard to find examples on this.

Ben Packard
  • 26,102
  • 25
  • 102
  • 183

5 Answers5

13

I don't know a way to get window ID and PID from the Accessibility API.
The NSWindow method Laurent mentioned only provides Window IDs but not the PID of the window owning application.
I would use the CGWindowList methods that are available since 10.5.
To get a list of window IDs and the PID of the owner you can try the following:

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
for (NSMutableDictionary* entry in (NSArray*)windowList) 
{
    NSString* ownerName = [entry objectForKey:(id)kCGWindowOwnerName];
    NSInteger ownerPID = [[entry objectForKey:(id)kCGWindowOwnerPID] integerValue];
    NSLog(@"%@:%d", ownerName, ownerPID);
}
CFRelease(windowList);  

You can control if you want all windows (including offscreen, ...) with the option paramter.
Also the entry objects contain a lot more information. Documentation link

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
4

This works for me in Swift 5.1:

let windowList: CFArray? = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID)

for entry in windowList! as Array {

    let ownerName: String = entry.object(forKey: kCGWindowName) as? String ?? "N/A"
    let ownerPID: Int = entry.object(forKey: kCGWindowOwnerPID) as? Int ?? 0
    print("ownerName: \(ownerName), ownerPID:\(ownerPID)")
}
blakeness
  • 211
  • 2
  • 4
3

Use AXUIElementCopyAttributeValues to copy the value for kAXWindowsAttribute, which should be an array of AXUIElement objects representing the application's windows.

As you can guess from its function name, it follows the copy rule.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Nice Hint, Peter. But... It didn't work for me and I finally found out that I always had `kAXErrorCannotComplete` when trying to get `AXUIElementCopyAttributeNames` (I'm getting the `AXUIElementRef` like on the original question, would it be the problem?!). – StuFF mc Aug 03 '14 at 18:28
  • @StuFFmc: I suggest asking a separate question and/or filing a bug. – Peter Hosey Aug 10 '14 at 20:53
  • another example of why we don't just drop a link in an answer. what is the copy rule? click the link to not find out. – dldnh Sep 21 '21 at 20:45
  • It was a much more useful link back when I originally put it there, before Apple broke the link. I've updated the link to point to the new location. – Peter Hosey Jun 28 '22 at 23:44
0

You can use windowNumbersWithOptions:. It lists all the windows from all the applications by their number. But I can't find how to get a NSWindow from a window number...

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • 9
    You can't, even in principle, get an NSWindow from a window number, because the premise is that you are doing this from an external application, i.e. another process, and each NSWindow * pointer is in the address space of the process that owns the window. The window number is the index the window server uses and provides so that any application can do things like request screen snapshots. – Scott Marks Jan 16 '12 at 19:50
0

If you get kAXErrorCannotComplete it is likely because you used a newer project where XCode automatically added the App Sandbox or Hardened Runtime capabilities. Delete them from the Signing and Capabilities tab and it should start working.