首页 > 解决方案 > 如何获得具有键盘/鼠标焦点的 Mac OS X 应用程序的显示范围?

问题描述

我正在使用以下脚本来“最大化”我 Mac 上的当前应用程序窗口。

当最前面的应用程序窗口位于主/较大的外部显示器上时,或者当我的笔记本电脑未连接到外部显示器时,它可以正常工作。

但是,当我连接到外部显示器并且我试图最大化的窗口位于我的笔记本电脑显示器(不是外部主显示器)上时,此脚本最终会将窗口放大到更大显示器的尺寸。

try
    tell application "Finder" to set b to bounds of window of desktop
    try
        tell application (path to frontmost application as text)
            set bounds of window 1 to {item 1 of b, 22, item 3 of b, item 4 of b}
        end tell
    on error
        tell application "System Events" to tell window 1 of (process 1 where it is frontmost)
            try
                set position to {0, 22}
                set size to {item 3 of b, (item 4 of b) - 22}
            on error
                click (button 1 of window 1 where subrole is "AXZoomButton")
            end try
        end tell
    end try
end try

我相信问题是我得到想要的方式boundsb

bounds of window of desktop

这似乎总是返回主显示器的边界。

我需要一种方法来检测当前正在运行的应用程序的显示边界,该应用程序具有键盘和鼠标焦点。

注意:如果这个问题的解决方案是 Swift 而不是 Apple Script,我会非常酷。

标签: swiftmacosapplescript

解决方案


你有没有考虑过这个:

tell application (path to frontmost application as text)
    set zoomed of window 1 to true
end tell

推荐阅读