Get the description when doc is closed

In some of my scripts rarely used, I added an handler to display some instructions.
This handler is called if a week has passed since the last launch.
Now, I would like to refer to an external library.

The instructions displayed to the user are picked from the Description.rtfd resource.
For that, my script is saved as a bundle.
As I’m french, I need to use accentuated characters.
This is the point: the “read file” Applescript command returns the RTF code for each accentuated character.

Do you have any suggestion for a workaround?

If you wish to read an .rtfd file as plain text, you can use AppleScriptObjC or do shell script and the textutil command. here’s some sample ASObjC code:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theURL to current application's |NSURL|'s fileURLWithPath:"/Users/shane/Desktop/Untitled.rtfd"
set theWrapper to current application's NSFileWrapper's alloc()'s initWithURL:theURL options:0 |error|:(missing value)
set attribString to current application's NSAttributedString's alloc()'s initWithRTFDFileWrapper:theWrapper documentAttributes:(missing value)
set theString to attribString's |string|() as text

Hi Stanley!

Tried your script. Does not work. I got this error:
-[NSPlaceholderString initWithRTFD:documentAttributes:]: unrecognized selector sent to instance 0x6280000141d0

Is it because I’m still on Yosemite?

No, it’s because I wrote it without testing it. Please try the amended version.

Stanley,

As always your answer is perfect.
Thanks for being here after so much other places (like XPress forum, late 90s!)

:wink:

1 Like

I hope you’re not stalking me :wink:

1 Like

It might be a good answer in your case, but for the more general problem of extracting text from an .rtfd file, it has a problem: if the .rtfd file has any attachments, they will be represented in the string by an invisible character.

So let’s say I have an .rtfd file with the words “Santa Claus” followed by an image. If I run the script above and show both Best and Source views, I get this:

The attachment character isn’t visible in the Best or Source views, although if you move the cursor through, you can tell that it is present. But if you look at the value for theString in the Variables list, you will see a dotted box containing OBJ, which represents the attachment character. I’m assuming this special glyph is only present in the system font used for tables.

Once you see the character, the solution is simple:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theURL to current application's |NSURL|'s fileURLWithPath:"/Users/shane/Desktop/Untitled.rtfd"
set theWrapper to current application's NSFileWrapper's alloc()'s initWithURL:theURL options:0 |error|:(missing value)
set attribString to current application's NSAttributedString's alloc()'s initWithRTFDFileWrapper:theWrapper documentAttributes:(missing value)
set theString to (attribString's |string|()'s stringByReplacingOccurrencesOfString:(character id 65532) withString:"") as text

And now the answer is more than perfect!
:wink: