【问题标题】:Restore windows by application name (within a function) in AppleScript?在AppleScript中按应用程序名称(在函数内)恢复窗口?
【发布时间】:2017-10-23 22:12:02
【问题描述】:

我有一个传递应用程序名称的函数。在函数中,我想做的一件事是恢复应用程序的窗口:

on test(applicationName)
    -- do some work
    -- restore all windows
    -- do some more work
end test

我找到了有关如何通过设置小型化属性 ala 来恢复应用程序窗口的参考:

tell application "Maps"
   set miniaturized of windows to false
end tell

(见Un-minimizing an app with Applescript

但这需要在编译时指定应用程序的名称 - 我必须将应用程序的名称硬编码到代码中 - 即使 applicationName 是一个字符串,我也不能使用“告诉应用程序应用程序名称”:

on test(applicationName)
    -- do some work

    -- restore all windows
    tell application applicationName
        set miniaturized of windows to false
    end tell

    --- do some more work
end test

(见tell application - string vs. string?

当我将应用程序的名称作为变量引用时,是否可以恢复应用程序的窗口?

必须有另一种方法来做到这一点,但我发现的唯一例子是“告诉应用程序/设置窗口小型化”方法。

【问题讨论】:

  • 你运行的是什么版本的 OS X/macOS?
  • 塞拉 (10.12.6)
  • 除编码问题外,请注意并非所有应用程序都可编写脚本(具有 AppleScript 字典),并且并非所有可编写脚本的应用程序都具有windows 元素。跨度>
  • 感谢您的提示,瓦迪安! (我是一个 AppleScript NooB)

标签: applescript restore


【解决方案1】:

使用 System Events 访问应用程序进程窗口的属性以控制其小型化状态可能会更成功。

与尝试通过应用程序对象本身执行此操作不同,相关应用程序不需要支持 AppleScript。我相信在 System Events 下运行的所有包含窗口的进程都有一组可通过 AppleScript 访问的属性,包括一个名为 AXMiniaturized 的属性,其值为 @987654324 @ 或false

虽然我没有尝试用你的方法诊断问题,但我确实起草了这个方法(尽管是在 MacOS 10.13 上),这似乎证实了我所说的。希望脚本是不言自明的:

use application id "com.apple.systemevents"

to getProcessesWithMiniaturizedWindows()
    return the name of (every process whose value of ¬
        attribute "AXMinimized" of every window ¬
        contains true)
end getProcessesWithMiniaturizedWindows

to restoreAllWindowsForProcess:(procName as text)
    local procName

    set value of attribute "AXMinimized" of (every window ¬
        of the process named procName whose value of ¬
        attribute "AXMinimized" = true) to false
end restoreAllWindowsForProcess:

on run
    repeat with processName in getProcessesWithMiniaturizedWindows()
        restoreAllWindowsForProcess_(processName)
    end repeat
end run

注意。您可能需要在系统偏好设置>安全和隐私>隐私>辅助功能中为System Events授予辅助访问权限(高山脉)。

【讨论】:

    【解决方案2】:

    简单直接的解决方案是这样的:

    on test(applicationName)
        -- do some work
    
        -- restore all windows
        tell application "System Events"
            tell process applicationName
                tell every window
                    set value of attribute "AXMinimized" to false
                end tell
            end tell
        end tell
    
        --- do some more work
    end test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      相关资源
      最近更新 更多