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: LightYear on Dec 05, '04 03:27:27AM

Righto, dragging this one back to the top. I finally got around to implementing this great feature (which I do miss, using Opera at work) but still found a couple of bugs with the revisions in the comments. Dusting off my Applescript skills (muchly lacking), here's what I found:

You must have "Always show tab bar" enabled in Preferences, Tabs for single page windows to be picked up! This is because the Applescript uses the count of the tabs to determine if we are looking at a browser window (as opposed to the Downloads window or a source window or something). There could well be better ways of determining this, but I'll deal with the tab bar always being there for now.

There were some inefficiences and old code mucking a couple of things up, so here's my version (which does not include the Save dialog feature!) of the Save URLs Applescript.


-- localizations 
property windowMenu : "Window"
property nextTabMenuItem : "Select Next Tab"
-- end localizations 

property url_list : {}
property docCount : 0
property frontMostWindow : 1

tell application "System Events"
	tell process "Safari"
		activate
		
		-- count the windows
		set winCount to count of windows
		
		set menuItemCount to count of menu items of menu windowMenu of menu bar 1
		set menuItemOffset to menuItemCount - winCount
		
		set url_list to {}
		
		-- loop through the windows
		repeat with winNum from 1 to winCount
			-- find this window in the Window menu using the window title
			set winTitle to title of window winNum
			
			if (count of characters of winTitle) > 20 then
				set winTitle to text 1 thru 20 of winTitle
			end if
			
			repeat with j from menuItemOffset + 1 to menuItemCount
				if ((title of menu item j of menu windowMenu of menu bar 1) ¬
				starts with winTitle) then
					set itemNum to j
					exit repeat
				end if
			end repeat
			
			-- activate a window
			click menu item itemNum of menu windowMenu of menu bar 1
			
			-- Check for browser window
			set tabCount to count of radio buttons of window frontMostWindow
			if tabCount > 0 then
				-- activate the first tab
				click radio button 1 of window frontMostWindow
				
				repeat with tab from 1 to tabCount
					set tabURL to value of text field 1 of group 1 of ¬
					splitter group 1 of window frontMostWindow
					
					-- save the url
					set url_list to url_list & tabURL
					my nextTab()
				end repeat
				
				-- empty line to seprate windows
				set url_list to url_list & ""
			end if
		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_text 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 path to preferences folder as string
set prefs_file to prefs_folder & "Safari Saved URLs"

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_text to open_file starting at eof
	close access open_file
on error
	try
		close access file prefs_file
	end try
end try

-- let the user know we are done.
tell application "Safari"
	activate
	display dialog "All Done" buttons {"OK"} default button 1
end tell


on nextTab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item nextTabMenuItem of menu windowMenu of menu bar 1
		end tell
	end tell
end nextTab

---
* Nothing is foolproof to a sufficiently talented fool *

[ Reply to This | # ]

Bugs in this version.
Authored by: papley on Mar 30, '05 07:21:01PM
There are a few things about this that won't work.

One thing that worked in the previous version but doesn't work in this version is handling more tabs than are able to be displayed in the window. When safari can no longer fit all the tabs in the window a drop-down list is provided to access the remaining tabs.

Because you iterate on the count of radio buttons instead of by the number of tabs (which you don't have direct access to) you'll miss any additional tabs.


set tabCount to count of radio buttons of window frontMostWindow 
repeat with tab from 1 to tabCount
       set tabURL to value of text field 1 of group 1 of ¬
       splitter group 1 of window frontMostWindow
       -- save the url
       set url_list to url_list & tabURL
       my nextTab()


[ Reply to This | # ]