使用applescript隐藏/最小化最大化的窗口

时间:2021-07-29 22:57:56

标签: applescript

我正在尝试编写一个applescript,它隐藏所有应用程序的所有窗口,但有一些例外。以下脚本在大多数情况下都有效,但如果应用程序有一个最大化的窗口,它不会被隐藏。

tell application "System Events"
   set showApps to {{name:"Finder"}, {name:"Chrome"}, {name:"Notion"}}
   set openApps to name of (application processes whose visible is true)

   repeat with x in openApps
       if showApps does not contain x then
           tell every window
               set isFullScreen to get value of attribute "AXFullScreen"
               if isFullScreen is true then
                   set value of attribute "AXFullScreen" to false
               end if
           end tell
           set visible of application process x to false
       end if
   end repeat
end tell

有人能发现这里的错误吗?有没有办法做到这一点?

2 个答案:

答案 0 :(得分:0)

您需要退出全屏模式,因为我认为在该模式下“窗口”不是窗口。尝试使用“密钥代码 53”——对于 esc。

这应该会让你退出全屏模式,然后常规方法应该可以工作。顺便说一下,大多数可编写脚本的应用程序的窗口都具有“小型化”属性。至少在 Safari 中,在将应用程序从 FS 模式中拉出后,它的表现似乎很糟糕,因此您可以进行设置。

因为我没有 Chrome,所以 Safari 可能看起来像这样 - 您可能只需将“Google Chrome”或“Chrome”替换为“Safari”,它就可以与该应用一起使用。

打开一个页面,然后运行脚本。浏览器激活后,单击全屏按钮。延迟期过后,它应该退出 FS 模式,然后最小化窗口。您可能需要处理最后几行。

tell application "Safari"
    activate
    delay 3
    tell application "System Events"
        tell application process "Safari"

            key code 53 -- esc
            
            delay 1
        end tell
    end tell
    
    set miniaturized of window 1 to false
    delay 0.4
    set miniaturized of window 1 to true
    
end tell

答案 1 :(得分:0)

你告诉 windows,但忘记了应用程序进程 x。所以,正确的告诉块应该是告诉(应用进程x的每个窗口)。但是根本不需要检查窗口状态。将进程的可见性设置为 false 无论如何都会关闭其所有窗口。所以,事情更简单了:

set showApps to {"Finder", "Chrome", "Notion"}

tell application "System Events"
    repeat with x in (get name of processes whose visible is true)
        if showApps does not contain x then set visible of process x to false
    end repeat
end tell