【问题标题】:Applescript - List all hidden Window in the dock - miniaturizedApplescript - 列出扩展坞中的所有隐藏窗口 - 小型化
【发布时间】:2012-04-15 15:49:56
【问题描述】:

我的 applescript 程序需要一些帮助。

我想列出所有打开的和在 Dock 中的窗口,所以我尝试了这个:

tell application "System Events"
set procs to processes
set windowInDock to {}
repeat with proc in procs
    try
        if exists (window 1 of proc) then
            repeat with w in (every window of proc whose miniaturized is true)
                copy a & w's miniaturized to the end of windowInDock
            end repeat
        end if
    end try -- ignore errors
end repeat
end tell
return windowInDock

但它返回空数组。

我尝试列出所有窗口并获取小型化参数(w 已小型化),但它不起作用。

你有什么想法吗?

谢谢!

【问题讨论】:

  • 1) 如果您没有收到任何回复,请不要duplicate your question。相反,您可以编辑您的问题,将其放到 active 选项卡的顶部。 2) exists 属性仅用于别名引用。 3) 你在copy a & w's miniaturized to the end of windowInDock 行有一个语法错误(变量“a”没有定义),所以你的代码甚至不应该运行。如果您需要进一步的帮助,请询问。 :)
  • 好的,谢谢! a 是分隔符。我忘记了复制/粘贴中的声明。所以a 是这样声明的set a to "@@##@@"。我需要知道我的重复循环中的w 是否在扩展坞中。我没有找到任何解决方案,你能帮帮我吗?
  • 看看 Red_Menace 的回答。他似乎比我更流利地使用 AppleScript,嘿(+1 对他的回答)。

标签: macos applescript


【解决方案1】:

exists 命令适用于任何对象,但 System Events 中的窗口属性不同于应用程序中的窗口属性(例如,没有小型化财产)。如果您不忽略所有错误,您会看到错误 - 将一堆代码包装在 try 语句中而至少不记录错误只是要求它。

您可以做的是获取应用程序列表,然后让它们获取有关其窗口的信息。不知道你打算如何处理真实值列表,所以在我的示例中,我只使用了窗口索引:

tell application "System Events" to set theNames to name of processes whose background only is false
set windowsInDock to {}
repeat with appName in theNames
    tell application appName to repeat with aWindow in (get windows whose miniaturized is true)
        tell aWindow to try
            get it's properties -- see event log
            set the end of windowsInDock to "window " & it's index & " of application " & quoted form of appName
        on error errmess -- ignore errors
            log errmess
        end try
    end repeat
end repeat

choose from list windowsInDock with prompt "Miniaturized windows:" with empty selection allowed

【讨论】:

  • 这是哪个系统/AppleScript 版本?在我的 OS X 10.6.8 + AppleScript 2.1.2 上,window 没有 miniaturizedindex 属性。
  • 我正在运行 Lion,但脚本示例也在 Snow Leopard 中运行。当然,这取决于在任何给定应用程序中实现的内容(例如 BBedit 窗口没有小型化属性),以及应用程序是否可编写脚本(预览版)。我刚刚更改了 OP 的脚本以将属性从系统事件中移开,但没有进行太多错误检查,因此如果某些没有小型化属性,看起来获取窗口将失败(我只是测试了一些默认应用程序)。
【解决方案2】:

这是一个适用于所有应用程序的脚本,至少在我的系统上是这样。

试试这个

set windowsInDock to {}
tell application "System Events"
    repeat with this_app in (get processes whose background only is false and windows is not {}) --get applications with 1+ window
        set windowsInDock to windowsInDock & (get windows of this_app whose value of its attribute "AXMinimized" is true)
    end repeat
end tell
return windowsInDock

【讨论】:

  • 请注意,这需要在系统偏好设置中启用辅助设备访问权限。
  • 以上代码在小牛(10.9.2)中无法运行:System Events got an error: Can’t get every process whose background only = false and every window ≠ {}.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 2011-09-25
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多