使用辅助功能 API 设置最前面的窗口

发布于 2024-08-18 08:46:26 字数 795 浏览 8 评论 0 原文

我想将外部应用程序(例如文本编辑)中的某个窗口设置为最前面。

我可以使用 GetFrontProcess 成功获取对应用程序本身的引用,并检查它是否位于最前面。如果不是,我可以使用 setFrontProcess 使其聚焦。

然后,我可以使用辅助功能 API 来检查该应用程序下的所有窗口。我正在检查某个窗口是否存在,如果存在,我将其与应用程序最前面的窗口进行比较:

//get the front window of textEditApp and store it in 'currentFrontWindow'
    AXUIElementCopyAttributeValue(textEditApp, kAXFocusedWindowAttribute, (CFTypeRef *)&currentFrontWindow);

如果我感兴趣的窗口不是最前面的,我需要将其设置为最前面。我认为我可以使用 AXUIElement Set AttributeValue 来执行此操作,但我没有取得任何成功。以下是我尝试做的方法。

//set the front window of textEditApp to be desiredFrontWindow
AXUIElementSetAttributeValue(textEditApp, kAXFocusedUIElementAttribute, desiredFrontWindow);

我已检查该窗口是否存在,并且应用程序已成功“切换”。但是为什么这行代码没有将指定的窗口带到前面呢?

谢谢。

I want to set a certain window, from an external app (for example textedit), to be front most.

I can successfully get a reference to the app itself using GetFrontProcess, and check whether it is front most. If it is not, I can use setFrontProcess to make it focussed.

I can then use the the accessibility API to examine all the windows under that application. I am checking that a certain window exists, and if so I compare it against the front most window of the application:

//get the front window of textEditApp and store it in 'currentFrontWindow'
    AXUIElementCopyAttributeValue(textEditApp, kAXFocusedWindowAttribute, (CFTypeRef *)¤tFrontWindow);

If the window I am interested in is not frontmost, I need to set it so. I thought that I could use AXUIElement Set AttributeValue to do this but I am not getting any success. Below is how I have tried to do it.

//set the front window of textEditApp to be desiredFrontWindow
AXUIElementSetAttributeValue(textEditApp, kAXFocusedUIElementAttribute, desiredFrontWindow);

I have checked that the window exists, and the application is 'switched to' successfully. But why doesn't this line of code bring the specified window to the front?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-08-25 08:46:26

但是为什么这行代码没有将指定的窗口带到前面呢?

因为您尝试设置只读属性。

为了使窗口位于最前面,您需要设置窗口的适当属性。对于应用程序也是如此:要使应用程序位于最前面,您需要设置应用程序的适当属性。

最前面窗口的 Cocoa/Mac OS X 名称是“主窗口”。 (例如,请参见 NSApplication 的与该概念相关的 NSWindow 方法。)辅助功能使用相同的名称,因此要使单个窗口位于最前面,请设置其kAXMainAttribute 的值为kCFBooleanTrue

使应用程序位于最前面的方法类似:将其kAXFrontmostAttribute 的值设置为kCFBooleanTrue。您需要执行这两项操作才能设置应用程序最前面的窗口并使应用程序处于活动状态。

据我所知,没有办法仅将应用程序的单个窗口置于最前面并为其提供会话焦点。

But why doesn't this line of code bring the specified window to the front?

Because you tried to set a read-only attribute.

In order to make a window frontmost, you need to set the appropriate property of the window. The same goes for an application: To make an application frontmost, you need to set the appropriate property of the application.

The Cocoa/Mac OS X name for the frontmost window is “the main window”. (See, for example, NSApplication's and NSWindow's methods relating to that concept.) Accessibility uses the same name, so to make a single window frontmost, set the value of its kAXMainAttribute to kCFBooleanTrue.

The way to make the application frontmost is similar: Set the value of its kAXFrontmostAttribute to kCFBooleanTrue. You'll need to do both of these to both set the frontmost window of the application and make the application active.

As far as I can tell, there is no way to bring only a single window of an application frontmost and give it session focus.

烟酒忠诚 2024-08-25 08:46:26

我能够在应用程序我开发的中完成此任务,方法是将应用程序放在前面,然后将前面有一个窗口。

首先将应用程序置于最前面:

let currentPid = NSRunningApplication.currentApplication().processIdentifier
if let pid = (the pid of the app in question) where pid != currentPid {
  NSRunningApplication(processIdentifier: pid)?.activateWithOptions(.ActivateIgnoringOtherApps)
}

然后将窗口发送到最前面:

let window = (window AXUIElement)
AXUIElementSetAttributeValue(window, NSAccessibilityMainAttribute, true)

I was able to accomplish this in an app I worked on by bringing the application to the front, then bringing a single window to the front.

First bringing the app to the front:

let currentPid = NSRunningApplication.currentApplication().processIdentifier
if let pid = (the pid of the app in question) where pid != currentPid {
  NSRunningApplication(processIdentifier: pid)?.activateWithOptions(.ActivateIgnoringOtherApps)
}

Then sending the window to the front:

let window = (window AXUIElement)
AXUIElementSetAttributeValue(window, NSAccessibilityMainAttribute, true)
别想她 2024-08-25 08:46:26

我最近发现,如果你不介意一些 Carbon,你可以使用 SetFrontProcessWithOptions 将窗口置于前面并为其提供焦点。

I recently discovered that, if you don't mind some Carbon, you can use SetFrontProcessWithOptions to bring a window to the front and give it focus if you pass kSetFrontProcessFrontWindowOnly as the second argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文