On macOS Ventura.
I plan on having this bound to a Keyboard Shrotcut via the Shortcuts App with a Shortcut that runs an Applescript file to achieve this function.
I was able to get ChatGPT to provide me this to work with:
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
tell application "System Events"
set frontmostProcess to first process where it is frontmost -- this will set frontmostProcess to the frontmost process
if background only of frontmostProcess is false then
tell frontmostProcess
set allWindows to every window
repeat with i from 1 to count allWindows
set thisWindow to item i of allWindows
set the bounds of thisWindow to screenResolution
end repeat
end tell
end if
end tell
This was throwing error: “execution error: System Events got an error: Can’t set bounds of UI element to any. (-10006)”
So it modified the script to just wrap it a try/catch block that skips a widow that throws an error when trying to be resized/maximized. Assuming that the window in question just did not have the ability to be resized, or some other issue.
tell application "Finder"
set screenResolution to bounds of window of desktop
end tell
tell application "System Events"
set frontmostProcess to first process where it is frontmost -- this will set frontmostProcess to the frontmost process
if background only of frontmostProcess is false then
tell frontmostProcess
set allWindows to every window
repeat with i from 1 to count allWindows
set thisWindow to item i of allWindows
try
set the bounds of thisWindow to screenResolution
on error
-- do nothing, just skip this window
end try
end repeat
end tell
end if
end tell
But now, nothing happens! It seems every single window, no matter what app I trigger the Shortcut on, seems to throw an error when it is tried to be resized in this way through Applescript.
I am guessing there is just a better/different way to resize each window of the current Application. Can anyone help me out? Thank you!
login202. The following script sets the active app to fullscreen mode:
tell application "System Events"
set activeApp to name of first process whose frontmost is true
try
tell process activeApp to set value of attribute "AXFullScreen" of every window to true
end try
end tell
The following script sets the position and size of the windows of the active app to that of the screen, although macOS apparently will not let you cover the menu bar:
tell application "Finder" to set theBounds to bounds of desktop's container window
tell application "System Events"
set activeApp to name of first process whose frontmost is true
try
tell process activeApp
set position of every window to {0, 0}
set size of every window to {item 3 of theBounds, item 4 of theBounds}
end tell
end try
end tell
In limited testing on my Ventura Mac, I was able to create a Shortcuts shortcut with the second script and to run it by way of a keyboard shortcut.
1 Like
Thank you peavine. This script did function, but it set the widows to fullscreen! Not exactly what I want, but it gives me something to work with, and I have saved it for if/when I do want to maybe work with setting windows to fullscreen.
Thank you Fredrik71. This script worked, for one window like your caveat stated. But I was able to modify it to use my previous script’s code to apply this function to all windows:
-- https://www.macscripter.net/t/maximize-all-windows-of-current-application/74781/3?u=login202
use framework "Foundation"
use framework "AppKit"
use scripting additions
set {screenPosition, screenSize} to (current application's NSScreen's mainScreen()'s frame())
tell application "System Events"
set frontmostProcess to first process where it is frontmost -- this will set frontmostProcess to the frontmost process
if background only of frontmostProcess is false then
tell frontmostProcess
set allWindows to every window
repeat with i from 1 to count allWindows
set thisWindow to item i of allWindows
try
tell thisWindow
set position to screenPosition
set size to screenSize
end tell
on error
-- do nothing, just skip this window
end try
end repeat
end tell
end if
end tell
Also. For others who may be interested and find this thread… I was able to get this to work using hammerspoon. Code snippet for that, using an anonymous function inside a keybind, below.
hs.hotkey.bind({"cmd", "alt", "ctrl", "shift"}, "M", function()
local curApp = hs.application.frontmostApplication()
local windows = curApp:allWindows()
for win = 1, #windows do
windows[win]:maximize()
end
end
Alcoolios. I supplied two scripts for you to test, and the second script appears to do exactly what you want. To verify this, simply open my second script in a script editor and run.
Fredrik71’s script works but uses ASObjC to get the screen size. This requires that two frameworks be loaded into memory, which can be slow. This is just a matter of personal opinion, but I would not use ASObjC to accomplish this simple task. The only exception is if ASObjC is used to accomplish other tasks, which seems unlikely.
As you have noticed, Fredrik71’s script only resizes one window. Your adaptation of the ChatGPT solution is to loop through and resize every window, which is unnecessary and relatively slow.
BTW, if this is just for your personal use, you may want to include in the script the desired window size, as in the following. This will be a bit faster than using Finder and, potentially, significantly faster than using ASObjC.
set thePosition to {0, 0} -- user set as desired
set theSize to {1920, 1080} -- user set as desired
tell application "System Events"
set activeApp to name of first process whose frontmost is true
try
tell process activeApp
set position of every window to thePosition
set size of every window to theSize
end tell
end try
end tell
My apologies peavine! I misunderstood your script(s) and added both to a single file ( I think since I have two sections in mine, one for getting the display size, and one to do the actual maximizing of all windows )
I have tested your script, as intended with just the section for maximizing all windows of the current window, and it works great! Thank you again!
I would like to hardcode the display size to increase the speed. But I do occasionally plug into an external display, and would like it to work there without having to modify the script each time.
1 Like
I happened to notice an oddity on my Ventura computer. If the Dock is right- or bottom-aligned, the above scripts will not extend an app’s window under the Dock and to the screen boundary . However, that’s not the case if the Dock is left-aligned. The following is a solution if that’s an issue, although it tends to be a bit slow:
use framework "AppKit"
use framework "Foundation"
set {x, y, w, h} to getVisibleFrame()
tell application "System Events"
set activeApp to name of first process whose frontmost is true
try
tell process activeApp
set position of every window to {x, y}
set size of every window to {w, h}
end tell
end try
end tell
on getVisibleFrame()
set theScreen to current application's NSScreen's mainScreen()
set {{x1, y1}, {w1, h1}} to theScreen's frame()
set {{x2, y2}, {w2, h2}} to theScreen's visibleFrame()
return {x2 as integer, (h1 - h2 - y2) as integer, w2 as integer, h2 as integer}
end getVisibleFrame
Also on my Ventura computer, System Events will not extend an app’s window past the screen bounds. So, the following might be considered as a simple solution just in general (note window size):
tell application "System Events"
set activeApp to name of first process whose frontmost is true
try
tell process activeApp
set position of every window to {0, 0}
set size of every window to {9999, 9999}
end tell
end try
end tell
2 Likes