AppleScript to Restore Window Sizes and Positions
After much scouring, cursing, and damning, I’ve cobbled together a script that restores my most-used windows to their proper positions.
Certainly not pretty, but it is AppleScript after all:
set front_app to (path to frontmost application as Unicode text)
--True Monitor size
property monitor_width : 1920
property monitor_height : 1200
--Thicknes of titlebars (to prevent overlapping; should probably remain set to 23)
property menubar : 40
--Margins to make room for iTunes, Dock, Desktop items, etc
property leftmargin : 1500
property rightmargin : 12
property topmargin : 40
property bottommargin : 2
property gap_val : 2
(* notes on space values:
iTunes @ top left: topmargin = 68
small dock: bottommargin = 30
*)
property open_info : false -- open the info window?
--Height and width of the Info Window
property info_width : 330
property info_height : 335
property info_gap : 2 --gap beyond info_width to leave
--set one of these values to 1 (rather than 0) to leave space for info window
property info_top_left : 0
property info_top_right : 0
property info_bottom_left : 0
property info_bottom_right : 0
property hide_others : false --hide other visible applications?
--set folder defaults
(*
options for folders:
0. root (all volumes)
1. user home directory
2. user documents
*)
property folder1_pref : 0
property folder2_pref : 2
set startDir to path to startup disk
set homeDir to (path to "cusr" from user domain)
tell application "Path Finder"
--set default directories (see properties above)
if folder1_pref = 0 or folder1_pref > 2 then set folder1 to startDir
if folder1_pref = 1 then set folder1 to homeDir
if folder1_pref = 2 then set folder1 to ((homeDir as string) & "Documents") as alias
if folder2_pref = 0 or folder2_pref > 2 then set folder2 to startDir
if folder2_pref = 1 then set folder2 to homeDir
if folder2_pref = 2 then set folder2 to ((homeDir as string) & "Documents") as alias
-- necessary to use system events to set posix path
tell application "System Events" to set posixF1 to POSIX path of folder1
set vSpace to (monitor_height - (topmargin + bottommargin + gap_val + menubar))
set win_vSpace to round (vSpace / 2)
set win_hSpace to (monitor_width - (leftmargin + rightmargin))
close every finder window
-- TOP WINDOW
open folder1
set top_window to finder window 1
set bottom_of_top to monitor_height - bottommargin - win_vSpace - gap_val
--{left,top,right,bottom}
set the bounds of top_window to ¬
{leftmargin + ((info_width + info_gap) * info_top_left), ¬
topmargin + (menubar), ¬
monitor_width - rightmargin - ((info_width + info_gap) * info_top_right), ¬
bottom_of_top}
-- BOTTOM WINDOW
open folder2
set bot_window to finder window 1
--{left,top,right,bottom}
set the bounds of bot_window to ¬
{leftmargin + ((info_width + info_gap) * info_bottom_left), ¬
bottom_of_top + gap_val, ¬
monitor_width - rightmargin - ((info_width + info_gap) * info_bottom_right), ¬
monitor_height - bottommargin}
-- INFO WINDOW
if open_info then
--Applescript currently can't set the position or bounds of the Info Window.
--Just move it to where you want it, close it, then open it. From then on, it should open in the proper place
PFInfo posixF1
end if
--Reactivate windows
activate
if hide_others then
tell application "System Events"
set visible of (every process whose visible is true and frontmost is false) to false
end tell
end if
end tell
try
tell application "Finder"
repeat while window 1 exists
close window 1
end repeat
end tell
end try
try
tell application "TextMate"
activate
set bounds of window 1 to {210, 400, 1498, 1198}
end tell
end try
try
tell application "iTerm"
activate
set bounds of window 1 to {42, 702, 1200, 1198}
end tell
end try
try
tell application "Adium"
activate
set bounds of window "Contacts" to {42, 400, 208, 700}
end tell
end try
try
tell application "X-Chat Aqua"
activate
set bounds of window 1 to {42, 30, 442, 398}
end tell
end try
try
tell application "Firefox" to activate
tell application "System Events" to tell process "Firefox"
tell window 1
set size to {1054, 880} --width, height
set position to {444, 30} --left, top
end tell
end tell
end try
try
tell application front_app
activate
end tell
end try
.. Then, using iKey I set this script to launch with an easily-accessible hotkey.