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


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