Basic example of creating an RTF document?


I’m trying to create a script that builds an RTF document pre-loaded with some basic information. I don’t want to use templates for this because I have rules about where the new document is to be created based on various attributes that I capture during the creation process.

However, I’m at a loss to figure out how to create a basic RTF document. I’ve never worked with these before, and I can’t seem to find a good, basic example of how this is done.

It looks like I want to create a new record like this:

create record with {name:"foobar", type:rtf, rich text:someText} in myGroup

where “myGroup” varies depending on the date, time of day, type of information, etc.

But how do I define the rich text? I’ve tried things like:

make new text

but DevonThink reports:

So I thought that I needed to make the record and then set content to it, so I tried this:

set newRec to create record with {name:"foobar", type:rtf} in myGroup

but applescript reports “missing value”

I did also try to load the contents of an existing RTF document into the new record using the source attribute:

set newRec to create record with {name:"foobar", type:rtf,source:"/path/to/myFile.rtf"} in myGroup

But what I get in that case is an RTF record with the line “/path/to/myFile.rtf”. Is there some way to open the document, retrieve it’s RTF content, and use that when I create the new record so that I can (hopefully) modify the template source from the script?

Anyway, I’ve tried about a hundred variations and permutations here, playing around with paragraphs and words and so forth, guessing at the correct way, but all I’m doing is spinning my wheels.

Can anyone help? A basic example of creating an RTF document from a script would be really nice.

Many posts in the forum explain this.

Here’s a simple snippet: Can someone create a "split and title" script

So why not make the script inside the template package do all of this for you? You’re already doing the heavy lifting of inventing a script to follow your logic rules. A script’s a script. Bundle it with your doc template, use DEVONthink’s text substitutions if needed (or customize your own) and you have a package that might be more functional. Look inside any of the .templatescriptd packages in ~/Library/DEVONthink Pro 2/Scripts.

The forum is full of custom template examples.

Because I’ve never even heard of a “templatescriptd” before, nor does there appear to be any information about them out there. I looked one over. It appears to be an application directory that is essentially what I would get if I was to compile an application on a Mac. There is a template file buried at the bottom of the directory that is apparently used as a resource. My first guess was that there must be a compiler or interpretor that creates this directory structure.

So I googled 'templatescriptd" but all I found was this page, which isn’t even from Devon Technologies: … rators.pdf

The basic answer is, copy an existing one, change it’s extension to “scptd” and then you can edit the script with the Apple Script editor. Which is, frankly, one of the most kludgey solutions I’ve ever found, on any platform, for any product, ever, anywhere. And I’ve been playing with systems ranging from super computers to embedded Linux since the mid 1980’s.

Am I missing something? Where is the product documentation that explains all of this? I don’t feel like spending the next month grinding on this problem just in order to figure out first-order bits of information that could have been explained with a half dozen pages of product doc.

Honestly, I’ve only been playing with this problem for less than 24 hours, which is exactly how much time I’ve spent looking at Apple Script. My impression is that someone needs to write a tutorial about how to use this stuff if Devon Technologies really wants its customers to use it. Otherwise, they should throw a python, perl, or ruby interpreter into the product and be done with it.

It’s unfortunate you were unable to locate the explanations on the forum for building templates. Actually, like many similar artifacts on OS X, the “.templatescriptd” is not a file but a package. A type of folder that OS X recognizes as a package and does not normally expose to the casual user as a folder. To see the contents, select the package in Finder and choose “Show Package Contents” from the contextual menu. Finder will then reveal the internal folder structure, which is generally something like this:

the above image is the content of the “Project.templatescriptd” smart template.

Here is what the DEVONthink manual says about these artifacts:

The manual (which is, of course, available from DEVONtech’s web site) goes on at length to explain how to create a template. (Conveniently, the manual has extensive information about scripting in general as well as scripting DEVONthink … IMO, the DEVONthink manual is more elaborate and helpful in this regard than almost any other major application I have seen.)

As a coder, I’ve found DEVONthink’s templating implementation to be one of the most sophisticated and extensible tools available to end users. I hope you have a chance to explore the simple but powerful workings of this technology.

1 Like