Search code examples
macosresizewindowapplescript

Resizing all windows of all running applications


I am attempting to write an applescript script that will allow me to resize all windows of all running applications whenever I choose (I currently use Stay, but find that it is glitchy at times, so I want to "re-invent" it)
I have been following some applescripting tutorials and have come up with the following code to do so, but it is buggy:

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"

The problem is that when this code encounters my only terminal window (with several open tabs), it error: 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}.

Changing tell application theProcess to tell application theProcessName doesn't help (same error), and neither does changing it to tell application "System Events" (Error: System Events got an error: Can’t make item 2 of every process into type integer.)

Interestingly, this works as expected:

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

So I'm very confused.
What am I doing wrong? How can I fix this?


Solution

  • 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
    
    • Took about 3 seconds on my Mac
    • Maximizes Terminal windows to fill the screen (except for the 4px area taken up by Dock)
    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
    
    • Took about 0.3 seconds on my Mac
    • Doesn't work with all applications like Preview or Reeder
    • Uses bundle identifiers because a few applications have different process and application names
    • Resizes Terminal windows so that they have a few pixels empty space above and below them

    I use this script to maximize windows:

    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
    

    In many applications that don't have basic AppleScript support the zoom button also maximizes windows to fill the screen.