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


Click here to return to the 'Saving and restoring tabs in Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Saving and restoring tabs in Safari
Authored by: Bardamard on Apr 28, '04 07:01:09PM
Great hint but wouldn't it be nice to save the information to a file so that you can have a collection of save tab files?? Here is a new script from the one listed that will give you a save dialog box to give it a name and to decide a location.

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
			
			-- empty line to seprate windows
			set url_list to url_list & ""
		end repeat
	end tell
end tell

-- convert url list  to text
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set url_list to url_list as text
set AppleScript's text item delimiters to old_delim

-- get path to prefs file where URLs will be stored
set prefs_folder to ("iBook1:Users:Shared:MySafariTabs:") as string
choose file name
set prefs_file to result as string
try
	set open_file to ¬
		open for access file prefs_file with write permission
	-- erase current contents of file:
	set eof of open_file to 0
	write url_list to open_file starting at eof
	close access open_file
on error
	try
		close access file prefs_file
	end try
end try
I didn't bother to extricate the unneeded code from the script, but it works great. But I did get rid of the unwanted "Done" dialog box. Seems a bit redundant. Here is the Restore All Tabs script with a "choose File" dialog as well. Again I didn't remove the unneeded code.

tell application "Safari" to activate

-- get path to prefs file where URLs are stored
set prefs_folder to path to preferences folder as string
choose file

set prefs_file to result as string

try
	-- read the saved urls
	set open_file to ¬
		open for access file prefs_file without write permission
	set url_list to read open_file using delimiter return
	close access open_file
	
	set tmpList to {}
	set newUrl_list to {}
	
	-- make urls from file into a list of lists
	-- each window is a seperate list in the big list
	repeat with aUrl in url_list
		if aUrl & "x" is "x" then
			set newUrl_list to newUrl_list & {tmpList}
			set tmpList to {}
		else
			set tmpList to tmpList & aUrl
		end if
	end repeat
	
	-- don't forget the last list, or maybe the only
	set newUrl_list to newUrl_list & {tmpList}
	
	tell application "Safari"
		
		-- loop through the list of windows
		repeat with urls in newUrl_list
			
			my new_window()
			set emptyWindow to true
			
			-- loop through the list of tabs
			repeat with aUrl in urls
				if emptyWindow then
					set emptyWindow to false
				else
					my new_tab()
				end if
				
				-- open the url
				set URL of document 1 to aUrl
				
			end repeat
			
		end repeat
	end tell
on error
	try
		close access file prefs_file
	end try
end try

-- let the user know we are done.
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

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


[ Reply to This | # ]