Hi everybody,
I'd like to make me a button on my stream deck that maximises the currently active window, if not already maximised, and restores the previous window size if it is already maximised.
TL;DR
Is there a reliable way to get the width and height of the display that the currently active window is on, if there are external monitors and/or an iPad in sidecar mode attached to the computer?
Long version
Since I'm a JavaScript coder by trade, I'm using the “real JavaScript” action to make it happen, which is excellent, by the way!
My algorithm goes like this, in pseudo-code:
maximised_window_width = display_width - stage_manager_margin_width
maximised_window_height = display_height - menu_bar_height
if
active_window_width is not maximised_window_width or
active_window_height is not maximised_window_height
then
remember_window_width
maximise_active_window
exit_script
if
active_window_width is maximised_window_width and
active_window_height is maximised_window_height
then
restore_previous_size
exit_script
The algorithm calculates the size a maximised window has, based on the screen width and height, with stage manager margin on the left subtracted from the width, and menu bar height subtracted from the height.
If then checks if the currently active window matches that maximised window size.
If it doesn't, the algorithm assumes the window is not maximised. It “remembers” the current window size (i.e. stores it in variables) and maximises the active window.
If the active window size matches the calculated maximised window size, the algorithm assumes the active window is already maximised and restores its previous size.
Of course, the algorithm would also have to deal with X and Y coordinates of the active window, I've left that out here for simplicity's sake.
This is all nice and well, my actual JS code gets the screen dimensions by running the shell command system_profiler SPDisplaysDataType -json
and parsing its output. It gets the active window's width, height and X/Y coordinates by running an AppleScript and parsing its output.
However, the issues start with a multi monitor setup. I usually have two monitors attached to my MacBook, one big Dell widescreen display and an iPad in Sidecar-Mode.
I need to get the size of the display that the currently active window is located on for my script to work properly in multi-monitor mode.
I try to do this with this JavaScript / AppleScript:
async function getWindowAndDesktopInfo() {
const appleScript = `
tell application "System Events"
set appOfInterest to name of application processes whose frontmost is true
set currentApplication to item 1 of appOfInterest
set firstWindow to the first window of application process currentApplication
set windowDimensions to size of firstWindow
set windowPosition to position of firstWindow
end tell
tell application "Finder"
set screenData to bounds of window of desktop
end tell
return "[" & (item 1 of windowPosition as text) & "," & (item 2 of windowPosition as text) & "," & (item 1 of windowDimensions as text) & "," & (item 2 of windowDimensions as text) & "," & (item 1 of screenData as text) & "," & (item 2 of screenData as text) & "," & (item 3 of screenData as text) & "," & (item 4 of screenData as text) & "]"
`;
const resultStr = await runAppleScript(appleScript);
const result = JSON.parse(resultStr);
const [
winPosX,
winPosY,
winWidth,
winHeight,
deskPosX,
deskPosY,
deskWidth,
deskHeight
] = result.map(str => parseInt(str, 10));
return {
winPosX,
winPosY,
winWidth,
winHeight,
deskPosX,
deskPosY,
deskWidth,
deskHeight
};
}
The variables deskWidth
and deskHeight
, I was hoping, would give me the dimensions of the display that the active window is on. Alas, that's not the case. On my multi-monitor-setup, I get the width and height of my displays, added up – kind of, the iPad is added in a way I don't really understand. And the position coordinates I don't get, either…
On my setup, I get:
desktop_position_x = -1366
desktop_position_y = -1440
desktop_width = 2647
desktop_height = 1117
These values are dependent on which display is the main display. In the above example, it's the Mac's built in screen, which is located below the big Dell monitor. If I change the main display to be the Dell monitor, this is what I get:
desktop_position_x = -573
desktop_position_y = 0
desktop_width = 3440
desktop_height = 2557
What I'd really need is a way to find out which of my three displays the currently active window, then I could easily determine the size a maximised window would have, and thus determine if the currently active window is currently maximised.
Is there something I've overlooked here?