An improved 'Combine Windows' AppleScript for Safari

Apr 06, '04 10:54:00AM

Contributed by: sporkstorms

The "Combine Windows" Safari script (available on the Safari AppleScript page) takes all the open browser windows and consolidates them into one tabbed window. However, if you have three windows with two tabs each, you might be surprised and frustrated when Apple's script closes all your windows and leaves you with only three tabs (instead of your original six). The problem is that the script only uses the active tab of each window.

The following script gathers up all tabs in all browser windows and combines them into one window (you may also download it in text or compiled script form from here:

Read the rest of the hint for the source to the script...

tell application "System Events"
  tell application process "Safari"
    try
      
      (* Get URLs of all tabs *)
      set urlList to {}
      set numWindows to count of windows
      repeat with w from 1 to numWindows
        set numTabs to count of radio buttons of window w
        repeat with t from 1 to numTabs
          click radio button t of window w
          set aURL to value of text field 1 of group 1 ¬
           of splitter group 1 of window w
          set urlList to urlList & aURL
        end repeat
      end repeat
      
    on error the errmsg number the errn
      display dialog errmsg buttons {"OK"} default button 1
    end try
  end tell
end tell

tell application "Safari"
  (* Close all windows *)
  set allWindows to every window
  repeat with w in allWindows
    close w
  end repeat
  
  (* Open all urls in one window *)
  my newWindow()
  set firstTab to true
  repeat with aURL in urlList
    if firstTab then
      set firstTab to false
    else
      my newTab()
    end if
    set the URL of document 1 to aURL
  end repeat
end tell

on newTab()
  tell application "Safari" to activate
  tell application "System Events"
    tell process "Safari"
      click menu item "New Tab" of menu "File" of menu bar 1
    end tell
  end tell
end newTab
on newWindow()
  tell application "Safari" to activate
  tell application "System Events"
    tell process "Safari"
      click menu item "New Window" of menu "File" of menu bar 1
    end tell
  end tell
end newWindow

Comments (22)


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