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

An AppleScript to stack windows in Xcode Apps
Xcode, or any Apple app for that matter, has a poor implementation of window stacking and default window placement. So I wrote an AppleScript that will stack Xcode windows. It would be fairly easy to adopt it to another application, but each target will have a different way of getting the list of windows to stack. For example, this script stacks only windows with names matching source documents.

This script is geared towards a single monitor, but will handle the monitor changing resolution. I have a laptop, but I often plug in an external monitor, and this will dynamically adapt. If you have multiple monitors, you could hard code the usable area you want the windows to stack in. If you know a way to get the dimensions of just the main device in AppleScript, post it.

This script stacks in a number of columns, not just one. It gives priority to a minimum window width, and figures out from there how many columns to make. The actual window width is the calculated from the number of columns, but that could be skipped if you prefer a fixed window width.

Here's the script:
tell application "Finder"
  -- get the bounds of the Desktop window
  set {desktopLeft, desktopTop, desktopRight, desktopBottom} to bounds of container window of desktop
  set screenWide to desktopRight - desktopLeft
  set screenTall to desktopBottom - desktopTop
end tell

tell application "Xcode"
  set screenTall to (screenTall - 24) -- leave room for menu bar
  set screenWide to (screenWide - 120) -- leave room on right
  
  set windowsPerStack to 6
  set minimumWide to 720 -- minimum width of window
  set maximumColumn to ((screenWide / minimumWide) - 0.4) as integer -- number of colums to stack in
  set maximumRow to windowsPerStack - 1
  set borderTall to 16 -- space under bottom of stack
  set borderWide to 8 -- space between stacks
  set marginTall to 20 -- space between windows in stack
  set marginWide to 10 -- space between windows in stack
  
  set windowWide to ((screenWide / maximumColumn) as integer) - borderWide - marginWide * maximumRow
  set windowLeft to (windowWide * 0) + borderWide
  
  set theExtra to 0
  set theCount to 0
  set theNames to name of every source document
  set theWindows to {}
  
  -- locate every window that will be stacked
  repeat with theWindow in windows
    if ((visible of theWindow) and (0 is level of theWindow)) then
      set theTitle to title of theWindow
      if (theTitle is in theNames) then
        set theWindows to theWindows & {theWindow}
      end if
    end if
  end repeat
  
  -- stack all the windows
  set theFinal to 0
  set theTotal to count of theWindows
  repeat with theIndex from 1 to theTotal
    set theWindow to item theIndex of theWindows
    
    if (theCount is 0) then
      if ((theTotal - theIndex) < maximumRow) then
        set theFinal to maximumRow - (theTotal - theIndex)
      end if
    end if
    
    -- reverse flips and flops to change stack direction
    set theFlips to theCount + theFinal
    set theFlops to maximumRow - (theCount + theFinal)
    set windowLeft to borderWide + marginWide * theFlops + (windowWide + maximumRow * marginWide + borderWide) * theExtra
    set windowTall to screenTall - marginTall * maximumRow
    set bounds of theWindow to {windowLeft, borderTall + marginTall * theFlips, windowLeft + windowWide, windowTall + marginTall * theFlips}
    
    set theCount to theCount + 1
    if (theCount > maximumRow) then
      set theCount to 0
      set theExtra to theExtra + 1
      if (theExtra ≥ maximumColumn) then
        set theExtra to 0
      end if
    end if
  end repeat
end tell
In Xcode 3, it is easy to tie a script to a command key using Edit User Scripts. In earlier versions, it took a little more trickery, but I am guessing there is already a tip for that. I would prefer that a variant of this script run whenever a new window is opened, and automatically stack that one window, but I have not figured that out yet.

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[7,135 views]  

An AppleScript to stack windows in Xcode | 2 comments | Create New Account
Click here to return to the 'An AppleScript to stack windows in Xcode' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to stack windows in Xcode
Authored by: DougAdams on Nov 13, '07 02:54:48PM

Hey thanks! I was too lazy to do this for myself.

---
Doug's AppleScripts for iTunes
dougscripts.com



[ Reply to This | # ]
An AppleScript to stack windows in Xcode
Authored by: ptman on Nov 27, '07 06:11:18AM
I'm crossposting this, as the other thread/tip where I posted is quite old: http://www.macosxhints.com/comment.php?mode=view&cid=93780

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 | # ]