在AppleScript中按应用程序名称(在函数内)还原窗口?

时间:2017-10-23 22:12:02

标签: applescript restore

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

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"即使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?

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

必须有另一种方法可以做到这一点,但我发现这样做的唯一例子是"告诉应用程序/集小型化windows"方法

2 个答案:

答案 0 :(得分:0)

使用系统事件访问应用程序进程窗口的属性以控制其小型化状态可能会取得更大成功。

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

虽然我没有尝试用您的方法诊断问题,但我确实起草了这种方法(尽管在MacOS 10.13上),这似乎证实了我所说的内容。希望脚本非常明显:

    use application "System Events"


    repeat with ProcessName in getProcessesWithMiniaturizedWindows()
        RestoreAllWindows for ProcessName
    end repeat


    on getProcessesWithMiniaturizedWindows()

        return name of (every process whose ¬
            windows's attribute "AXMinimized"'s value ¬
            contains true)

    end getProcessesWithMiniaturizedWindows


    on RestoreAllWindows for AppProcess as text
        local AppProcess

        set value of attribute "AXMinimized" of ¬
            (every window of process AppProcess whose ¬
                value of attribute "AXMinimized" is true) ¬
                to false

    end RestoreAllWindows

答案 1 :(得分:0)

简单直接的解决方案是:

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