AppleScript-最小化所有基于processName和windowTitle的窗口

时间:2020-07-24 11:45:11

标签: applescript

我试图遍历所有进程并获取每个进程主窗口的windowTitle。基于windowTitle,我要使进程Hide()。

tell application "System Events"
    repeat with aProcess in processes
        tell aProcess
           tell (1st window whose value of attribute "AXMain" is true)
              set windowTitle to value of attribute "AXTitle"
              if windowTitle is not "stackoverflow" then
                set visible of aProcess to false
              end if
           end tell
        end tell
    end repeat
end tell

我得到了

execution error: System Events got an error: Can’t get window 1 of item 1 of every process whose value of attribute "AXMain" = true. Invalid index. (-1719)

是因为所有进程都没有主窗口吗?

1 个答案:

答案 0 :(得分:1)

是因为所有进程都没有主窗口吗?

与不是所有具有窗口(任何类型)的进程有关。

tell application id "com.apple.systemevents" to tell (every process ¬
        whose front window's class = window and the front window's ¬
        title does not contain "foobar") to set visible to true

故障:

  • tell application id "com.apple.systemevents" to :将命令定向到系统事件。唯一的区别是,我通过应用程序包的标识符而不是其名称来引用该应用程序。并不是很重要,但是应用程序的捆绑标识符更改的可能性小于文件名。

  • tell (every process whose front window's class = window:这是一种检索具有至少一个窗口的进程列表的有效方法,因为它可以过滤出没有{{1 }}为class of its front window

  • missing value:已将进程过滤到仅包含窗口的应用程序,这进一步根据窗口的名称/标题对其进行了过滤。 and the front window's title does not contain "foobar"属性从title获取其值,但是访问一个attribute "AXTitle"对象的属性。在这里,我选择放松与窗口标题匹配时所施加的相等性,这样您就不必使用窗口的完整标题,而只需选择一个明智的选择即可。

  • attribute :隐藏先前返回的进程