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

An improved 'Combine Windows' AppleScript for Safari Web Browsers
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
    •    
  • Currently 3.25 / 5
  You rated: 4 / 5 (4 votes cast)
 
[20,595 views]  

An improved 'Combine Windows' AppleScript for Safari | 22 comments | Create New Account
Click here to return to the 'An improved 'Combine Windows' AppleScript for Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An improved 'Combine Windows' AppleScript for Safari
Authored by: goze on Apr 06, '04 11:53:30AM

Doesn't work in Jaguar, unless you have updated AppleScript with the GUI Scripting.
(my work hasn't let me yet)



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: andrew_zinn on Apr 06, '04 11:57:21AM

If one window has tabs and another does not, executing this script loses the untabbed window.



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: mwalworth on Apr 06, '04 01:11:27PM

The script depends on having the "Always show tab bar" preference in the "Tabs" preference pane turned on.



[ Reply to This | # ]
Turn on GUI scripting
Authored by: sinjin on Apr 06, '04 01:41:16PM
Great hint, I've been wanting this functionality for some time.

For those that see all their windows vaporize when running this script, remember to turn on GUI scripting!

[ Reply to This | # ]

Take care on non-English systems
Authored by: seb2 on Apr 06, '04 02:26:00PM

Remember to localize 'menu "File"', 'click menu item "New Tab"' and 'click menu item "New Window"' to whatever it says in the language you're using Safari in, otherwise it will fail to open new windows or tabs but instead just close everything.



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: rb3 on Apr 06, '04 06:20:26PM

Is there some one way to modify this hint to make a script that will save a collection of tabs in a way that would let you reopen that same set later? I don't mean a static set that would then be a link-folder in the toolbar, but rather 3-7 tabs in one window that are all not previously bookmarked. I'd love to be able to save a session of multi-threaded browsing, and rejoin it later...

I've never seen a good solution to this, and I feel like this script is sort of a cousin to what I am after. All suggestions welcomed.

---
512Ke, SE/30, 7500, PMG4/DualGig



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: timcrawf on Apr 06, '04 07:06:46PM

This is something I have wanted as well.
But I want more. I want the tabs to be independent objects. Like the tabs in Adobe tool palettes. What I want is to be able to have 2,3 or more windows open with multiple tabs in each. Now I want one window with one or more tabs from some of the other windows.
I want to click and drag a tab to a different window to reorganize my browsing.



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: nickfitz on Apr 06, '04 08:10:39PM

Not quite what you want, as you have to activate the tabs you want in turn, but you can drag the icon at the left of the address bar to an empty area (or the extreme right) of the tab bar of another window to open the page in a new tab in that window. Dropping it on an existing tab opens it in that tab.



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: truhe on Apr 08, '04 03:14:49AM

try firebird with a different theme than the default theme and the tab extensions. they provide everything you need, from saving of tab-sets to drag and drop from one window to another, rearranging the order, "close left tabs" and much more. very nice tool. i'm using it in mozilla at work.



[ Reply to This | # ]
Saving tabs...
Authored by: sinjin on Apr 06, '04 08:12:03PM
Ask and you shall receive--one of my favorite hints of all time ;-)

Save Tabs hint

[ Reply to This | # ]

Saving tabs...
Authored by: rb3 on Apr 06, '04 11:31:55PM

I would have sworn that I had scoured everything around here that was related to this search, but clearly I'd missed this great hint - so many, many thanks for pointing it out to me. I've been swearing to learn AppleScript since 1995, so maybe this will be the trigger I've been looking for for nine years.

---
512Ke, SE/30, 7500, PMG4/DualGig



[ Reply to This | # ]
Saving tabs...
Authored by: rb3 on May 13, '04 04:45:50PM

No sooner did I find this hint than the plug-in Saft upgraded itself to embody this funtionality.

---
512Ke, SE/30, 7500, PMG4/DualGig



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: makeinu on Apr 06, '04 09:03:39PM
I couldn't get this version to work correctly, but I have a version that does:

set url_list to {}

tell application "System Events"
	tell process "Safari"
		
		-- count the windows
		set winCount to count of windows
		
		-- loop through the windows
		repeat with i from 1 to winCount
			
			-- count the tabs
			set tabCount to count of radio buttons of window i
			
			-- loop through the tabs
			repeat with j from 1 to tabCount
				-- activate the tab 
				click radio button j of window i
				
				-- url of window
				set theUrl to value of text field 1 of group 1 
					of splitter group 1 of window i
				
				-- save the url
				set url_list to url_list & theUrl
			end repeat
			
		end repeat
	end tell
end tell


tell application "Safari"
	(* Close all windows *)
	set the_windows to every window
	repeat with a in the_windows
		close a
	end repeat
	
	(* Open all urls in one window *)
	my newWindow()
	set first_tab to true
	repeat with aUrl in url_list
		if first_tab then
			set first_tab 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
I whipped this up awhile ago, after discovering the original 'Combine Windows' script, using part of the 'Save Tabs' script as a starting point.

[ Reply to This | # ]
Tweaking this a bit for "tabless" windows...
Authored by: djrc on Apr 08, '04 02:47:26PM
The above script also only consolidates windows containing tabs -- if you have window(s) without tabs (and haven't set Safari to always display tabs), then the script will close those windows and not add them as new tabs in the consolidated window (so they get lost). A very minor change to the above script will change the behavior to consolidate "tabless" windows as well. Here's the revised version:

set url_list to {}

tell application "System Events"
	tell process "Safari"
		
		-- count the windows
		set winCount to count of windows
		
		-- loop through the windows
		repeat with i from 1 to winCount
			
			-- count the tabs
			set tabCount to count of radio buttons of window i
			
			--- if there are tabs:
			if tabCount > 0 then
				
				-- loop through the tabs
				repeat with j from 1 to tabCount
					-- activate the tab 
					click radio button j of window i
					
					-- url of window
					set theUrl to value of text field 1 of group 1 
						of splitter group 1 of window i
					
					-- save the url
					set url_list to url_list & theUrl
				end repeat
				
			--- if no tabs:	
			else
				-- just add the url of the window
				set theUrl to value of text field 1 of group 1 
					of splitter group 1 of window i
				
				-- save the url
				set url_list to url_list & theUrl
			end if
			
		end repeat
	end tell
end tell


tell application "Safari"
	(* Close all windows *)
	set the_windows to every window
	repeat with a in the_windows
		close a
	end repeat
	
	(* Open all urls in one window *)
	my newWindow()
	set first_tab to true
	repeat with aUrl in url_list
		if first_tab then
			set first_tab 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


[ Reply to This | # ]
Tweaking this a bit for "tabless" windows...
Authored by: makeinu on Apr 08, '04 05:45:38PM
Hate to disagree, but I tested it before publishing, and it works. However, I suppose that I should mention that I am not yet running Panther, so I don't know if that may make a difference. Here is another version, slightly tweaked for better performance.


set URL_list to {}

tell application "System Events"
	tell process "Safari"
		
		-- count the windows
		set winCount to count of windows
		
		-- loop through the windows
		repeat with i from 1 to winCount
			
			-- count the tabs
			set tabCount to count of radio buttons of window i
			
			-- loop through the tabs
			repeat with j from 1 to tabCount
				-- activate the tab 
				click radio button j of window i
				
				-- url of window
				set theUrl to value of text field 1 of group 1 
					of splitter group 1 of window i
				
				-- save the url
				set URL_list to URL_list & theUrl
			end repeat
			
		end repeat
	end tell
end tell


tell application "Safari"
	(* Close all windows *)
	set the_windows to every window
	repeat with a in the_windows
		close a
	end repeat
	
	(* Open all urls in one window *)
	my newWindow()
	set emptyWindow to true
	
	-- loop through the list of tabs
	repeat with aURL in URL_list
		if emptyWindow then
			set emptyWindow to false
		else
			my newTab()
		end if
		
		-- open the url
		set URL of document 1 to aURL
	end repeat
end tell

-- let the user know we are done.
tell application "Safari"
	activate
	display dialog "All Done" buttons {"OK"} default button 1
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



[ Reply to This | # ]
Tweaking this a bit for "tabless" windows...
Authored by: djrc on Apr 08, '04 06:27:30PM
Well, it worked the way I described (on my laptop, which was the only system I tested it on) -- just closing windows that had no tabs. I guess that's obvious, since I wouldn't have tried to "fix" it otherwise! In any event, I am running OS X 10.3.3 and I also do not have Safari set to always display tabs. I don't know whether one or both of those facts is the explanation for the differing behavior.

[ Reply to This | # ]
Tweaking this a bit for "tabless" windows...
Authored by: makeinu on Apr 08, '04 10:02:29PM

Meh, presume it's a difference in GUI scripting between Jaguar and Panther then. I'd read that many things changed with the upgrade. I'll keep your version in mind for when I make the jump. Thanks!



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: jedik on Apr 06, '04 10:16:02PM

Nice tip, thanks.

One question: is there any way to activate the Scripts menu *only* for Safari, as it happens with Address Book and Mail?

I have tried to create a folder in my ~/Library/Scripts folder, the same way I did for my Address Book scripts, but it didn't work out. :\

I don't like the AppleScript Manu (too many icons in the menubar already ;), and I would like to enable this feature in Safari. Is it possible???

Thanks in advance.

---
:: Jedi Knight ::
-- Mac Rules! --



[ Reply to This | # ]
Improved! 'Faster', less fragile, drops empty tabs
Authored by: ageless on Apr 08, '04 08:50:00PM
Here's a revision of djrc's revision.

The main difference is that the active window is preserved, which is helpful if you first select a window with a lot of tabs or with tabs that shouldn't (or can't) be reloaded. If you have one window with 8 tabs and three other windows with only one tab, then this revision will be much faster!

The other cool thing is that this revision will drop empty tabs, as well as empty windows.

Since this builds on djrc's, you don't need to have the tab bar set to always show.

There are a few other features I'm trying to add:

-- Need to fix so that tabs with alert dialogs (like "Server not found") won't mess everything up. (Update: Fixed!)

-- Automatically preserve window with greatest number of tabs.

-- Set script to trigger when a page is opened in a new window

I'm also looking to spruce up mazatty's save/restore scripts (see comment above), so that the save script can handle empty tabs and alert dialogs (I think it chokes on both).

***UPDATE***

I fixed it so that it will dismiss the "server not found" alert dialog and drop the corresponding window or tab!

It may have problems with javascript alerts, I haven't tested it yet. And better error checking would be nice (instead of just messing things up). Anyone?


set url_list to {}

tell application "System Events"
	tell process "Safari"
		
		-- count the windows
		set winCount to count of windows
		
		-- loop through the windows, except for the active one
		repeat with i from 2 to winCount
			
			-- count the tabs
			set tabCount to count of radio buttons of window i
			
			--- if there are tabs:
			if tabCount > 0 then
				
				-- loop through the tabs
				repeat with j from 1 to tabCount
					-- activate the tab 
					click radio button j of window i
					
					-- dismiss a possible alert dialog
					if exists sheet 1 of window i then
						
						tell sheet 1 of window i
							click button "OK"
						end tell
						
					else
						
						-- url of window
						set theUrl to value of text field 1 of group 1 
							of splitter group 1 of window i
						
						if theUrl is not equal to "" then
							
							-- save the url
							set url_list to url_list & theUrl
							
						end if
					end if
				end repeat
				
				--- if no tabs: 
			else
				-- just add the url of the window
				
				-- dismiss a possible alert dialog
				if exists sheet 1 of window i then
					
					tell sheet 1 of window i
						click button "OK"
					end tell
					
				else
					
					set theUrl to value of text field 1 of group 1 
						of splitter group 1 of window i
					
					if theUrl is not equal to "" then
						
						-- save the url
						set url_list to url_list & theUrl
						
					end if
				end if
			end if
		end repeat
	end tell
end tell


tell application "Safari"
	(* Close all windows other windows *)
	repeat with i from 2 to winCount
		close window 2
	end repeat
	
	(* Open all urls in the active window *)
	repeat with aUrl in url_list
		my newTab()
		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


[ Reply to This | # ]
Improved! 'Faster', less fragile, drops empty tabs
Authored by: ceesaxp on Apr 12, '04 04:02:10PM

I wish there was a way to do it without reloading each URL in newly created tabs... Wait, don't say "Camino" -- I want a way to do that in Safari...



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: itsajeepthing111 on May 14, '06 01:22:24AM

I was having problems with all of the applescript codes written above, don't know why, but I was. Thus, I wrote my own bassically from scratch. I am somewhat of beginner with applescript so feel free to point out errors or suggestions.
It consolidates all the open windows & tabs into a single new window with tabs, and gets rid of all duplicate pages including those on the frontmost window (i.e. the window selected when the applescript was activated). Only tested on Tiger. Hope this helps some ppl out.

set url_list to {}

tell application "Safari"
activate
try

--Goes Through Each Window--
repeat while (window 1 exists) and (document 1 exists)
set first_URl to the URL of document 1
set first_timeD to 1

--Goes Throught Each Tab--
repeat while ((the URL of document 1 is not equal to first_URl) or (first_timeD is equal to 1))
if first_timeD is equal to 1 then
set first_timeD to 0
end if
set this_URL to the URL of document 1
if url_list does not contain this_URL then
set url_list to url_list & this_URL
end if
tell application "System Events" to keystroke "}" using command down

--Reduce Beeping Noise--
delay 0.2

--End Tab Loop--
end repeat

close window 1

--End Window Loop--
end repeat

--Creates New Window to Consolidate Pages--
tell application "System Events" to keystroke "n" using command down
set the URL of document 1 to item 1 of url_list
repeat with w from 2 to (count of url_list)
my new_tab()
set the URL of document 1 to item w of url_list
end repeat

on error the error_message number the error_number
display dialog the error_message buttons {"OK"} default button 1
end try
end tell

on new_tab()
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 new_tab



[ Reply to This | # ]
An improved 'Combine Windows' AppleScript for Safari
Authored by: itsajeepthing111 on May 14, '06 07:07:06PM

Here's an updated version, realized beeping noise was due to an invalid key press not because script was running to fast. Here it is:

set url_list to {}

tell application "Safari"
activate
try

--Goes Through Each Window--
repeat while (window 1 exists) and (document 1 exists)
set first_URl to the URL of document 1
set first_timeD to 1

--Goes Throught Each Tab--
repeat while ((the URL of document 1 is not equal to first_URl) or (first_timeD is equal to 1))
if first_timeD is equal to 1 then
set first_timeD to 0
end if
set this_URL to the URL of document 1
if url_list does not contain this_URL then
set url_list to url_list & this_URL
end if

--Checks to Make Sure There is Another Tab to selcet (Therfore Negating Beep Noise From Invalid Key Press)
tell application "System Events"
tell application process "Safari"
set selectNextEnabled to enabled of menu item "Select Next Tab" of menu "Window" of menu bar 1
end tell
end tell
if selectNextEnabled is true then
tell application "System Events" to keystroke "}" using command down
end if

--End Tab Loop--
end repeat

close window 1

--End Window Loop--
end repeat

--Creates New Window to Consolidate Pages--
tell application "System Events" to keystroke "n" using command down
set the URL of document 1 to item 1 of url_list
repeat with w from 2 to (count of url_list)
my new_tab()
set the URL of document 1 to item w of url_list
end repeat

on error the error_message number the error_number
display dialog the error_message buttons {"OK"} default button 1
end try
end tell

on new_tab()
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 new_tab



[ Reply to This | # ]