AppleScript, JavaScript and Safari Tabs
Here is an example of using AppleScript and JavaScript to open a bunch of tabs in a Safari window. This is for opening all the unread threads on a vBulletin powered forum page.
tell application "Safari"
activate
try
set newPostLinks to (do JavaScript "
var links = [];
var myRE = new RegExp(\"newpost\", \"i\");
for(i=0; i<document.links.length; i++) {
if(document.links[i].href.match(myRE) != null) {
links.push(document.links[i].href);
}
}
links;" in document 1)
set i to 1
repeat with newPostLink in newPostLinks
tell window 1 to make new tab
set URL of tab (i + 1) of window 1 to newPostLink
set i to i + 1
end repeat
end try
end tell
In AppleScript, you can tell Safari to execute JavaScript and return a value. The JavaScript loops through all the links on the page and saves the ones that contain "newpost." The last line of the JavaScript, links;
, returns the list to AppleScript. Then we tell Safari to make a new tab and load the URL for each of the links in the returned list.
I have found this to be a nice time saver and use it frequently with FastScripts, a great script launcher.