Get meta tags keywords from a webpage using Safari

(Re-posted for easy search / now that BB comments have been liberated :slight_smile: )

This subroutine returns the contents of a particular meta tag on the current page loaded in Safari.

getMetaTag("keywords") -- this is the tag we're looking for

on getMetaTag(HTMLElementValue)
	set getJavaMetaTag to "document.getElementsByTagName(\"meta\").item("
	set getJavaName to ").name"
	set getJavaContent to ").content"
	set metaTagContents to ""
	tell application "Safari"
		repeat with x from 0 to 5 by 1 -- this looks for the first 5 meta tags
			set JavaElementValue to do JavaScript getJavaMetaTag & x & getJavaName in document 1
			if JavaElementValue is HTMLElementValue then
				set metaTagContents to do JavaScript getJavaMetaTag & x & getJavaContent in document 1
			end if
		end repeat
	end tell
	return metaTagContents
end getMetaTag

This is my first attempt at using JavaScript, but I think it worked out pretty well. At first I tried to use JS code to sift through the tags, but didn’t have much luck getting it right. If you’re more comfortable with ApplesScript (as I am) this is a safe and convenient technique for testing the waters and getting the two to interface.

Edit: If you need those keywords as a list, simply change the second-to-last line to:

return every word of metaTagContents as list