Skip to content

Instantly share code, notes, and snippets.

@GeekAndDad
Last active November 18, 2021 05:15
Show Gist options
  • Save GeekAndDad/64751f303d367eaa5c572e655e15a44a to your computer and use it in GitHub Desktop.
Save GeekAndDad/64751f303d367eaa5c572e655e15a44a to your computer and use it in GitHub Desktop.
Code for a blog post about a silly AppleScript to move Safari windows back up after a bug in Safari moves them down on wake from sleep sometimes. The post is here: https://geekanddad.wordpress.com/?p=2161. This script moves all Safari windows up the same amount to keep their relative positioning and moves them up as much as possible while still …
-- Put in the public domain with zero warranty of any kind
--
-- only tested on macOS 11.6, with Safari 13.0; ymmv
use scripting additions
tell application "Safari"
set windlist to windows
log "Examining " & length of windlist & " windows..."
set lowestYWindow to first item of windlist
set lowestYValue to get second item of (get bounds of lowestYWindow)
repeat with wind in windlist
-- log (get name of wind)
-- log (get bounds of wind)
set curValue to second item of (get bounds of wind)
if curValue < lowestYValue then
copy wind to lowestYWindow
set lowestYValue to curValue
end if
end repeat
-- subtract out the title bar (hard coded - bad dog!)
set yOffset to lowestYValue - 25
if yOffset > 0 then
log "moving " & length of windlist & " windows up " & yOffset & " pixels"
repeat with aWind in windlist
set aBounds to (get the bounds of aWind)
-- debugging - log the windows we're about to move's starting bounds
-- log {"Window ", (get the name of aWind)}
-- log {"Starting bounds: { ", aBounds, " } "}
set the second item of aBounds to ((get the second item of aBounds as integer) - yOffset)
set the fourth item of aBounds to ((get the fourth item of aBounds as integer) - yOffset)
set the bounds of aWind to aBounds
-- debbugging - then log where we put it
-- log {"Modified bounds of window: { ", (get the bounds of aWind), " } "}
end repeat
else
display alert ("The highest window is already at the top! Not moving any windows!")
end if
log "done"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment