代码之家  ›  专栏  ›  技术社区  ›  Ben Packard Ross Knipe

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

  •  5
  • Ben Packard Ross Knipe  · 技术社区  · 16 年前

    我想设置一个特定的窗口,从外部应用程序(例如textedit)到最前面。

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

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

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

    如果我感兴趣的窗口不是最前面的,我需要设置它。我想我可以用axuielement 集合 attributevalue可以做到这一点,但我没有取得任何成功。下面是我尝试的方法。

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

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

    谢谢。

    3 回复  |  直到 10 年前
        1
  •  8
  •   Peter Hosey    16 年前

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

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

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

    最前面的窗口的cocoa/mac os x名称是_主窗口_。(例如,参见 NSApplication's 和 NSWindow's methods 与该概念相关。)可访问性使用相同的名称,因此要使单个窗口最前面,请设置其值 kAXMainAttribute 到 kCFBooleanTrue .

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

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

        2
  •  3
  •   Keith Smiley    10 年前

    我可以在应用程序中完成这个任务 I worked on 将应用程序置于前端,然后将单个窗口置于前端。

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

    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)
    
        3
  •  1
  •   simonster    15 年前

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