Gather all windows to main display, take 3

Alec Jacobson

November 03, 2010

weblog/

Here's my new definitive "gather all windows to main display" applescript:
set main_display_size to get_main_display_size()
set main_display_width to item 1 of main_display_size
set main_display_height to item 2 of main_display_size

tell application "System Events"
  set frontmostApplication to name of the first process whose frontmost is true
end tell

tell application "System Events" to set the visible of every process to true

tell application "System Events" to set theApps to (name of every process whose visible is true)
repeat with theApp in theApps
  try
    tell application theApp
      set i to 1
      set stack_x to 0
      set stack_y to 0
      if theApp is "Finder" then
        set real_windows to item 1 of windows
      else
        set real_windows to windows
      end if
      repeat (length of real_windows) times
        try
          set old_bounds to bounds of (window i)
          -- only mess with windows whose top left corner is not on main display
          if (item 1 of old_bounds) < 0 or (item 1 of old_bounds) ≥ main_display_width or (item 2 of old_bounds) < 0 or (item 2 of old_bounds) ≥ main_display_height then
            --      if (item 1 of old_bounds
            set new_x1 to (((item 1 of old_bounds) mod main_display_width) + main_display_width) mod main_display_width
            if new_x1 > main_display_width / 2 then
              set new_x1 to stack_x
              set stack_x to stack_x + 10
            end if
            set new_y1 to (((item 2 of old_bounds) mod main_display_height) + main_display_height) mod main_display_height
            if new_y1 > main_display_height / 2 then
              set new_y1 to stack_y
              set stack_y to stack_y + 10
            end if
            set new_x2 to new_x1 + ((item 3 of old_bounds) - (item 1 of old_bounds))
            if new_x2 > main_display_width then
              set new_x2 to main_display_width
            end if
            set new_y2 to new_y1 + ((item 4 of old_bounds) - (item 2 of old_bounds))
            if new_y2 > main_display_height then
              set new_y2 to main_display_height
            end if
            
            set new_bounds to {new_x1, new_y1, new_x2, new_y2}
            set bounds of (window i) to new_bounds
          end if
        end try
        set i to i + 1
      end repeat
    end tell
  end try
end repeat
-- return focus to original frontmost
tell application frontmostApplication
  activate
end tell

on get_main_display_size()
  set command to "/usr/sbin/system_profiler SPDisplaysDataType"
  set output to (do shell script command)
  set AppleScript's text item delimiters to "Resolution: "
  set displays to text items of output
  set i to 2
  repeat ((length of displays) - 1) times
    if (offset of "Main Display: Yes" in (item i of displays)) is not equal to 0 then
      set word_list to words of item i of displays
      set main_display_width to item 1 of word_list
      set main_display_height to item 3 of word_list
    end if
    set i to i + 1
  end repeat
  return {main_display_width as integer, main_display_height as integer}
end get_main_display_size