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


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