Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 使用辅助功能API设置最前面的窗口_Cocoa_Accessibility_Macos Carbon - Fatal编程技术网

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

Cocoa 使用辅助功能API设置最前面的窗口,cocoa,accessibility,macos-carbon,Cocoa,Accessibility,Macos Carbon,我想从外部应用程序(例如textedit)将某个窗口设置为最前端 我可以使用GetFrontProcess成功获取对应用程序本身的引用,并检查它是否位于最前端。如果不是,我可以使用setFrontProcess使其聚焦 然后,我可以使用可访问性API检查该应用程序下的所有窗口。我正在检查某个窗口是否存在,如果存在,我将其与应用程序最前面的窗口进行比较: //get the front window of textEditApp and store it in 'currentFrontWindo

我想从外部应用程序(例如textedit)将某个窗口设置为最前端

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

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

//get the front window of textEditApp and store it in 'currentFrontWindow'
    AXUIElementCopyAttributeValue(textEditApp, kAXFocusedWindowAttribute, (CFTypeRef *)&currentFrontWindow);
如果我感兴趣的窗口不是最前面的,我需要将其设置为最前面的。我原以为我可以使用AXUIElementSetAttributeValue来实现这一点,但我没有取得任何成功。下面是我如何尝试做到这一点

//set the front window of textEditApp to be desiredFrontWindow
AXUIElementSetAttributeValue(textEditApp, kAXFocusedUIElementAttribute, desiredFrontWindow);
我已检查窗口是否存在,并且应用程序已成功“切换到”。但是为什么这行代码不把指定的窗口放在前面呢

谢谢

但是为什么这行代码不把指定的窗口放在前面呢

因为您试图设置只读属性

要使窗口位于最前面,需要设置窗口的相应属性。应用程序也是如此:要使应用程序位于最前端,需要设置应用程序的相应属性

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

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


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

我最近发现,如果你不介意一些碳,如果将kSetFrontProcessFrontWindowOnly作为第二个参数传递,则可以使用将窗口置于前端并使其聚焦。

我能够在应用程序中实现这一点,方法是将应用程序置于前端,然后将单个窗口置于前端

首先,将应用程序推向前台:

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)
然后将窗口发送到前面:

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)

(再次)感谢彼得-这是一种魅力。至于应用程序,我一直在使用setFrontProcess而不是KaxFrontMoStatAttribute——这是个坏主意吗?在浏览了应用程序和窗口之后,有一个AXUIElementRef我很感兴趣,我想把它放在最前面(即让它成为键盘焦点)。我可以在元素的各个层中找到它的引用,但是如何设置它的焦点呢?元素是否有kAXMainAttribute的等价物?也许我应该发布另一个对我有用的问题。谢谢。我认为在现代的swift中,这看起来像AXUIElementSetAttributeValue(窗口,kAXMainWindowAttribute为CFString,true为CFTypeRef)