TextWrangler - appending suffixes to lines of html

Hi everyone,

As part of a larger applescript that I won’t get all into, I’ve ended up with one or more .html files which has the proper beginning tags for each line of html, but lack all the ending for every line.

Fortunately, I can use a generic suffix for every line - that suffix would be “

”, since those are the only prefixes used in any of the documents.

So, what I’d like is to use AppleScript to instruct TextWrangler to “add suffix” to every line of every .html document in a given folder.

For some reason I’m stumped. I won’t post my own attempt because it is probably so bad it will just confuse the discussion.



no it won’t. In fact it would help us a great deal.

If you really think it’s that bad, then start from scratch - messy code can block your brain anyway.

So, I recomend you start again, having this forum in mind, and post the code you have so far.
That way we have a better idea of where you are stuck at.

This is a good idea anyway when you are working on a bigger project, to make a lot of little scripts working together.

Okay, well, here’s the big picture: I’m writing a book in .rtf, using DevonThink. What I want is to write an AppleScript that will convert that .rtf into .html and then email it to my @kindle account, so that I can in one stroke transfer a copy of my book to my Kindle. Since the Kindle is very basic in terms of reading html tags, I want to use TextWrangler to edit the html, removing most of the junk, and replacing it with the simple tags


, and (as well as adding the custom Kindle page break tag, <mbp:pagebreak />.

So anyway, here’s the beginning of the Applescript I’ve written - which works fine (with one slight issue*). The search strings are simply the more complex html tags that the DevonThink converter put into the document. (Each line in the original html ends with ).

tell application id "com.devon-technologies.thinkpro2"
	set theselection to selection
	repeat with therecord in theselection
		set theConvertedRecord to convert record therecord to html
		export record theConvertedRecord to "/Users/writing/Desktop/Converted"
	end repeat
end tell
tell application "TextWrangler"
	replace "<div align=\"center\" class=\"div0\"><font class=\"font0\">" searching in "/Users/writing/desktop/Converted" using "<mbp:pagebreak /><h1><center>"
	replace "<div align=\"center\" class=\"div1\"><font class=\"font0\">" searching in "/Users/writing/desktop/Converted" using "<p><center>"
	replace "<div align=\"justify\"><font class=\"font0\">" searching in "/Users/writing/desktop/Converted" using "<p>"
	replace "</font></div>" searching in "/Users/writing/desktop/Converted" using ""

What I end up with after these commands is an html document where, in the html body lines, each line has the proper beginning tags (either


, sometimes with ). But none of these lines has any end tag at all.

So what I want to do is to instruct TextWrangler to append the suffix “

” to every line in these documents. I’ve done this manually and the document comes out working great.

Here’s what I’ve tried: (continuing from above)

set theFolder to "/Users/writing/desktop/Converted"
	repeat with theFile in theFolder
		repeat with theLine in theFile
			add suffix "</p></h1></center>"
		end repeat
	end repeat

But I get the following error message: "Textwrangler got an error (MacOS Error code -1701). It highlights the Applescript line that begins “add suffix.”

So that’s the whole story (so far)!

Thanks in advance to everyone for your help.

    • the problem with this early part of the script is that I have to click a Confirm Save dialogue every time a replace command is finished - “Save changes before continuing?” – so I guess I need to put in some kind of “save document” line after each replace, correct?


to append a specified suffix to each line with TextWrangler you can use something like this,
the script processes all files in the folder Converted on Desktop with extension .html

set theFolder to ((path to desktop as text) & "Converted:")
tell application "Finder" to set theFiles to files of folder theFolder whose name extension is "html"
repeat with oneFile in theFiles
	tell application "TextWrangler"
		open (oneFile as alias)
		add suffix text of document 1 suffix "</p></h1></center>"
		close document 1 saving yes
	end tell
end repeat

As the script saves all modified files, test it first with one file.

I’d recommend a more reliable version which could check each line for each tag and add only the existing counterparts.

great, now we’re talking :slight_smile:
please hang on, I’ve got to check some code…

Edit: added:

from TextWrangler.sdef

that makes me think:

replace "searchText" searching in "convertedFolder" using "replacementText" saving yes

should keep that dialog away.

then if every line ending shall receive the same suffix, I’d try:

replace return searching in "convertedFolderPath" using "suffix" saving yes
-- or
replace "\n" searching in "convertedFolderPath" using "suffix" saving yes
-- no windows newlines (I guess)

also TW supports scriptable grep, fun ahead as the chance of making awful html seems large.