Working with RTF

I am writing some scripts to work with NoteTaker from AquaMinds and I am trying to append text to the end of a RTF string. I am failing and I am sure it’s due to my lack of AppleScript knowledge as I am a new AppleScript programmer (programmer of 15+ years, just not AppleScript). My script is working beautifully for it’s intended purpose and I wanted to add in this one last feature.

Basically, I have a RTF string (Dictionary says: “RTF (Unicode text): A string containing RTF to be displayed in the entry). I want to append to the end of it in italic, " (Hello World)”.

Any thoughts on how to go about doing this with a RTF string?

Thanks!

To the best of my knowledge, AppleScript doesn’t understand formatting (like italics), so the only way to format text is to use the tools in an application that can.

I believe I made it function, but it seems prone to fail. Looking at the RTF string, it contains just that, RTF escape sequences. All my entries always ended like:

…Hello World}
…Goodbye World}

So, I simply dropped the last character, and added \f0\i1 (Project Name)} back onto the string and all is working. I tried it with items that had bold, italic, other fonts, etc… and it’s working. They always ended with a } and I always want to append right onto the end.

Thanks for the help.

That’s roughly the size of it, Jeremy.

I noticed your question earlier and (since I don’t currently use NoteTaker) thought I’d offer an example of writing RTF to file. However, I didn’t have quite enough time to reply then. Even though you’ve evidently cracked the problem, I’ll post the code anyway - just in case it helps…

This should create a small RTF file on your desktop (if it doesn’t already exist), initialise it, write a short string to it - and then open it in TextEdit:

set p to (path to desktop as Unicode text) & "test file.rtf"
set rtf_text to "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf330{\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}{\\colortbl;\\red255\\green255\\blue255;}\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\\tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural\\f0\\fs24 \\cf0 This is a demonstration RTF document.}"
set f to open for access file p with write permission
set eof f to 0
write rtf_text to f
close access f
tell application "TextEdit" to open p

This should add the required italicised text to the same document file and then reopen it (whether it’s already open or not):

set p to (path to desktop as Unicode text) & "test file.rtf"
set f to open for access file p with write permission
set e to get eof f
if e > 0 then
	set t to read f to e - 1
	set eof f to 0
	write t & "\\f1\\i  (Hello World)}" to f
	tell application "TextEdit" to open p
end if
close access f

Where do you look up all the escape codes for RTF (I mean is there a handy doc) or is the best option to simply cut and paste from TextEdit revealed in BBEdit for example?

Since the RTF specification originated from our very good friends at Microsoft, the Rich Text Format (RTF) Specification, version 1.6, might be a good place to start. (While there are more recent, downloadable versions that rely on installation using .exe files, this one is at least viewable online.)

For details of text attributes not available in standard RTF, and which Apple has extended with custom commands, see the Attributed Strings Programming Guide: RTF Files and Attributed Strings.

Thanks, Kai. Instead of grinding thru all that (though I did look), I think I’ll continue to look at text prepared in TextEdit, for example, in BBEdit to which the formatting is just text.

You could even use TextEdit itself:

tell application "TextEdit" to make new document with properties {text:read (choose file of type {"public.rtf"} without invisibles)}

Nice stuff, kai. And useful. If you know the rtf codes for any edits you want to make in a TextEdit document, it can be tons faster to use the read/write commands and vanilla AppleScript than to use TextEdit itself. And (if you get it right) TextEdit will obligingly “tidy up” afterwards when it opens and saves the document itself.

Knowing your penchant for brevity, I thought you might be interested in this version of your text-appending script. (The ‘as file specification’ is for backwards-compatibility with TextEdit 1.2.):

set p to ((path to desktop as Unicode text) & "test file.rtf") as file specification
set f to (open for access p with write permission)
try
	write "\\f1\\i  (Hello World)}" to f starting at -1
	tell application "TextEdit" to open p
end try
close access f

Characteristically astute, Mr G. :slight_smile: (Thanks, too, for the note about TextEdit 1.2 - which should prove useful for those trying to run this stuff on an older system.)