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.
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.
Share Improve this question asked Nov 18, 2008 at 16:01 FourFour 1915 silver badges8 bronze badges 1- if/when you figure this out - you definitely need to share it back to the world.. I'd love something like this! – warren Commented Nov 18, 2008 at 16:12
5 Answers
Reset to default 2For 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!
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
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./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.
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/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;
}
}
}
Here is a script for AppleScript that should get you going (this script is for Safari)...
property these_URLs : {}
set the text item delimeters of AppleScript to ""
----------------------------------------------------------------------------------
--Initialize the program
set the action to the button returned of (display dialog "Create or view a list of favorites..." buttons{"Cancel","View","Create"} default button 3)
if the action is "Create" then
create_text_file()
else
open_favorites()
end if
---------------------------------SUBROUTINES--------------------------------------
on create_text_file()
--get the URL of every tab of every window
tell application "Safari"
set theWindows to (get every document) as list
repeat with i from 1 to the count of theWindows
set this_Window to item i of theWindows
set theTabs to (get every tab of this_Window) as list
repeat with x from 1 to the count of theTabs
set this_Tab to item x of theTabs
set this_URL to (the URL of this_tab) as string
set the end of these_URLs to this_URL
end repeat
end repeat
end tell
--put the URLs into a text document
set newFile to (choose file name with prompt "Choose a name and location for the new file:" default location (path to desktop folder))
try
open for access newFile with write permission
set the text item delimiters of AppleScript to return
set the_URLs to these_URLs as string
write the_URLS to file newFile
close access newFile
on error
try
close access newFile
end try
end try
set the text item delimiters of AppleScript to ""
end create_text_file()
on open_favorites()
--Verify whether you have saved any favorites with this script
if these_URLs is {} then display dialog "You have not added any favorites with this script." with icon note buttons{"Cancel"}
--there are favorites so open all the URLs you stored in the text file
repeat with i from 1 to the count of these_URLs
set this_URL to item i of these_URLs
tell application "Safari" to make new tab at the end of tabs of document 1 with properties {URL:this_URL}
end repeat
end open_favorites
If you have any questions, just ask. :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347647a4569783.html
评论列表(0条)