Skip to content

Instantly share code, notes, and snippets.

@netj
Created June 10, 2012 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netj/2903884 to your computer and use it in GitHub Desktop.
Save netj/2903884 to your computer and use it in GitHub Desktop.
A piece of AppleScript for changing the size and position of windows
-- moveAndResize -- a piece of AppleScript for changing the size and position of windows
-- Author: Jaeho Shin <netj@sparcs.org>
-- Created: 2012-06-09
(*
Running the following line will move and resize all windows of that application:
tell application "Finder" to my moveAndResize(x,y, windows, w,h)
For x,y,w,h, you can give:
* null, to leave it unchanged, or
* a number greater than 1 which is the actual number in pixels, or
* a ratio value ranging between 0 and 1, which will then
reposition (or resize) relative to the remaining space (or entire screen size, respectively).
*)
to moveAndResize(newX, newY, wins, newW, newH)
set {screenWidth, screenHeight} to screenSize
set {marginH, marginV} to screenMargin
set {offsetX, offsetY} to newPosition
repeat with win in wins
-- see where window win is and how large it is
try
set {origX, origY, origX2, origY2} to bounds of win
set {origW, origH} to {origX2 - origX, origY2 - origY}
on error
tell application "System Events"
set {origX, origY} to get position of win
set {origW, origH} to get size of win
end tell
end try
set effWidth to screenWidth - marginH
set effHeight to screenHeight - marginV
-- first, figure out width and height
set _w to origW
if newW is not null then
if newW ≤ 1 then -- scale
set _w to newW * effWidth
else
set _w to newW
end if
end if
set _h to origH
if newH is not null then
if newH ≤ 1 then
set _h to newH * effHeight
else
set _h to newH
end if
end if
-- then, the position
set _x to origX
if newX is not null then
if newX ≤ 1 then
set _x to offsetX + newX * (effWidth - _w)
else
set _x to newX
end if
end if
set _y to origY
if newY is not null then
if newY ≤ 1 then
set _y to offsetY + newY * (effHeight - _h)
else
set _y to newY
end if
end if
-- change the size and position of the window
try
set bounds of win to {_x, _y, _x + _w, _y + _h}
on error
tell application "System Events"
set position of win to {_x, _y}
set size of win to {_w, _h}
end tell
end try
end repeat
end moveAndResize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment