获取 OS X 中最前面的 window 的名称,具有辅助功能 API
Get name of frontmost window in OS X with Accessibility API
我正在尝试弄清楚如何使用 Cocoa/Objective-C 获取最前面的 window 的名称。我在我的应用程序中将辅助功能 API 用于其他功能,所以如果需要的话没问题,但我没有找到这样做的命令。有谁知道如何在Objective-C中找到最前面的window的名字?
提前致谢。
你没有说清楚,但我猜你想要最前面的 window 你自己的应用程序。我不知道这是否是最好的,但这对我有用:
- (NSWindow*) frontmostWindowP {
NSArray* windNumsArray = [NSWindow windowNumbersWithOptions:0];
NSInteger frontmostNumb = [[windNumsArray objectAtIndex:1] integerValue];
NSArray* windObjsArray = [[NSApplication sharedApplication] windows];
for ( NSWindow* windP in windObjsArray )
if ( [windP windowNumber] == frontmostNumb )
return windP;
return 0;
}
"[NSWindow windowNumbersWithOptions:0]" returns 一个任意整数数组 "windowNumbers",例如 [ 10, 313, 319, 384 ]。第一个数字对我来说总是“10”,而且是垃圾。我想它可能代表菜单栏或其他东西。无论如何,元素 1 到 N 是应用程序 windows 顺序的 window 编号,从前到后。这就是为什么“[windNumsArray objectAtIndex:1]”。问题是 "windowNumber" 在其他方面毫无用处,但我们已经完成了一半。
"[[NSApplication sharedApplication] windows]" returns 一个 windows 数组,这次是 NSWindow 指针, 不是 在任何可信赖的订单。所以剩下的就是找到具有正确 windowNumber 的 NSWindow*,因此是 for 循环。
如果您对 windows 除了自己的应用感兴趣,请查看 "NSWindow windowNumbersWithOptions" 的选项。
您可以使用 NSApplication 的 keyWindow 并访问 NSWindow.For 的标题 属性 访问其他应用程序的 window 您可以参考这个
Getting the main window of an app via an NSRunningApplication instance
"Apple has traditionally been pretty locked-down about this sort of thing. NSRunningApplication itself was just introduced in 10.6, and as you said, it's a bit limited. Depending on what you want to do, the answer might be in the Accessibility framework or it might be the CGWindow API. You can use the processIdentifier from the NSRunningApplication to match it up with those APIs."
我正在尝试弄清楚如何使用 Cocoa/Objective-C 获取最前面的 window 的名称。我在我的应用程序中将辅助功能 API 用于其他功能,所以如果需要的话没问题,但我没有找到这样做的命令。有谁知道如何在Objective-C中找到最前面的window的名字? 提前致谢。
你没有说清楚,但我猜你想要最前面的 window 你自己的应用程序。我不知道这是否是最好的,但这对我有用:
- (NSWindow*) frontmostWindowP {
NSArray* windNumsArray = [NSWindow windowNumbersWithOptions:0];
NSInteger frontmostNumb = [[windNumsArray objectAtIndex:1] integerValue];
NSArray* windObjsArray = [[NSApplication sharedApplication] windows];
for ( NSWindow* windP in windObjsArray )
if ( [windP windowNumber] == frontmostNumb )
return windP;
return 0;
}
"[NSWindow windowNumbersWithOptions:0]" returns 一个任意整数数组 "windowNumbers",例如 [ 10, 313, 319, 384 ]。第一个数字对我来说总是“10”,而且是垃圾。我想它可能代表菜单栏或其他东西。无论如何,元素 1 到 N 是应用程序 windows 顺序的 window 编号,从前到后。这就是为什么“[windNumsArray objectAtIndex:1]”。问题是 "windowNumber" 在其他方面毫无用处,但我们已经完成了一半。
"[[NSApplication sharedApplication] windows]" returns 一个 windows 数组,这次是 NSWindow 指针, 不是 在任何可信赖的订单。所以剩下的就是找到具有正确 windowNumber 的 NSWindow*,因此是 for 循环。
如果您对 windows 除了自己的应用感兴趣,请查看 "NSWindow windowNumbersWithOptions" 的选项。
您可以使用 NSApplication 的 keyWindow 并访问 NSWindow.For 的标题 属性 访问其他应用程序的 window 您可以参考这个 Getting the main window of an app via an NSRunningApplication instance
"Apple has traditionally been pretty locked-down about this sort of thing. NSRunningApplication itself was just introduced in 10.6, and as you said, it's a bit limited. Depending on what you want to do, the answer might be in the Accessibility framework or it might be the CGWindow API. You can use the processIdentifier from the NSRunningApplication to match it up with those APIs."