Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create identical tiled windows in most apps via AppleScript System
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.]
    •    
  • Currently 2.71 / 5
  You rated: 5 / 5 (7 votes cast)
 
[13,116 views]  

Create identical tiled windows in most apps via AppleScript | 12 comments | Create New Account
Click here to return to the 'Create identical tiled windows in most apps via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create identical tiled windows in most apps via AppleScript
Authored by: crum on Jun 20, '07 08:08:21AM

Nice and usefull, thanks!



[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: bootle on Jun 20, '07 10:42:00AM
I've been trying to do something very similar for a very long time. Use a quicksilver trigger to just center the frontmost window. I could never get it to work (I hate applescript), until I saw this script.

tell application "Finder"
	set screenSize to bounds of window of desktop
	set screenWidth to item 3 of screenSize
end tell

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

try
	tell application myFrontMost
		set windowSize to bounds of window 1
		set windowXl to item 1 of windowSize
		set windowYt to item 2 of windowSize
		set windowXr to item 3 of windowSize
		set windowYb to item 4 of windowSize
		
		set windowWidth to windowXr - windowXl
		
		set bounds of window 1 to {(screenWidth - windowWidth) / 2.0,
 windowYt, (screenWidth + windowWidth) / 2.0, windowYb}
	end tell
end try
Works exactly how I always wanted, I set it to ctrl+option+shift+c

[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: NovaScotian on Jun 20, '07 02:44:38PM
bounds of window of desktop returns the entire width of multiple screens side by side, so the script in this form splits windows between screens.

[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: omo on Jun 21, '07 04:52:01AM

Both of the concerns already popped up my mind, but i thought to share the script anyway; The one with the dock positioning should be easy to fix, the one with dual screen setups is probably not that easy to solve. I'll try to fix them some time in the future and post them here. Cheers, Moritz



[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: spheare on Jun 21, '07 04:54:57AM
The script actually cascades the windows. I modified it to tile all windows of an application horizontally across the screen so you can see them all at once.

-- ADJUST THIS TO YOUR LIKING
property spacing : 10

-- DO NOT TOUCH BELOW HERE
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
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 44
else
	set myTopCorr to 22
end if
try
	tell application myFrontMost
		set allWindows to (every window where visible is true)
		set n to count of allWindows
		set nSpacingWidth to (n + 1) * spacing
		set nWindowWidth to (availScreenWidth - nSpacingWidth) / n
		repeat with x from 1 to n
			tell window x
				set nLeft to (x * spacing) + (x - 1) * nWindowWidth
				set bounds to {
					nLeft, 
					myTopCorr + spacing, 
					nLeft + nWindowWidth, 
					availScreenHeight - spacing}
			end tell
		end repeat
	end tell
on error the error_message number the error_number
	display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try



[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: raffar on Jun 22, '07 06:20:38AM
Here is a modification. I added options to allow right and left margins to be set independently. If these properties are set to 0, then default to previous behaviour. I also needed to add some height to correct for the menubar on my system. And lastly, this doesn't seem to work with Firefox. Anyone have any idea why? (Firefox does not seem to be responding to the "tell window x" directive.)
property maxWindowWidth : 1000
property menubarCorrection : 14 -- menubar height
property leftSideMarginDefault : 100 -- default position from left to start cascade. If 0 will default to half of available screen width minus maxWindowWidth
property rightSideMarginDefault : 200 -- default position from left to start cascade. If 0 will default to half of available screen width minus maxWindowWidth
-- therefore, if both leftSideMarginDefault and rightSideMarginDefault are 0, then center windows


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

if leftSideMarginDefault is equal to 0 then
	set leftSideMargin to (availScreenWidth - maxWindowWidth) / 2
else
	set leftSideMargin to leftSideMarginDefault
end if

if rightSideMarginDefault is equal to 0 then
	set rightSideMargin to (availScreenWidth - maxWindowWidth) / 2
else
	set rightSideMargin to rightSideMarginDefault
end if

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 menubarCorrection
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 {leftSideMargin + windowOffset, 30 + myTopCorr + windowOffset, (availScreenWidth - rightSideMargin) + 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



[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: HairyPotter on Jun 24, '07 11:05:05PM

anybody knows if there is a way to make a window always on top?



[ Reply to This | # ]
Terminal.app
Authored by: ptman on Nov 27, '07 06:04:15AM
Hi, I'm using Leopard and trying to make an applescript that tiles my terminal windows. I can't get this working. For some reason, even if event log shows that I'm giving the right set bounds -commands, the windows all keep to the top, right below the menu bar. Make sure you test this with enough terminal windows, I have a small screen so 3-5 are enough.

tell application "Finder"
	set {desktopLeft, desktopTop, desktopRight, desktopBottom} to bounds of window of desktop
	set screenWidth to desktopRight - desktopLeft
	set screenHeight to desktopBottom - desktopTop - 22 -- menu
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 screenHeight to screenHeight - (dockTileSize + 18)
else
	set screenWidth to screenWidth - (dockTileSize + 18)
end if
log "screenHeight: " & screenHeight & " screenWidth: " & screenWidth

tell application "Terminal"
	set windowWidth to 490 -- 80x24 terminal with monaco 10pt minus the scrollBar
	set windowHeight to 368 -- 80x24 terminal with monaco 10pt
	
	set perRow to screenWidth div windowWidth
	set perColumn to screenHeight div windowHeight
	set theWindows to (every window where visible is true)
	
	repeat with theIndex from 1 to count of theWindows
		set theWindow to item theIndex of theWindows
		set realIndex to theIndex - 1
		set theColumn to realIndex mod perRow
		set theRow to (realIndex div perRow) mod perColumn
		set windowLeft to theColumn * windowWidth
		set windowTop to theRow * windowHeight
		log "theColumn: " & theColumn & " theRow: " & theRow
		set bounds of theWindow to {windowLeft, windowTop, windowLeft + windowWidth + 16, windowTop + windowHeight}
	end repeat
end tell


[ Reply to This | # ]
Tile windows horizontally and vertically
Authored by: isherman on Feb 03, '08 04:36:09AM

I've modified the script that tiles windows so that all visible windows are shown side-by-side, and tiled both horizontally and vertically:

property horizontalSpacing : 10 -- sets the horizontal spacing between windows
property verticalSpacing : 10 -- sets the vertical spacing between windows

-- Find screen bounds
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

-- Account for space occupied by dock if dock is on the bottom and not hidden
tell application "System Events"
set dockProps to property list file "~/Library/Preferences/com.apple.dock.plist"
set docVisible to not the value of the property list item "autohide" of dockProps
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 docVisible and dockSide is "bottom" then
set availScreenHeight to availScreenHeight - (dockTileSize + 10)
end if

-- Select current application as the application whose windows to tile
tell application "System Events"
set myFrontMost to name of first item of (processes whose frontmost is true)
end tell

-- Top margin correction factor
if myFrontMost is "Finder" then
set myTopCorr to 44
else
set myTopCorr to 22
end if

-- Tile the windows
try
tell application myFrontMost
set allWindows to (every window where visible is true)
set n to count of allWindows
set nPerAxis to (square root of (n as real)) -- if n is a perfect square, sqrt(n) windows will be shown on each row and column
set nColumns to round (ceiling of nPerAxis) rounding as taught in school -- otherwise, prefer to have more columns than rows
set nRows to round (floor of nPerAxis) rounding as taught in school
set nSpacingWidth to (nColumns - 1) * horizontalSpacing
set nSpacingHeight to (nRows - 1) * verticalSpacing
set nWindowWidth to (availScreenWidth - nSpacingWidth) / nColumns
set nWindowHeight to (availScreenHeight - myTopCorr - nSpacingHeight) / nRows
repeat with x from 1 to n
tell window x
set nLeft to ((x - 1) mod nColumns) * (horizontalSpacing + nWindowWidth)
set nTop to myTopCorr + ((x - 1) div nColumns) * (verticalSpacing + nWindowHeight)
set bounds to {
nLeft,
nTop,
nLeft + nWindowWidth,
nTop + nWindowHeight}
end tell
end repeat
end tell
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try



[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: isherman on Feb 14, '08 12:28:42AM
O.k., the above was basically my first attempt at AppleScripting, and while it worked o.k. on limited test cases on my system is in general not very robust. For a better (more robust) script along these lines, you might want to take a look at this script.

[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: mbrowne on Feb 24, '08 03:37:43PM
If you get the error "NSReceiverEvaluationScriptError: 4" when trying to run this script, you might need to enable GUI scripting. I think actually a system update to the latest version of Mac OS Tiger may have turned it off (not sure though, as I didn't try this script before then). I had to open the AppleScript utility ("/Applications/AppleScript/AppleScript Utility.app") and check "Enable GUI Scripting" and then it worked..xexcept not in Finder, which is mainly what I needed it for, but thanks anyway for posting.

There's a nice ApplesScript application called 2Finder that I found to get the tile effect in Finder (with 2 windows at least). You might need to alter some properties at the beginning of the script (as I did) if your dock is not set to auto-hide.
http://www.versiontracker.com/dyn/moreinfo/macosx/30516

[ Reply to This | # ]
Create identical tiled windows in most apps via AppleScript
Authored by: kanenas on Apr 20, '08 11:13:24AM
A "try" block can catch the lack of an "orientation" property. Something like:
tell application "System Events"
	set dockProps to property list file "~/Library/Preferences/com.apple.dock.plist"
	try
		set dockSide to the value of the property list item "orientation" of dockProps
	on error
		set dockSide to "bottom" -- this is the default orientation, right?
	end try
	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)
else if dockSide is in {"left", "right"} then
	set availScreenWidth to availScreenWidth - (dockTileSize + 10)
end if


[ Reply to This | # ]