Skip to content

Instantly share code, notes, and snippets.

@tylerjohnst
Last active July 26, 2022 17:49
Show Gist options
  • Save tylerjohnst/f21e427529eb50b462e0c350fa2df76d to your computer and use it in GitHub Desktop.
Save tylerjohnst/f21e427529eb50b462e0c350fa2df76d to your computer and use it in GitHub Desktop.
Ruby script that resizes the height of a chrome window by a fixed amount of pixels. Useful for debugging CSS/JS animation issues.
require 'shellwords'
def tell_chrome_to(*commands)
tell(%Q(application "System Events" to tell application process "Google Chrome"), *commands)
end
def tell(target, *commands)
["tell #{target}", *commands.flatten, 'end tell']
end
def osascript(*commands)
commands = commands.flatten.map(&Shellwords.method(:escape))
arguments = (["-e"] * commands.length).zip(commands).flatten.join(' ')
`osascript #{arguments}`.chomp
end
def desired_pixels
input = gets.strip
if input.match?(/q/)
exit 0
elsif input == ""
input = 1
else
input = input.to_i
end
end
begin
dimensions = osascript tell_chrome_to("get size of window 1")
width, height = dimensions.split(",").map(&:to_i)
while true do
print "Reduce window height by how many pixels? (1): "
height -= desired_pixels
puts "Resizing to height: #{height}"
osascript tell_chrome_to(tell("window 1", "set {size} to {{#{width}, #{height}}}"))
end
rescue SystemExit, Interrupt
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment