Applescript为所有窗口设置窗口边界

时间:2013-02-23 08:04:00

标签: macos applescript

我修改了一段时间前发现的一些AppleScript,以便将最前面的窗口放置在某个位置,这样屏幕/底座/菜单栏边缘的每边大约有10px的边距:

set front_app to (path to frontmost application as Unicode text)

tell application "Finder"
    set _b to bounds of window of desktop
    set scrn_width to item 3 of _b
    set scrn_height to item 4 of _b
end tell

tell application front_app
    activate
    set bounds of window 1 to {10, 35, (scrnWidth - 10), (scrnHeight - 80)}
end tell

问题是我必须单独为每个窗口执行此操作。我想只运行一次并让它适用于每个应用程序的所有窗口。

我尝试修改了大约5个不同的脚本但只是出错了。这就是我所拥有的:

tell application "System Events"
    tell application "Finder"
        set _b to bounds of window of desktop
        set scrn_width to item 3 of _b
        set scrn_height to item 4 of _b
    end tell
    set _windows to get windows of (application processes whose visible is true)
    repeat with this_window in (items of _windows)
        set bounds of this_window to {10, 35, (scrn_width - 10), (scrn_height - 80)}
    end repeat
end tell

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

经过一番努力,这就是我想出来的。

tell application "System Events"
    set frontmostApps to every process whose frontmost is true
    if ((count of frontmostApps) = 0) then return
    set frontmostAppAlias to application file of (item 1 of frontmostApps)
end tell

tell application "Finder" to set desktopBounds to bounds of window of desktop

set screenWidth to item 3 of desktopBounds
set screenHeight to item 4 of desktopBounds

tell application (frontmostAppAlias as string)
    set resizableAppWindows to every window whose resizable is true
    repeat with i from 1 to (count of resizableAppWindows)
        set appWindow to item i of resizableAppWindows
        set bounds of appWindow to {10, 35, (screenWidth - 10), (screenHeight - 80)}
    end repeat
end tell

我最初开始尝试在tell app "System Events"区域内执行所有操作,但发现window的{​​{1}} s似乎不允许与正常application process相同的调用window 1}} s,即使从脚本定义他们应该。这导致tell直接阻止了应用程序本身。

您可能希望将set bounds包装在try块中,因为可能会有一些应用程序具有可调整大小的窗口,该窗口具有最大大小限制集,这可能会导致错误。< / p>

相关问题