Batch reformatting font (but retain bold and italic) for rtfd files

Looking for valuable help from this forum:
I have written a script that performs batch reformatting on a selection of my annotation notes by (1) changing all fonts to Courier New but retain all original bold and italic format (2) deleting all extra lines between paragraphs.
The script works for the most part but with two big issue:
(1) The first section of my annotation note is a 2-row x 3-col table. All text seems to be reformatted as expected in the DT3 view pane after the running the script. However, when the file is opened in an external editor (TextEdit and Pages), I found that everything in the annotation file is being placed into the cell of an 1x1 table.
I think there are two ways to work around this issue: script to start the formatting at paragraph 7 (if the table has 2x3 = 6 paragraphs from the top?) OR script to dissolve/convert the table back into 6 paragraphs before starting the reformat process. I have no idea how to script these work arounds.

(2) After the reformat, some/most images in the file are now laying on top of some text and there is no way to move the image. When I open the same file in external editors, the images are displayed normally (ie not laying on top of other text). I have no idea why it happened. Note: strangely, even I change my script (for testing, I use “tell selected text of think window 1” instead of “tell text of theRecord”, and take away those lines relating to “repeat with theRecord …” ) to perform the reformatting within a document window by selecting everything except for the table, the sticky and overlapping images still occur.

Thank you very much in advance.

My code: (BTW, still haven’t figured out how to place programming code in this forum in tidy and easy manner…)

tell application id "DNtp"

set theSelection to the selection
if theSelection is {} then error “Select an item, please”
repeat with theRecord in theSelection
try
tell text of theRecord
set (font of every attribute run whose (font contains “Bold”)) to “Courier New Bold”
set (font of every attribute run whose (font contains “Italic”)) to “Courier New Italic”
set (font of every attribute run whose (font does not contain “Bold” and font does not contain “Italic”)) to “Courier New”
set {alignment, line spacing, paragraph spacing, minimum line height, maximum line height} to {left, 0, 15, 0, 15}
set size of paragraph 1 to 12
set font of paragraph 1 to “Courier New Bold”
set alignment of paragraph 1 to justified
end tell

  	tell text of theRecord
  		set i to 1
  		set cnt to number of paragraphs
  		repeat while i ≤ cnt
  			if length of ((text of paragraph i) as string) is 1 then
  				set text of paragraph i to ""
  				set cnt to cnt - 1
  			else
  				set i to i + 1
  			end if
  		end repeat
  	end tell
  end try

end repeat
end tell

Just enclose your code with [code] … [/code]. A document demonstrating these issues would be useful.

Your script is doing far more than changing fonts, as you know, so you should troubleshoot it by removing the code that does not change fonts, running it and seeing if the excised code is the problem. I suspect it is the “remove extra lines” issue. You might be more successful with something like TextSoap, since writing scripts to manipulate RTF is notoriously fraught.

Thanks. I will do a step-by-step trouble shooting, and go for ready-made solution if unsuccessful.

Just send a message to u with example. Thanks.

Could you upload a copy of one of the files you are converting so we can test the code on it?

By the way, I am writing my own script to reformat clutter-free RTFs and make them more legible, and this code of yours is exactly what I needed.

@ngan and @dansroka
It’s nice to see you kids playing with the new toys :wink: :slight_smile:
Always something to learn.

1 Like

I am just putting together different lines of codes from other forum members (and thanks to them!)


Half the battle is finding this stuff!

The other half is learning to do it yourself … ohhh, snap! :wink: :stuck_out_tongue:
Couldn’t resist. :slight_smile:

With Applescript’s arcane and unpredictable syntax, I don’t know how much you can learn without copying others!

With Applescript’s arcane and unpredictable syntax,

I disagree with this. There’s nothing arcane about it. It’s a simple plain English language that gives a non or beginner-programmer very powerful tools.

And as far as unpredictable? That’s either misunderstandings of simple things or due to how a developer implemented things.

I taught myself from scratch, starting from a simple project and progressing to doing things people said weren’t possible to do outside of Objective-C. :slight_smile:

PS: I’m a rare bird in that I have AppleScripted in the corporate world professionally for 20+ years. I even built inter-departmental scripted workflows for a multi-national, multi-billion dollar corporation.

Well there you go! You are a rare bird! :wink:

I’ve taught myself everything from Pascal (back in the day) to PHP and Javascript. But I just seem to have a block with Applescript. Once I see a script, it udally makes sense, and I can usually modify it, etc. But I find it hard to predict that “sense” beforehand.

It’s an interesting phenomenon I’ve witnessed for a very long time that people who start on terse languages very often have a mental block at the simplicity of AppleScript.

CSS: “font-size:10px;”

Applescript: “Hey now, if you don’t mind, you see that text up there on the screen? No, not that text, the other text. Yes, that’s the one. Now, if you’d be so kind, could you make it 10px in size? Yeah, 10px. That’d be great.”

:wink: :stuck_out_tongue:

CSS: “font-size:10px;”

Haha! That’s a totally different mechanism, and one I’m terribly fond of. I’m not a fan of RTF and am an advocate of Markdown due to things like that. :slight_smile:

Just a follow up:
I tried my best to troubleshoot the script and found out that
(1) whenever there are tables in rtf/rtfd, any command attempting to adjust paragraph or line spacing will likely create unforeseeable outcome (in my case all text are placed within an 1x1 table)
(2) attempting to clean extra lines is not easy. Different methods all work differently to a certain extent.

This is the final code I use, and I am happy with the results. If there is no table in the note, paragraph and line spacing codes can be used.

tell application id "DNtp"
	set theSelection to the selection
	if theSelection is {} then error "Select an item, please"
	repeat with theRecord in theSelection
		try
			tell text of theRecord
				set (font of every attribute run whose (font contains "Bold")) to "Verdana"
				set (font of every attribute run whose (font contains "Italic")) to "Courier New Italic"
				set (font of every attribute run whose (font does not contain "Bold" and font does not contain "Italic" and font does not contain "Verdana")) to "Courier New"

(*
				set alignment of text to left
				set paragraph spacing of text to 20
				set line spacing of text to 5
*)

				reformat text
			end tell
		end try
	end repeat
	display alert "batch processing completed" giving up after 1
end tell
2 Likes