Ben*_*ard 5 cocoa accessibility macos-carbon
我想设置一个窗口,从外部应用程序(例如textedit),到最前面.
我可以使用GetFrontProcess成功获取对应用程序本身的引用,并检查它是否是最前面的.如果不是,我可以使用setFrontProcess使其聚焦.
然后,我可以使用辅助功能API来检查该应用程序下的所有窗口.我正在检查某个窗口是否存在,如果存在,我将它与应用程序的最前面窗口进行比较:
//get the front window of textEditApp and store it in 'currentFrontWindow'
AXUIElementCopyAttributeValue(textEditApp, kAXFocusedWindowAttribute, (CFTypeRef *)¤tFrontWindow);
Run Code Online (Sandbox Code Playgroud)
如果我感兴趣的窗口不是最前面的,我需要设置它.我认为我可以使用AXUIElement Set AttributeValue来做到这一点,但我没有取得任何成功.以下是我尝试过的方法.
//set the front window of textEditApp to be desiredFrontWindow
AXUIElementSetAttributeValue(textEditApp, kAXFocusedUIElementAttribute, desiredFrontWindow);
Run Code Online (Sandbox Code Playgroud)
我已检查窗口是否存在,并且应用程序已成功"切换到".但为什么这行代码没有将指定的窗口带到前面?
谢谢.
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.
使应用程序最前端的方法类似:将其值设置kAXFrontmostAttribute为kCFBooleanTrue.您需要同时执行这两项操作以设置应用程序的最前面的窗口并使应用程序处于活动状态.
据我所知,没有办法只将应用程序的一个窗口放在最前面并给它关注会话.
我能够在我开发的应用程序中完成此任务,方法是将应用程序置于最前面,然后将单个窗口置于最前面。
首先将应用程序放在前面:
let currentPid = NSRunningApplication.currentApplication().processIdentifier
if let pid = (the pid of the app in question) where pid != currentPid {
NSRunningApplication(processIdentifier: pid)?.activateWithOptions(.ActivateIgnoringOtherApps)
}
Run Code Online (Sandbox Code Playgroud)
然后将窗口发送到前面:
let window = (window AXUIElement)
AXUIElementSetAttributeValue(window, NSAccessibilityMainAttribute, true)
Run Code Online (Sandbox Code Playgroud)