Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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