ISO Fastest way to get RSS link off Safari doc

I can’t find keyboard access to the little blue RSS toggle button in Safari 2, I even snooped around menubar nib looking for a connection, but couldn’t find it. I think it should be a simple matter for AS to do a JS in Safari to get this info from the link rel tag, but I’m unsatisfied with the methods that come to mind. Can anyone steer me in a better direction?

Edit: here’s code based on the link I made to my old post. It seems to work ok, I guess I was just hoping for a more direct way of finding specific tags with JS that’s a step up from repeat loops.

tell application "Safari"
	set rss_link to ""
	repeat with x from 0 to 5 by 1
		do JavaScript "document.getElementsByTagName(\"link\").item(" & x & ").type" in front document
		if result is "application/atom+xml" or result is "application/rss+xml" then
			set rss_link to (do JavaScript "document.getElementsByTagName(\"link\").item(" & x & ").href" in front document)
			if rss_link does not contain "://" then
				set _path to (URL of front document)
				tell (a reference to AppleScript's text item delimiters)
					set {tid, contents} to {contents, "/"}
					set {_path, contents} to {(text items 1 thru -2 of _path as text) & "/", tid}
				end tell
				set rss_link to _path & rss_link
			end if
			set URL of front document to rss_link
			exit repeat
		end if
		if x is 5 then beep
	end repeat
end tell

All suggestions and magical alternatives eagerly welcomed.

Not magic, but just the UI way (working here):

try
	tell application "System Events" to ¬
		tell process "Safari" to ¬
			click button 1 of text field 1 of splitter group 1 of group 4 of tool bar 1 of window 1
end try

Otherwise, you may find a better way to handle “link” tags (eg, MacScripter’s front page is “application/rss+xml”).

Thank you for the reply jj. I think the group index changes from machine to machine, but in my case I neglected to mention that I always keep my address bar hidden, and use instead the automatic show/hide behavior of cmd-L for getting around the web.

Good point. Repeat loops are looking better by the minute.

JavaScriptized loops (I think the “absURL” function should convert most or all of relative urls…):

set x to "

for(i=0;i<document.getElementsByTagName('link').length;i++){
	with (document.getElementsByTagName(\"link\").item(i)){
		if (rel == 'alternate' && type.indexOf('application/') == 0 &&  type.indexOf('+xml') == (type.length - 4)) {
			window.location.href = absURL(href);
			break;
		}
	}
}

function absURL(u){
	if (u.indexOf('://') == -1) { // relative url
		if (u.indexOf('/') == 0) { // eg, '/blah.xml' or '/blah/blah.xml'
			return window.location.host + u;
		} else if (u.indexOf('../' == 0)) { // eg, '../blah.xml' or '../../../../blah.xml'
			u = u.split('../'); d = d.split('/'); nu = [];
			d = window.location.href.substring(0,window.location.href.lastIndexOf('/'));
			for(x=0;x<d.length-u.length+1;x++)nu.push(d[x]);
			return nu.join('/') + '/' + u[u.length-1];
		} else { // eg, 'blah/blah.xml' or 'blah/blah/blah.xml'
			d = window.location.href.substring(0,window.location.href.lastIndexOf('/'));
			return d + '/' + u;
		}
	} else {
		return u;
	}
}

"

tell application "Safari" to ¬
	do JavaScript x in document 1

Excellent JJ! That’s exactly what I was looking for. Thank you very much! :smiley: