A Safari script to open links located in a webpage automatically ?

Does anyone heard about a script to let Safari automatically open all the links located in a web page, either in different tabs, or with different pages ? I have to check pages with more 20 links, and it is a bit tiresome to click twenty times to the links.

Thanks in advance for any information or advices.

Hi Tanaka

Here is a script to open in tabs all links in the html of the front document in Safari.

There are some problems with this script. One being that it doesn’t deal with javascript created links, another being that it only finds links in quotes - I’m sure there are more.

--Script to open page links in Safari tabs
-- John Maisey 16/5/5
--
set myList to {}

tell application "Safari"
	set myURL to URL of front document
	set URLsource to source of front document
end tell

set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "href=\""}
set myChunks to (text items 2 thru -1 of URLsource)
set AppleScript's text item delimiters to myTID

repeat with myChunk in myChunks
	set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "\""}
	set myList to myList & (text item 1 of myChunk)
	set AppleScript's text item delimiters to myTID
end repeat

repeat with myListNo from 1 to length of myList
	set myLink to item myListNo of myList
	if myLink does not contain "://" then
		set myCount to -2
		repeat
			if (myLink starts with "../") or (myLink starts with "./") then
				set myLink to (characters ((offset of "/" in myLink) + 1) thru -1 of myLink) as text
				set myCount to myCount - 1
			else
				exit repeat
			end if
		end repeat
		set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
		set myBase to (text items 1 thru myCount of myURL) as text
		set AppleScript's text item delimiters to myTID
		set myLink to (myBase & "/" & myLink) as text
		set (item myListNo of myList) to myLink
	end if
end repeat

tell application "Safari"
	repeat with theURL in myList
		my new_tab()
		set the URL of document 1 to theURL
	end repeat
end tell

--credit http://www.macosxhints.com/article.php?story=20030414185226343
on new_tab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
end new_tab

Best wishes

John M

AppleScript: AppleScript 1.9.3
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

I use this (but I don’t remember its features, though :rolleyes: )

sfriExtractLinks()

to sfriExtractLinks()
	try
		tell application "Safari" to rest of paragraphs of (do JavaScript "
function mf(obj){
	if (obj.frames.length == 0) { // extract links from this page
		for (q=0;q<obj.document.links.length;q++){
			processLink(obj.document.links[q].href);
		}
	} else { // rotate again
		for (q=0;q<obj.frames.length;q++){
			mf(obj.frames[q]);
		}
	}
}
//var okURLs = '%%http://%%https://%%ftp://%%mailto://%%file://%%news://%%hotline:%%ed2k://%%carracho://%%';
function processLink(l){
	if (z.indexOf('\\r'+l+'%%%') == -1 && l != '' && l.indexOf('javascript') != 0 && l.indexOf('#') != 0) {
		if (l.indexOf('http://') == 0 || l.indexOf('https://') == 0 || l.indexOf('ftp://') == 0  || l.indexOf('mailto://') == 0  || l.indexOf('file://') == 0  || l.indexOf('news://') == 0 ) { // añadir, sin más
			z += '\\r' + l + '%%%';
		} else {
			if (l.indexOf('/') == 0) { // eg, '/index.html'
				docHost = getHost(top.window);
				petabyte = '\\r' + docHost + l + '%%%';
				if (z.indexOf(petabyte) == -1) { z += petabyte }
			} else { // eg, 'index.html' or '../index.html'
				docURL = getURL(top.window);
				ns = l.split('../');
				if (ns.length == 1) { // eg, 'index.html' or 'kaka/pepe.html'
					petabyte = '\\r' + docURL + '/' + ns + '%%%';
					if (z.indexOf(petabyte) == -1) { z += petabyte }
				} else { // eg, '../index.html' or '../kaka/index.html';
					lessURL = docURL.split('/');
					lessURL = lessURL.slice(0,lessURL.length - ns.length + 1);
					lessURL = lessURL.join('/');
					petabyte = '\\r' + lessURL + '/' +  ns[ns.length - 1] + '%%%';
					if (z.indexOf(petabyte) == -1) { z += petabyte }
				}
			}
		}
	}
}
function getHost(w){
	return w.location.protocol + '//' + w.location.hostname;
}
function getURL(w){
	louro = w.location.href;
	zwx = louro.lastIndexOf('/');
	louro = louro.substring(0,zwx);
	return louro
}

w = top.frames.length;
z = '';
if (w == 0) {
	x = top.document.links.length;
	for (i = 0; i < x; i++){
		processLink(top.document.links[i].href);
	}
} else {
	fframes = true;
	for (i = 0; i < w; i++){ // repeat with every frame
		if (top.frames[i].location.href) {
			mf(top.frames[i]);
			fframes = false;
		}
	}
	if (fframes) {
		for (sfriQ=0;sfriQ<document.links.length;sfriQ++){
			processLink(document.links[sfriQ].href);
		}
	}
}
z = z.split('%%%');
z = z.join('');
z" in document 1)
	on error
		{}
	end try
end extractLinks