Adding html tags to textEdit document

I am currently adding text to an html template, writing text in textEdit. It’s rather laborious to add in

tags around every paragraph, even with a shortcut in Dreamweaver.

I have started to fiddle with an AppleScript to run on the front document in textEdit but AppleScript seems to handle paragraphs via returns or line breaks, not the actual paragraph itself.

For example, this script run on the following text returns 9, not 5 as expected:

tell application “TextEdit”
set myParas to count paragraphs of front document
end tell
display dialog myParas

"AN UNEXPECTED PARTY

In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down

on or to eat: it was a hobbit-hole, and that means comfort.It had a perfectly round door like a porthole, painted green, with a shiny yellow brass knob in the exact middle.

The door opened on to a tube-shaped hall like a tunnel: a very comfortable tunnel without smoke, with panelled walls, and floors tiled and carpeted, provided with polished chairs,

and lots and lots of pegs for hats and coats—the hobbit was fond of visitors."

So is adding in a

tag at the beginning of each paragraph and a

at the end going to be a frustrating exercise in counting line breaks or is there an easier way to do this?

Use BBEdit instead of TeachText. It has scriptable grep, among other things. Life is far too short to be spend editing HTML in TextEdit.

For various reasons, that’s not an option, plus it doesn’t answer the question. Obviously AppleScript can manipulate paragraphs, but the script I wrote is indicating that is’s targeting returns or line breaks, not paragraphs of text.

So, is there a way to directly select paragraphs?

All of those blank lines are paragraphs, which is why AppleScript is counting them. They happen to be paragraphs that are empty of any text besides the ending return, but they are paragraphs nonetheless. It’s not an AppleScript thing, that’s just how paragraphs are counted. For example, if you were to write a VBA macro in Microsoft Word, those would be counted as paragraphs there, too.

Your best bet (if you absolutely have to stick with TextEdit) is likely to be first doing a search for \n\n and replace with \n to get rid of those blank lines, then apply your

tags to every paragraph. Or something like that. It’s not going to be pretty doing this in TextEdit, is my guess.

(And this is also why you should always use paragraph formatting and line spacing to separate paragraphs of text instead of blank lines.)

OK, thanks. I suspected as much, I was just hoping that AppleScript had made a shortcut for an obvious situation.

Anyhoo, I fixed it.

I added in the two returns just so I could get the spacing back so that it looks less confusing in the html document (as opposed to having all the paragraphs jammed together in a block of text and p tags), not for any real functionality.

In fact, I don’t even need to do the “if length of the paragraph > 0” as it still works with the blank returns wrapped in p tags as it just adds in the blank lines, but then all those empty p tags are a no-no, even if only from a html readability standpoint.

Then I needed to get rid of the last two returns at the end, again, just because I like things neat, not for any functionality reasons.

And finally I needed my iMac to tell me that I’m brilliant, cos no one else does. Sniff.

set myParas to “” as string

tell application “TextEdit”
set theText to text of front document as string
repeat with zzz from 1 to count of paragraphs of theText
set thisParagraph to paragraph zzz of theText as string
if (length of thisParagraph > 0) then
set myParas to myParas & “

” & thisParagraph & “

” & return & return as string
end if
end repeat
set myParas to text 1 thru -3 of myParas
set the clipboard to myParas
say “You’re brilliant”
end tell