a script to collect tab URLs & titles

Mitchell L Model MLMLists at Comcast.net
Sun Apr 3 21:26:09 PDT 2005


Here is a script that makes an HTML page of links 
with the titles and URLs of all the
tabs of the current browser.  (Tabs don't have 
titlesin OmniWeb 5's \AppleScript dictionary --
the script steps through all the tabs and takes 
each one's title from the browser.)
The script prompts for a filename to which the 
HTML will be written.  At the end
the script opens the file it created -- I'm not 
sure what the best approach is for that:
open a new browser every time, open a new tab in the current browser, or open
a new browser the first time but reuse that s a special "tab URLs" browser if
the script is run again.

To install, extract this text, put it into a 
script in Script Editor (or whichever tool you
use to edit AppleScripts -- I use Late Night Software's wonderful Script
Debugger, but it's expensive), and save it to your OmniWeb 5 script
folder (Application Support:OmniWeb 5:Scripts in either ~/Library or /Library).
I called it "Collect URLs of Tabs", but you can 
make up your own name for it, of course.

Is this too long to post here?  Would it have 
been better to stop here and put the
rest on a web page for people to download?  Had I done that I could have posted
a compiled script rather than a text file so 
people wouldn't have to go into a script
editor to use it.  I'd like to hear what people 
think about this -- this script seems to
me to be around the border beyond which things are too long to include in posts
to the list, but I never know what's expected.
__________________________________ CUT _______________________________________

-- Save the URLs and titles of all tabs in OmniWeb's front Browser (Window)
-- as links in an HTML file whose name is 
obtained through the usual file dialog.
-- When finished, open the file.

-- Mitchell L Model, mlm at acm.org

(*
In addition to its utility this script 
demonstrates some aspects of AppleScript that
might be helpful to people learning (more) Applescript programming including:

	OmniWeb:

		accessing tabs of the front browser (window)
		getting the URL of a tab
		making a tab current
		getting the title of the front browser
			(which it takes from the current tab)

     Generic:

		global variables
		top-level script statements other than "tell" and "on"

		subroutines
		conditionals
		repetition

		prompting the user for a file name
		handle user cancellation of a file dialog
		opening files for writing
  		closing files

	 	exception handling
		passing along an exception

		creating records
		access fields of records
		adding items to a list

*)

------------------------------ Global Constants ------------------------------

-- These appear before the code so that they don't distract
-- from the meaningful parts of the script.  Each is actually
-- used in just one place.

-- the text to print out to start the HTML page
set startString to "<html>
	<head>
		<title>
			OmniWeb Tab URLs
		</title>
	</head>
	<body>
		<h1>
			OmniWeb Tab URLs
		</h1>
		<ul>
"

-- the text to print out to conclude the HTML page
set endString to "		</ul>
	</body>
</html>
"

-- the text to print out before each URL
set listItemStartString to "			<li><a href=\""

-- the text to close the link to the URL
set listItemEndString to "</a></li>
"

----------------------------------- "Main" -----------------------------------

set filnam to askFileName()
if filnam is null then return -- user cancelled

(*	fil is used as a global throughout because otherwise it's tricky to
	set and/or return it from within openFile's try (below), but also for
	convenience since nearly all the 
subroutines refer to it 			*)
set fil to my openFile(filnam)

my writeURLs()
close access filnam

----------------------------- OmniWeb Subroutines --------------------------

on collectUrls()
	tell application "OmniWeb" -- no need to activate
		set urls to {}
		set theBrowser to front browser
		repeat with tb in tabs of theBrowser
			set active tab of theBrowser to tb
			copy {addr:the address of 
tb, name:the name of theBrowser} ¬
				to the end of urls
		end repeat
		urls
	end tell
end collectUrls


----------------------------- Generic Subroutines ----------------------------

on askFileName()
	try
		return choose file name default name "OmniWeb Tabs.html"
	on error errmsg number errnum from errobj
		if errnum is -128 then
			return null -- user cancelled
		else
			error errmsg number errnum from errobj -- pass it along
		end if
	end try
end askFileName

on openFile(filnam)
	global fil
	try
		set fil to open for access filnam with write permission
	on error errmsg number errnum from errobj
		-- open closing first if already 
open; this can happen when the script
		-- is being debugged or gets interrupted then rerun again
		if errnum is -49 then -- close 
and reopen, without error protection
			close access filnam
			set fil to open for access filnam with write permission
		else -- some other error occurred; just pass it on
			error errmsg number errnum from errobj
		end if
	end try
	set eof of fil to 0
	fil
end openFile

on writeString(str)
	global fil
	write str to fil
end writeString

on writeURLs()
	global startString, endString
	my writeString(startString)
	my writeListItems(my collectUrls())
	my writeString(endString)
end writeURLs

on writeListItems(urls)
	global listItemStartString, listItemEndString
	repeat with u in urls
		writeString(listItemStartString)
		writeString(the addr of u)
		writeString("\">")
		writeString(the name of u)
		writeString(listItemEndString)
	end repeat
end writeListItems


----------------------------------- The End ----------------------------------




More information about the OmniWeb-l mailing list