Suppose I have a plain txt file in a text editor such as TextEdit:

title 1
http://a.b/c

title 2
http://d.e/f

...

I'd like to convert all the lines beginning with http:// to HTML code for URL, so that the aforementioned content will become:

title 1
<a href="http://a.b/c">http://a.b/c</a>

title 2
<a href="http://d.e/f">http://d.e/f</a>

...

How can I get this done in Automator or AppleScript? (My current solution is using Gmail, but it involves multi-step copy-paste.)

Thank you very much in advance.

share|improve this question
feedback

2 Answers

up vote 1 down vote accepted

This will let you avoid another editor:

set inFile to "/Users/you/Desktop/Urls.txt"
set outFile to "/Users/you/Desktop/Urls2.txt"

do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of outFile
share|improve this answer
This AppleScript works perfectly! Thank you. – user1870433 Dec 3 '12 at 0:14
feedback

Just do a regex search and replace in a text editor or Terminal:

sed -E 's|^(http:.*)|<a href="\1">\1</a>|g' file.txt
share|improve this answer
Thank you very much for the quick and helpful response! Since the Terminal method only replaced the first URL, I looked for a Mac application that supported regex expression. I got MacVim. There is a "Convert to HTML" menu command in the "Syntax" menu of MacVim that accomplishes the replacement I wanted perfectly. Thank you. – user1870433 Dec 2 '12 at 16:46
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.