Skip to content

Instantly share code, notes, and snippets.

@Dru89
Last active March 7, 2023 20:52
Show Gist options
  • Save Dru89/a2621529607e2ac7b207fa8c815d48b5 to your computer and use it in GitHub Desktop.
Save Dru89/a2621529607e2ac7b207fa8c815d48b5 to your computer and use it in GitHub Desktop.
-- Return the absolute value of a number.
-- e.g. `abs(-14)` returns `14`, `abs(7)` returns 7.
on abs(theNumber)
if theNumber is less than 0 then return -theNumber
return theNumber
end abs
-- Splits `theString` by a provided delimiter.
-- e.g. `split("4:3", ":")` returns {"4", "3"}
on split(theString, theDelimiter)
set theOldDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to every text item of theString
set AppleScript's text item delimiters to theOldDelimiter
return theArray
end split
-- Calculate the best new size for a window given
-- 1. its current size
-- 2. the desired ratio
-- 3. and a minimum allowed size.
on calculateBestWindowSize(theSize, theRatio, minSize)
-- Get whatever the current sizes are.
set theOldWidth to the first item of theSize
set theOldHeight to the second item of theSize
-- New width would be height * ratio
set theNewWidth to theOldHeight * theRatio
-- New height would be width / ratio.
set theNewHeight to theOldWidth / theRatio
-- If the new height is smaller than the minimum size, adjust the width instead.
-- If the new width is smaller than the minimum size, adjust the height instead.
if theNewHeight < minSize then
return {theNewWidth, theOldHeight}
else if theNewWidth < minSize then
return {theOldWidth, theNewHeight}
end if
set theHeightDifference to abs((theOldHeight - theNewHeight) / theOldHeight)
set theWidthDifference to abs((theOldWidth - theNewWidth) / theOldWidth)
-- Otherwise, set the size to whatever would cause the smallest change
-- to the overall window size.
if theHeightDifference < theWidthDifference then
return {theOldWidth, theNewHeight}
else
return {theNewWidth, theOldHeight}
end if
end calculateBestWindowSize
-- Converts a ratio string like "4:3" into a number like 1.3333333
on getRatioFrom(theRatioString)
set theRatioArray to split(theRatioString, ":")
set theWidth to the first item of theRatioArray
set theHeight to the second item of theRatioArray
return theWidth / theHeight
end getRatioFrom
-- Set the dimensions of the frontmost window to a given ratio.
-- The new window size will be calculated like this:
-- 1. If the new height would be less than `theMinSize`, then we adjust the width.
-- 2. If the new widthw would be less than `theMinSize`, then we adjust the height.
-- 3. Otherwise, we adjust whichever dimension would change the window size less.
on setFrontmostWindowTo(theRatioString, theMinSize)
set theRatio to getRatioFrom(theRatioString)
-- Grab the frontmost window
tell application "System Events" to ¬
tell (its first application process whose frontmost is true) to ¬
tell its first window
-- Update its size.
set its size to my calculateBestWindowSize(its size, theRatio, theMinSize)
end tell
end setFrontmostWindowTo
setFrontmostWindowTo("1:1", 250)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment