PDA

View Full Version : AppleScript input is text file and output is list of links in Firefox


thomasrawley
07-01-2007, 11:33 AM
I just bought my first mac and I am therefore new to AppleScript. Here is what I am trying to do.

I have a text file (.txt) of URLs where each URL is on its own line (I think AppleScript calls this a paragraph). The text file looks like what is below.

http://www.somewhere.com/somewhere
http://www.someotherplace.com/someotherplace
etc...

The number of links in the file can change so I would imagine some looping function is in order.

I want to actually turn each one of these links into HTML links and open Firefox to display a list of the links. To continue with the example, I would want to see this in Firefox.

<a href="http://www.somewhere.com/somewhere">1</a>
<a href="http://www.someotherplace.com/someotherplace">2</a>
etc...

The 1 and the 2 can be whatever really...if they are the same as the URL that is fine.

Any thoughts?

Thanks,

Thomas

tw
07-01-2007, 03:55 PM
hmmm... your question is a little unclear. for instance, simply opening a text document like that in Microsoft Word will automatically create links that you can click on. you could use applescript to transform that text list into an html file, save it, and then open the file you just saved in Safari, I suppose, but it sounds like you're looking for something more dynamic than that. if so, you probably want to use PHP, not applescript.

thomasrawley
07-01-2007, 06:02 PM
Basically I am trying to transform a text file of links into an HTML page that lists all the links and make sure they are clickable. My intent is to then open that page in Firefox so I can use the DownloadThemAll extension to download the files that the URLs point to all at once (instead of clicking on each one and downloading them individually).

I am proficient with PHP but that would require the PHP file to be hosted somewhere and the user would have to upload the text file of links so that PHP could process the file.

Also, I just want to check out what AppleScript can do...

Thanks,

Thomas

melon
07-01-2007, 06:20 PM
You could even install an extension for Firefox like Linkification (https://addons.mozilla.org/en-US/firefox/addon/190) and open the text document in there, the plugin automatically parses the text links and converts them to clickable ones.

thomasrawley
07-01-2007, 07:08 PM
Linkification works great! Thanks for the tip.

Thomas

tw
07-01-2007, 07:17 PM
Basically I am trying to transform a text file of links into an HTML page that lists all the links and make sure they are clickable. My intent is to then open that page in Firefox so I can use the DownloadThemAll extension to download the files that the URLs point to all at once (instead of clicking on each one and downloading them individually).

ah, ok. this isn't difficult. I'd do it this way (probably some errors in here, but you'll get the idea...):


-- init
set textPath to alias <path to text file with links>
set htmlPath to alias <path to where you want to save your html file>

-- set up html header
set headerText to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" & return & "<html>" & return & "<head>" & return & "****** content=\"text/html; charset=us-ascii\" ***********=\"content-type\">" & return & "<title>Whatever</title>" & return & "</head>" & return

-- get file data
set fp to open for access textPath
set linktext to read fp
close access fp

-- make links
set bodyText to {"<body>"}
set links to every paragraph of linktext
set applescript's text item delimiters to ""
repeat with thisLink in links
set end of bodyText to ("<a href=\"" & thisLink & "\">" & thisLink & "</a>") as text
end repeat
set end of bodyText to "</body>"
set end of bodyText to "</html>"

-- write html to disk
set applescript's text item delimiters to return
set htmlText to headerText & (bodyText as text)
set applescript's text item delimiters to ""
set fp to open for access htmlPath with write permission
write htmlText to fp as text
close access fp
tell application "Firefox"
open htmlPath
end tell

thomasrawley
07-01-2007, 09:21 PM
Thanks for the code! This helps out a lot. It blows my mind how easy it is to write AppleScript code...it is one of the most user friendly languages to read ever!

Thanks again.

Thomas