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

A 'Save Tabs as Bookmark Folder' script for Safari Web Browsers
I've been frustrated with Safari's lack of a "Save tabs" as bookmark folder method. Combining a couple of posts mainly this one, I wanted to share a solution. First, download, compile and save this safari_tabs script in your Safari "Scripts folder." From there, when you activate it, it will iterate through every tab of every window and create a bookmark folder with a bookmark for each tab. Note, this is not localized, and has been tested with 10.3.5

[robg adds: I have not tested this one...]
    •    
  • Currently 1.80 / 5
  You rated: 2 / 5 (5 votes cast)
 
[13,826 views]  

A 'Save Tabs as Bookmark Folder' script for Safari | 16 comments | Create New Account
Click here to return to the 'A 'Save Tabs as Bookmark Folder' script for Safari' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: blalor on Sep 22, '04 11:00:31AM

cool!

couple of things (of course):
• how can I add the folder to the bookmarks menu instead of the "collections"?
• why don't the page titles come across, instead of the last component of the URL path?



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: osxpounder on Sep 22, '04 12:21:09PM

Does not compile for me.

Stops at the first "Â" it encounters, saying,

Syntax Error
Expected expression but found unknown token

---
--
osxpounder



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: rhithyn on Sep 22, '04 12:24:53PM

It's a line-break character (special case) for AppleScript. Just delete the offending character and append the next line to the end of the current one (remove spaces too if you feel up to it. That will make it compilable.



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: gdsimms on Sep 22, '04 12:45:26PM

Doesn't work for me. I fxed the "Á" business, compiled and saved the script, accessed it from the script menu and wathced it do its magic. It iterated through each tab in each window I have open, bringing them to the front one after the other, and all that. It ended up by creating a new collection folder in the bookmarks window with the current date and time, but the collection was empty.

I have tried this several times now, and the same thing happens.



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: starfield on Sep 22, '04 03:56:22PM

Doesn't work for me either. :-(
I also fixed all the "Á" errors and it compiled.
I am just a applescript beginner.
I am using Mac OS X 10.3.5 with german (swiss)
I run it directly in safari from the script menu. Nothing happened. Then i run it directly from skripteditor and got the following error "?System Events? got an error: NSReceiverEvaluationScriptError: 4"
Maybe someone more experienced can help?

PS: this is my FIRST post :-)

[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: lionel77 on Sep 22, '04 08:26:22PM
I run it directly in safari from the script menu. Nothing happened. Then i run it directly from skripteditor and got the following error "?System Events? got an error: NSReceiverEvaluationScriptError: 4"

same here...
too bad, this would have been kinda handy...


[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: hypert on Sep 22, '04 10:44:20PM
Same here. I've done some GUI scripting, but I can't quite figure out how else to do what the script is trying to do on this line:

click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 2 of window "Bookmarks"


Also, I just put this in Script Editor, saved, and ran from there.  My Safari doesn't have the Script menu others have mentioned.  I've turned on Safari's Debug menu, but I haven't seen a Script menu.  How do I turn that on?

Thanks!</code></pre>


[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: Frederico on Sep 22, '04 11:59:49PM
This script is a raging example of why GUI scripting is so terribly, horribly unreliable -- with all due respect to the author.

For example, this script as written will:

• fail if the Address bar is not visible
• fail if the Location bar is visible
• double the number of entries for each URL if the Downloads or Activity window is open
• Fail for any other number of differences in what the author and the user has set to display

I can fix a lot of this script with a lot of checks and error handling, but I'm not sure it's worth it.

To the poster directly above, and those above that, you might try swapping out this section for the original (be sure to delete the original section and replace it entirely with this:

-- create a new bookmark folder
tell application "Safari"
	activate
	tell application "System Events"
		tell process "Safari"
			-- add bookmark folder
			click menu item AddBookmarkFolder of menu Bookmarks of menu bar 1
			-- name the folder "Saved Tabs - <current date>"
			set theDate to (current date) as string
			keystroke "Saved Tabs - " & theDate
			-- hit return to name the folder
			keystroke return
			-- programming a tab will not select the bookmarks area, so have to
			-- create a new subfolder
			-- and then delete it
			delay 5
			try
				click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 2 of window "Bookmarks"
			on error
				click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 1 of window "Bookmarks"
			end try
			keystroke return
			keystroke "x" using {command down}
		end tell
	end tell
end tell



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: Frederico on Sep 23, '04 12:09:39AM
OK, here's a couple more fixes:


on addUrlToList()
	-- add the URL to the end of the list
	tell application "Safari"
		activate
		set newURL to the URL of document 1
		--set the end of the URL_list to the URL of document 1
		if URL_list does not contain newURL then set the end of the URL_list to newURL
	end tell
end addUrlToList

The above section will prevent multiple URLs from being added; still need to fix the script so it doesn't check every URL an extra time for every open non-browser window.


-- create a new bookmark folder
tell application "Safari"
	activate
	tell application "System Events"
		tell process "Safari"
			-- add bookmark folder
			click menu item AddBookmarkFolder of menu Bookmarks of menu bar 1
			-- name the folder "Saved Tabs - <current date>"
			set theDate to (current date) as string
			keystroke "Saved Tabs - " & theDate
			-- hit return to name the folder
			keystroke return
			-- programming a tab will not select the bookmarks area, so have to
			-- create a new subfolder
			-- and then delete it
			delay 5
			try
				click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 2 of window "Bookmarks"
			on error
				try
					click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 1 of window "Bookmarks"
				on error
					click button "New Bookmark" of group 2 of splitter group 1 of scroll area 1 of group 3 of window "Bookmarks"
				end try
			end try
			keystroke return
			keystroke "x" using {command down}
		end tell
	end tell
end tell

This second version of the above section should create the bookmark focus required in nearly every possible window configuration (View Location Bar on/off; view Address Bar on/off; etc.)

I still have to fix the condition where the script won't run at all if the view is not that the author tested it under.

[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: abrin523 on Sep 23, '04 01:09:42AM
a few items and an updated script: 1) check the localization items at the top, the previous version was not fully localized 2) the script relies on the optional scriptible UI settings for applescript

-- localizations
property windowMenu : "Window"
property nextTabMenuItem : "Select Next Tab"
property AddBookmarkFolder : "Add Bookmark Folder"
property Bookmarks : "Bookmarks"
property newbookmark : "New Bookmark"
property cut : "x"
property paste : "v"
-- end localizations

property URL_list : {}
property docCount : 0

-- much of this script generously borrowed from previous posts at
-- http://www.macosxhints.com/article.php?story=20030913153245341

-- get a list of the documents

tell application "Safari"
	activate
	set docCount to count of documents
end tell

-- iterate through each tab of window in Safari
tell application "System Events"
	tell process "Safari"
		
		set menuItemCount to count of menu items of menu windowMenu of menu bar 1
		set menuItemOffset to menuItemCount - docCount
		
		set URL_list to {}
		
		-- count the windows
		set winCount to count of windows
		
		-- loop through the windows
		repeat with k from 1 to winCount
			
			set winNum to k
			
			set winTitle to title of window winNum
			
			-- a bit nasty, but the front window is always number 1
			set winNum to 1
			
			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
			
			set firstUrl to ""
			
			-- check for browser window
			if (count of radio buttons of window winNum) > 0 then
				
				-- activate the first tab
				click radio button 1 of window winNum
				
				-- url of first tab
				set firstUrl to value of text field 1 of group of splitter group 1 of window winNum
				
				my addUrlToList()
				my nextTab()
				
				my addUrlToList()
				
				-- url of the next tab
				set nextUrl to value of text field 1 of group of splitter group 1 of window winNum
				
				repeat until firstUrl is equal to nextUrl
					
					-- save the last url
					my nextTab()
					my addUrlToList()
					-- url of next tab
					set nextUrl to value of text field 1 of group of splitter group 1 of window winNum
					
				end repeat
			else
				my addUrlToList()
			end if
		end repeat
	end tell
end tell

-- create a new bookmark folder
tell application "Safari"
	activate
	tell application "System Events"
		tell process "Safari"
			-- add bookmark folder
			click menu item AddBookmarkFolder of menu Bookmarks of menu bar 1
			-- name the folder "Saved Tabs - <current date>"
			set theDate to (current date) as string
			keystroke "Saved Tabs - " & theDate
			-- hit return to name the folder
			keystroke return
			-- programming a tab will not select the bookmarks area, so have to
			-- create a new subfolder
			-- and then delete it
			click button newbookmark of group 2 of splitter group 1 of scroll area 1 of group 2 of window Bookmarks
			keystroke return
			keystroke cut using {command down}
		end tell
	end tell
end tell


-- Safari does some neat things when you paste a URLs while the bookmark list
-- is selected. Specifically, it creates a bookmark for that URL.
-- For each URL in the URLlist, paste it.
tell application "Safari"
	repeat with j from 1 to (count of the the URL_list)
		set this_URL to item j of the URL_list
		-- use a try statement to avoid undefined error
		try
			set the clipboard to this_URL
			-- if the tab has the bookmarks folder open
			-- ignore it, as it will bookmark the bookmarks folder
			if (this_URL is not equal to "bookmarks://") then
				tell application "System Events"
					tell process "Safari"
						keystroke paste using {command down}
					end tell
				end tell
			end if
		end try
	end repeat
end tell


on addUrlToList()
	-- add the URL to the end of the list
	tell application "Safari"
		activate
		set the end of the URL_list to the URL of document 1
	end tell
end addUrlToList

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


[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: Frederico on Sep 23, '04 01:26:01PM

Interesting that you addressed localization issues, but did not take advantage of or offer comment on the other fixes posted. The script as resubmitted, still, has numerous serious flaws based on assumptions of user prefs and open windows that can be addressed.



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: abrin523 on Sep 24, '04 12:18:30AM

Frederico, I think your changes are great and I definitely see the issues you've addressed -- i just didn't see them when I posted the update. I agree that the GUI Scripting is far less than ideal. I'll see about updating it.



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: starfield on Sep 26, '04 06:44:27PM

Thanks alot for your time!
For some reason it still gives me the same error :-(

NSReceiverEvaluationScriptError: 4

While running the script with skripteditor. (It shows the error while highlighting the following line:

count of menu items of menu windowMenu of menu bar 1

Too bad all applescripts I have heard of or found that enable similar things (like saving and restoring the state of windows and tabs) never work on my mac :-(
It would be a real time saver.



[ Reply to This | # ]
Another way to go
Authored by: macstansbury on Sep 23, '04 11:06:50AM
It isn't ready for primetime, yet, but SafariStand is a plugin for Safari that will do a lot of things, but also has a Bookmark Shelf feature that will let you open a bunch of tabs, then save them and open them later. It is a separate window, but it works how it's supposed to, and the author would take kindly to input from the English speakers. Get SafariStand from here.

[ Reply to This | # ]
Just use Safari Extender
Authored by: bakalite on Sep 23, '04 12:36:02PM

Safari Extender does something very similar to this. There are scripts out there that work, but everytime something changes the scripts break. Safari Extender is maintained by the author, a well known shareware developer. To me, it's worth a small fee to have this (and other) features available reliably.

http://batista.org/extender.html



[ Reply to This | # ]
A 'Save Tabs as Bookmark Folder' script for Safari
Authored by: Anonymous on Sep 25, '04 01:05:14AM
Saft does this as well.

[ Reply to This | # ]