views:

2981

answers:

4

Topic: Programmatically manipulate web browser in OS X 10.4.x+ Tiger/Leopard. Subjects: Webkit, Safari, Firefox, APIs, Applescript, Automator, Javascript, Ruby, Ruby on Rails, OS X, Tiger Goal: Collect/Read/Extract URLs from Safari into text (Ruby on Rails code) file. Note: A solution that uses FF would be very appreciated, too. I use Safari (v. 3.x, OS X 10.4.x) more and much prefer a solution that works in Safari.

At times, I use the web browser to find/display multiple site pages that I 1) want to visit again later and 2) the URLs of which I want to group together in a text file for a) future reference and/or b) programmatically manipulate.

For example: In today's NYT I find seven NYT articles I want to post to my del.icio.us acct. and share via email in their "printer friendly" format long after they are headlined in that day's online edition. I open each one in a browser window's tap, then Presto! their URLs automagically are wooshed into a file where a (custom) Ruby on Rails app sends the print versions' URLs to email addresses and my Del.icio.us acct.

I figure there's a way to do the URL extracting step from the OS using Applescript or Automator. I figure there MAY be a way to do it with Javascript.

My Question: How to read the web browser's tabs' location field and collate these strings into a text file (either within my OS or over the wire to a web app.)?

Much appreciated.

+1  A: 

If you desire to do this is Firefox you're going to have to learn and use XUL. The tabs are not visible to javascript (security/privacy concerns) and there's no API for this sort of information outside of firefox.

Getting Started with XUL development is a good resource for how to start programming in XUL.

IBM has a decent tutorial for your first XUL code.

Here is a tab handler in XUL to give you an idea of how to deal with tabs.

A short tutorial that demonstrates mixing XUL, javascript, and other technologies to update a website.

Lastly, here's a good lifehacker tutorial on firefox extensions/add-ons which shows you much of the above for a simple example, and then how to package it as an .xpi so others can easily add it to and manage it from their firefox.

I don't have any insight into Safari, but it's based on webkit and there should be some resources on customizing it similar to how you'd use XUL on firefox.

Good luck!

Adam Davis
+1  A: 

For Safari, this would be pretty trivial to do with Applescript. I'd suggest starting with something like Bookmark all tabs to get the basic tab-grabbing logic that you'll need, and maybe merge it into John Gruber's old Save and restore Safari URLs script to save the URLs as a list to a text file.

There may be better Applescript solutions out there, too; those were just the first I found via Google, and both are pretty badly dated.

For further help and resources pertaining to Applescript, I suggest the MacScripter forums.

Good luck!

One Crayon
A: 

The most simple solution in Firefox is "Bookmark all open tabs" (in the bookmark menu). Give this "bookmark folder" a specific name. You can then go into your profile (http://support.mozilla.com/en-US/kb/Profiles) and open the file "bookmarks.html" which contains all the info you want and then some.

If you want to use a UI, check out the Taboo and the "Sitzungs-Manager" (probably Session-Manager). Both are for Firefox 3.0+

Taboo allows you to save a tab for "later reading" (kind of like the bookmark menu but just a single click; will save a screenshot of the page, too).

The session manager plugin allows you to save the currently open tabs and windows and restore them later.

Aaron Digulla
A: 

You can modify this bit-0-code to get you started. I used this to build my own personal bookmarking plugin for one tab. You can pop-up a list of urls in a xul window and then select the ones you want to post / arrogate to one place.

function getGetDocData(){
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
               .getService(Components.interfaces.nsIWindowMediator);
var mainWindow = wm.getMostRecentWindow("navigator:browser");
var tab  = mainWindow.getBrowser().selectedTab;
titleDG = tab.label;
for(var i = 0; i < window.opener.length; i++){
 var doc = window.opener[i].document;
 if(doc.title == tab.label){
  hrefToDG = doc.location.href;
 }
}

}

doyle