Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

objective c - Get NSWindow* from CGWindowListCopyWindowInfo

I have accomplished listing all the windows (in z order from front to back I think/hope) using CGWindowListCopyWindowInfo but I am having an issue getting the NSWindow* from it so I can use with orderFront: etc.

It seems I don't even get CGWindowID from it.

This is my code, it is js-ctypes.

var cfarr_win = ostypes.API('CGWindowListCopyWindowInfo')(ostypes.CONST.kCGWindowListOptionAll | ostypes.CONST.kCGWindowListExcludeDesktopElements, ostypes.CONST.kCGNullWindowID);

var cnt_win = ostypes.API('CFArrayGetCount')(cfarr_win);

for (var i = 0; i < cnt_win; i++) {
    var thisWin = {};
    // trying to get NSWindow* to the window here, so i can use with orderFront: etc

    // example on how i get pid:
    var rez_pid = ostypes.API('objc_msgSend')(c_win, ostypes.HELPER.sel('objectForKey:'), myNSStrings.get('kCGWindowOwnerPID'));
    var int_pid = ostypes.API('objc_msgSend')(rez_pid, ostypes.HELPER.sel('integerValue'));
    thisWin.pid = int_pid;

    // How can I get NSWindow*

}

PS: Even though I am using the exclude desktop elements flag I am still get desktop elements like cursor and dock, by any chance if answerer can shed some light on how to fix that too that would be awesome!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The key you should be using to get window ID's is kCGWindowNumber.

And to get a NSWindow from a window number, you could use [NSApp windowWithWindowNumber:windowNumber].

Unfortunately this will only work for windows that your app owns and not for other applications windows.

Furthermore, if you really wanted to use NSWindow once you get the window ID for other app's windows, it's a bad assumption: not all CGWindows are NSWindows. And outside of that above call, Apple doesn't provide a way to get from CGWindow to NSWindow. To work with other app's windows (provided the other app is cooperative), you'll have to stick with working with the CGWindow objects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...