Trying to add code at the end of an HTML link

Hello again. Still trying to puzzling out the conversion scripts I am working on. I am currently stumped and if possible I’d like someone to point me in the right direction. I currently have an Applescript that finds/replaces some stuff in the clipboard, which is what I need to convert some pages from one format to another. It works fine. Because this is for an HTML based email, and all web clients strip out CSS code, I need to add inline code to make the text be in the font and color I need. Essentially what I’m trying to do here is make all href links look like this:

so a link like

Totoro blankets would look like this:
Totoro blankets

but it’s made more complex since of course we don’t know the text in the href.

If anyone can help me get this figured out, I’ll send you a Totoro blanket ^_^;

The script I was using, for reference, is below. (thanks, tonyhu!)

set thefile to the clipboard as Unicode text
findAndReplace(“href="/”, “href="http://affiliates.jlist.com/click/1872?url=http://www.jbox.com/search/”, thefile)
set the clipboard to the result
on findAndReplace(tofind, toreplace, TheString)
set ditd to text item delimiters
set text item delimiters to tofind
set textItems to text items of TheString
set text item delimiters to toreplace
if (class of TheString is string) then
set res to textItems as string
else – if (class of TheString is Unicode text) then
set res to textItems as Unicode text
end if
set text item delimiters to ditd
return res
end findAndReplace

Model: Mac Pro
AppleScript: 10.5
Browser: Safari 523.10
Operating System: Mac OS X (10.5)

Okay it’s not pretty, but in some brief testing it “seems” to work so give it a shot on your end and let me know!

set theHtml to "<body>
<p>This is some html with a link <a href=\"http://somesite.com/here\">here</a></p>
<p>and</p>
<p>a link <a href=\"http://anothersite.com/woot\">over here</a></p>
</body>"


set fixedHtml to fixLinks(theHtml)

on fixLinks(theHtml)
	set atid to text item delimiters
	set text item delimiters to "<a href=\""
	set linkCount to (count text items of theHtml) - 1
	set text item delimiters to atid
	
	set startPoint to 1
	repeat linkCount times
		set _o1 to offset of "<a href=\"" in (text startPoint thru -1 of theHtml)
		set _o2 to offset of "\">" in (text (startPoint + _o1 - 1) thru -1 of theHtml)
		set theHtml to text 1 through (startPoint + _o1 + _o2 - 2) of theHtml & " style=\"color:black\"" & text (startPoint + _o1 + _o2 - 1) thru -1 of theHtml
		set startPoint to (_o1 + _o2)
	end repeat
	return theHtml
end fixLinks

Cool! That rocks, it works perfectly. Well, almost: sometimes the code is already set to style=“color:black” because I’ve manually done it. So I ended up with this being added to those lines ever time I ran the script. How would I remove that?

This is what it shows for the lines that already have that code:

(Problems with this email? Click here to see it online.)

Hmm, checking it some more. I actually think there’s a problem with the script, since it adds the "style=“color:black” " at the front (a total of 111 of them in that one tag, with jlistmail_r.html), but then in the tables below,where I list products and have text links, the color:black tags are not added. For example:

Restocked Traditional Items. See some fun items back on the site, including the Jizo Pen that looks like a Buddhist statue, a fun Ninja Sword Phone Strap w/ Power Stone attached to it, and our beautiful Winter Kokeshi Doll.

Perhaps the tags in the front are showing up because of a script problem, i.e. it’s adding them to that first tag, not where they go? Hopefully we can get that code added to the href above (and others like it), without duplicating it if it should already exist.

This might make it easier, you can set text to a color by using the tags.

For example:

This text is black!

Maybe adding to the front and to the end of the links will make it easier.

Actually, that does sound like a good idea. A lot easier to trap since it’s at the front.

I modified the script give earlier by adding one thing and it seemed to work.


set theHtml to "<body>
<p>This is some html with a link <a href=\"http://somesite.com/here\">here</a></p>
<p>and</p>
<p>a link <a href=\"http://anothersite.com/woot\">over here</a></p>
<!-- added a few more links -->
<a href=\"link2.html\"> link2 </a>
<a href=\"link3.html\"> link3 </a>
<a href=\"link4.html\"> link4 </a>
<a href=\"link5.html\"> link5 </a>

</body>"


set fixedHtml to fixLinks(theHtml)

on fixLinks(theHtml)
   set atid to text item delimiters
   set text item delimiters to "<a href=\""
   set linkCount to (count text items of theHtml) - 1
   set text item delimiters to atid
   
   set startPoint to 1
   repeat linkCount times
       set _o1 to offset of "<a href=\"" in (text startPoint thru -1 of theHtml)
       set _o2 to offset of "\">" in (text (startPoint + _o1 - 1) thru -1 of theHtml)
       set theHtml to text 1 through (startPoint + _o1 + _o2 - 2) of theHtml & " style=\"color:black\"" & text (startPoint + _o1 + _o2 - 1) thru -1 of theHtml
       -- the line below is what I changed, not sure if -2 is necessary, but it's safer (allows for one href to follow another)
       set startPoint to (startPoint + _o1 + _o2 - 2 )
   end repeat
   return theHtml
end fixLinks

hope this helps

Model: powerbook pro
AppleScript: 2.0
Browser: Firefox 2.0.0.10
Operating System: Mac OS X (10.5)