Skip to content

Instantly share code, notes, and snippets.

@dvessel
Created August 13, 2012 00:37
Show Gist options
  • Save dvessel/3335837 to your computer and use it in GitHub Desktop.
Save dvessel/3335837 to your computer and use it in GitHub Desktop.
AppleScript for controlling the front window. I use this with LaunchBar.
-- Sets Window size and position for the front most window.
-- Multi-monitor aware, takes arguments.
-- usage: [ center | left | right | full ], [ width x height ]
-- Default Options
property dPosition : "center"
property dWidth : 1066
property dHeight : 1300
-- Limit Dimensions
property minWidth : 300
property minHeight : 300
on run
my setPosition({pos:dPosition, w:dWidth, h:dHeight})
end run
on handle_string(args)
try
-- Use current window dimensions by default.
tell application id "com.apple.systemevents" to ¬
set {_width, _height} to ¬
size of (front window of (first application process where frontmost = true))
set AppleScript's text item delimiters to ","
set _position to text item 1 of args
try
set dimensions to text item 2 of args
set AppleScript's text item delimiters to "x"
try
set _width to text item 1 of dimensions as number
end try
try
set _height to text item 2 of dimensions as number
end try
end try
set AppleScript's text item delimiters to ""
my setPosition({pos:_position, w:_width, h:_height})
on error errMsg number errorNumber
tell application (path to frontmost application as Unicode text) to ¬
display dialog errMsg ¬
buttons {"Cancel"} default button 1 ¬
with title "Error on handle_string: " & errorNumber ¬
with icon 0
end try
end handle_string
on setPosition(args)
set Rect to my getDisplayRect()
tell application (path to frontmost application as Unicode text)
activate
try
-- Set size!
tell application id "com.apple.systemevents"
-- Set height/width within limits.
if (pos of args is in {"full", "f"}) then
-- Fill screen
set pos of args to "center"
set _width to w of Rect
set _height to h of Rect
else
-- Other sizing args
set _width to w of args
set _height to h of args
-- Limit dimensions
if (_width < minWidth) then set _width to minWidth
if (_height < minHeight) then set _height to minHeight
if (_width > w of Rect) then set _width to w of Rect
if (_height > h of Rect) then set _height to h of Rect
end if
-- Get front window of front application.
set frontWindow to ¬
front window of (first application process where frontmost = true)
-- Setting window dimensions.
set size of frontWindow to {_width, _height}
-- Minimize jarring shifts by moving the window only if it has no room to grow then size up again.
-- x axis
if (item 1 of (get size of frontWindow) < _width) then
set position of frontWindow to {¬
(x of Rect) + (w of Rect) - _width, ¬
item 2 of (get position of frontWindow)}
set size of frontWindow to {¬
_width, ¬
item 2 of (get size of frontWindow)}
end if
-- y axis
if (item 2 of (get size of frontWindow) < _height) then
set position of frontWindow to {¬
item 1 of (get position of frontWindow), ¬
(y of Rect) + (h of Rect) - _height}
set size of frontWindow to {¬
item 1 of (get size of frontWindow), ¬
_height}
end if
-- Get new window dimensions.
set {_width, _height} to size of frontWindow
-- Final positioning.
if (pos of args is in {"center", "c"}) then
set position of frontWindow to {¬
(x of Rect) + (((w of Rect) - _width) / 2), ¬
(y of Rect) + (((h of Rect) - _height) / 2)}
else if (pos of args is in {"left", "l"}) then
set position of frontWindow to {¬
(x of Rect), ¬
(y of Rect) + (((h of Rect) - _height) / 2)}
else if (pos of args is in {"right", "r"}) then
set position of frontWindow to {¬
(x of Rect) + ((w of Rect) - _width), ¬
(y of Rect) + (((h of Rect) - _height) / 2)}
else
-- Invalid input --
tell application (path to frontmost application as Unicode text) to ¬
return display dialog "Usage:
center | left | right | full, [ width x height ]
Defaults when no options are given:
" & dPosition & ", " & dWidth & " x " & dHeight ¬
buttons {"Okay"} default button 1
-- Invalid input --
end if
end tell
on error errMsg number errorNumber
tell application (path to frontmost application as Unicode text) to ¬
display dialog errMsg ¬
buttons {"Cancel"} default button 1 ¬
with title "Error on setPosition: " & errorNumber ¬
with icon 0
end try
end tell
end setPosition
on getDisplayRect()
-- Read DisplaySets
set displayProperties to ¬
do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets"
tell application id "com.apple.systemevents"
-- Read "DisplaySets" defaults as a propertly list
set DisplaySets to first item of ¬
(value of (make new property list item with properties {text:displayProperties}) as list)
-- Get position of front most window.
set {fwx, fwy} to position of ¬
(front window of (first application process where frontmost = true))
end tell
-- Use this to read the coordinates.
set Rect to {x:null, y:null, w:null, h:null}
-- Locate which display the front most window is located.
repeat with Display in DisplaySets
set {x, y, w, h} to {¬
OriginX of Display as number, ¬
OriginY of Display as number, ¬
Width of Display as number, ¬
Height of Display as number}
if ((fwx ≥ x) and (fwx < ((x) + (w))) and ((fwy ≥ y) and (fwy < ((y) + (h))))) then
set Rect to {x:x, y:y, w:w, h:h}
exit repeat
end if
end repeat
return Rect
end getDisplayRect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment