4

我正在尝试编写一个 applescript 脚本,它允许我在选择时调整所有正在运行的应用程序的所有窗口的大小(我目前使用Stay,但发现它有时会出现故障,所以我想“重新发明”它)
我一直在关注一些applescripting教程,并提出了以下代码来做到这一点,但它有问题:

tell application "Finder"
    set rect to bounds of window of desktop
end tell

property excludedApplicationNames : {"Finder"}
tell application "System Events"
    say "a"
    repeat with theProcess in processes
        say "b"
        if background only of theProcess is false then
            say "c"
            set theProcessName to name of theProcess as string
            if theProcessName is not in excludedApplicationNames then
                say theProcessName
                tell application theProcess
                    set bounds of windows of process theProcess to rect
                end tell
            end if
        end if
    end repeat
end tell
say "done"

问题是当这段代码遇到我唯一的终端窗口(有几个打开的选项卡)时,它会出错:System Events got an error: Can’t set application (item 2 of every process) to {0, 0, 1280, 900}.System Events got an error: Can’t set application (item 2 of every process) to {0, 0, 1280, 900}.

更改tell application theProcesstell application theProcessName没有帮助(同样的错误),也没有将其更改为tell application "System Events"(错误System Events got an error: Can’t make item 2 of every process into type integer.:)

有趣的是,这按预期工作:

tell application "Finder"
    set rect to bounds of window of desktop
end tell

tell application "Terminal"
    repeat with theWindow in windows
        set bounds of theWindow to rect
    end repeat
end tell

所以我很困惑。
我究竟做错了什么?我怎样才能解决这个问题?

4

3 回答 3

6
tell application "Finder"
    set {0, 0, dtw, dth} to bounds of window of desktop
end tell
tell application "System Events"
    repeat with p in (processes where background only is false)
        tell p
            if name is not in {"Finder"} then
                set position of windows to {0, 0}
                set size of windows to {dtw, dth}
            end if
        end tell
    end repeat
end tell
  • 在我的 Mac 上花了大约 3 秒
  • 最大化终端窗口以填满屏幕(Dock 占用的 4px 区域除外)
tell application "Finder"
    set dtb to bounds of window of desktop
end tell
tell application "System Events"
    bundle identifier of processes where background only is false
end tell
repeat with bid in result
    tell application id bid
        try
            if name is not in {"Finder"} then
                set (bounds of windows where visible is true) to dtb
            end if
        end try
    end tell
end repeat
  • 在我的 Mac 上花了大约 0.3 秒
  • 不适用于 Preview 或 Reeder 等所有应用程序
  • 使用捆绑标识符,因为一些应用程序具有不同的进程和应用程序名称
  • 调整终端窗口的大小,使其上方和下方有几个像素的空白空间

我使用这个脚本来最大化窗口:

try
    tell application "Finder" to set dtb to bounds of window of desktop
    tell application (path to frontmost application as text)
        if name is in {"Terminal"} then
            error
        else
            set bounds of window 1 to dtb
        end if
    end tell
on error
    tell application "System Events" to tell (process 1 where it is frontmost)
        try
            click (button 1 of window 1 where subrole is "AXZoomButton")
        end try
    end tell
end try

在许多没有基本 AppleScript 支持的应用程序中,缩放按钮还可以最大化窗口以填满屏幕。

于 2013-01-30T08:12:48.560 回答
1

这个考虑了扩展坞的大小。我在显示器的右侧有我的,但它应该很容易修改以适应底部的扩展坞。

tell application "Finder"
    set dtb to bounds of window of desktop
end tell

tell application "System Events" to tell process "Dock"
    set dockDimentions to size in list 1
    set dockWidth to item 1 of dockDimentions
end tell

tell application "System Events"
    bundle identifier of processes where background only is false
end tell

repeat with bid in result
    tell application id bid
        try
            if name is not in {"Finder", "System Preferences", "Notepad", "Terminal", "Activity Monitor"} then
                set x to item 1 of dtb
                set y to item 2 of dtb
                set w to (item 3 of dtb) - dockWidth
                set h to item 4 of dtb
                set (bounds of windows) to {x, y, w, h}
            end if
        end try
    end tell
end repeat
于 2014-11-24T06:34:04.943 回答
0

这最终对我有用:

property blacklist : {"Finder", "Preview", "Console", "AppleScript Editor", "Spotify", "TaskCoach"}
property buttonApps : {"LyX", "Eclipse"}
property buttonMaps : {{name:"LyX", Button:1, pname:"lyx"}, {name:"Eclipse", Button:2, pname:"eclipse"}}

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

tell application "System Events"
    set bids to bundle identifier of processes where background only is false
end tell

repeat with bid in bids
    tell application id bid
        if name is not in blacklist then
            set appName to name as string
            if name is "Terminal" then
                set newBounds to {0, 0, (item 3 of theBounds) - 10, item 4 of theBounds}
                repeat with theWindow in windows
                    if visible of theWindow is true then
                        say appName
                        set bounds of theWindow to newBounds
                    end if
                end repeat
            else if name is not in buttonApps then
                repeat with theWindow in windows
                    if visible of theWindow is true then
                        set bounds of theWindow to theBounds
                    end if
                end repeat
            else if name is in buttonApps then
                -- get the buttonNumber
                repeat with buttonApp in buttonMaps
                    if (name of buttonApp as string) is appName then
                        set theButton to Button of buttonApp
                    end if
                end repeat
                tell application "System Events"
                    repeat with theProcess in (processes where bundle identifier is bid)
                        try
                            tell theProcess to tell window 1 to click button theButton
                        end try
                    end repeat
                end tell
            end if
        end if
    end tell
end repeat

请注意,“Spotify”和“Task Coach”被列入黑名单,因为我无法通过以下方式调整它们的大小:

  1. 设置窗口边界
  2. 点击绿色按钮
  3. 单击菜单栏中的“窗口”>“缩放”
  4. 使用⌘</kbd>F10 shortcut that I had mapped it to.

如果有人能够提出更好的解决方案,我会全力以赴

于 2013-01-30T20:44:05.797 回答