5

我想将来自外部应用程序(例如 textedit)的某个窗口设置为最前面。

我可以使用 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);

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

谢谢。

4

3 回答 3

8

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

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

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

最前面窗口的 Cocoa/Mac OS X 名称是“主窗口”。(例如,与该概念相关的NSApplicationNSWindow 的方法。)可访问性使用相同的名称,因此要使单个窗口位于最前面,请将其值设置kAXMainAttributekCFBooleanTrue.

使应用程序最前面的方法类似:将其值设置kAXFrontmostAttributekCFBooleanTrue. 您需要同时执行这两项操作来设置应用程序的最前面的窗口并使应用程序处于活动状态。

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

于 2010-01-22T04:44:21.077 回答
5

我能够在我开发的应用程序中完成此操作,方法是将应用程序置于最前面,然后将单个窗口置于最前面。

首先将应用程序放在前面:

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)
于 2015-10-26T02:32:13.470 回答
1

我最近发现,如果你不介意一些 Carbon,你可以使用SetFrontProcessWithOptions将一个窗口带到前面,如果你将 kSetFrontProcessFrontWindowOnly 作为第二个参数传递给它焦点。

于 2011-07-22T02:11:09.623 回答