从CGWindowListCopyWindowInfo获取NSWindow * [英] Get NSWindow* from CGWindowListCopyWindowInfo

查看:635
本文介绍了从CGWindowListCopyWindowInfo获取NSWindow *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了使用CGWindowListCopyWindowInfo列出所有窗口(从前到后按z顺序排列,我认为/希望),但是从中获取NSWindow*时遇到了问题,因此可以与orderFront:等一起使用.

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.

看来我什至没有从中得到CGWindowID.

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

这是我的代码,它是js-ctypes.

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:即使我正在使用排除桌面元素"标志,我仍然可以获得诸如光标和停靠之类的桌面元素,如果答题者也可以阐明如何解决该问题,那将是非常棒的事情!

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!

推荐答案

用于获取窗口ID的键是kCGWindowNumber.

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

要从窗口号获取NSWindow,请您可以使用[NSApp windowWithWindowNumber:windowNumber] .

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.

此外,如果您真的想使用NSWindow,则一旦获得其他应用程序窗口的窗口ID,这是一个错误的假设:并非全部CGWindows是NSWindows .在上述调用之外,Apple没有提供从CGWindow到NSWindow的方法.要使用其他应用程序的窗口(前提是其他应用程序是协作的),则必须坚持使用CGWindow对象.

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.

这篇关于从CGWindowListCopyWindowInfo获取NSWindow *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆