Create identical tiled windows in most apps via AppleScript

Jun 20, '07 07:30:00AM

Contributed by: omo

After experimenting with certain scripts to tile windows and reduce screen cluttering distraction, I wrote the following AppleScript and assigned it to Control-W with Quicksilver:

property maxWindowWidth : 1000
tell application "Finder"
  set screenSize to bounds of window of desktop
  set availScreenWidth to item 3 of screenSize
  set availScreenHeight to item 4 of screenSize
end tell

tell application "System Events"
  set dockProps to property list file "~/Library/Preferences/com.apple.dock.plist"
  set dockSide to the value of the property list item "orientation" of dockProps
  set dockTileSize to the value of the property list item "tilesize" of dockProps 
end tell

if dockSide is "bottom" then
  set availScreenHeight to availScreenHeight - (dockTileSize + 10)
end if

set sideMargin to (availScreenWidth - maxWindowWidth) / 2

tell application "System Events"
  set myFrontMost to name of first item of (processes whose frontmost is true)  
end tell

if myFrontMost is "Finder" then
  set myTopCorr to 22
else
  set myTopCorr to 0
end if

try
  tell application myFrontMost
    set allWindows to (every window where visible is true)
    set n to count of allWindows
    set windowOffset to 0
    repeat with x from 1 to n
      set windowOffset to 20 * (x - 1)
      tell window x
        set bounds to {sideMargin + windowOffset, 30 + myTopCorr + windowOffset, (availScreenWidth - sideMargin) + windowOffset, availScreenHeight - (n + 1 - x) * 20}
      end tell
      set x to x + 1
      if x is equal to (n + 1) then exit repeat
    end repeat
  end tell
end try

You can adjust the width of the resulting tiled windows by changing the value of maxWindowWidth at the top of the script.

[robg adds: If you've never moved your dock to one side of the screen or the other, the above script will fail (because the 'orientation' key won't exist in the Dock's preferences file). The fix is to move the dock to one side, then move it back, and this will work as described. I'm sure there are AppleScript solutions that could check for the key's existence before trying to extract it, but such solutions are beyond my limited AppleScript skills.]

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070617075008161