Build Link List from Safari Window

The previous post reminded of one of the oldest scripts I have and still use regularly. I regularly have to gather information found on different webpages and email them to somebody. I try to gather all the relevant webpages in tabs in one Safari and then run this script. It will look at all the tabs in the frontmost Safari Window and build a plain text list out of the titles and links and place that in clipboard, ready to paste into an email or elsewhere.The result will look like this:

Apple
<http://www.apple.com/>

Google
<http://www.google.com/>

Scripting OS X | #! is not a curse word
<https://scriptingosx.com/>

The script is fairly straightforward:

global linkText

on run
	set linkText to ""

	tell application "Safari"
		activate
		set w to window 1

		set n to 0
		try -- this will fail for the downloads window
			set n to count tabs of w
		end try

		if n > 1 then
			repeat with t in every tab of w
				my appendLineWithDoc(t)
			end repeat
		else if n = 1 then
			my appendLineWithDoc(document of w)
		end if
	end tell

	set the clipboard to linkText

	return linkText
end run

on appendLineWithDoc(theDoc)
	tell application "Safari"
		tell theDoc
			try
				set linkText to linkText & name
				set linkText to linkText & return & "<" & URL & ">" & return & return
			end try
		end tell
	end tell
end appendLineWithDoc

You can save this script in your ~/Library/Scripts/Applications/Safari  folder and enable the Script menu and it will be shown only in Safari.