Reading RTFD data from RTFD file with image file attachments

Hello everybody.

I am currently working on RTFD data. I have successfully created on my desktop an RTFD file with embedded image file attachments. With the following script:


set imageFiles to choose file of type "public.image" with multiple selections allowed

tell application "Finder"
	try
		make new folder at desktop with properties {name:"ImageAttachments.rtfd"}
	end try
end tell
set theRTFD to POSIX path of (alias ("" & (path to desktop folder) & "ImageAttachments.rtfd"))

tell application "TextEdit"
	activate
	tell (make new document)
		repeat with aFile in imageFiles
			make new attachment with properties {file name:aFile}
		end repeat
		save it in POSIX file theRTFD
	end tell
	close front document saving no
end tell

Now I tried to read RTFD data from it, and can’t. Following attempt returns missing value:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- needed for used rtf methods

-- classes, constants, and enums used
property NSData : a reference to current application's NSData
property NSAttributedString : a reference to current application's NSAttributedString
property NSDictionary : a reference to current application's NSDictionary
property NSString : a reference to current application's NSString
property NSRTFTextDocumentType : a reference to current application's NSRTFTextDocumentType

set posixPath to POSIX path of (choose file with prompt "Choose an RTF(d) file" of type {"rtf", "rtfd"})
-- read file as RTF data
set theData to NSData's dataWithContentsOfFile:posixPath

So, I need help to read RTFD data to some variable.

What happens if you set posixPath to the path of the .rtf file inside the .rtfd bundle? (This is a fixed path.)

Hi, Shane, thank you for your help. My goal was to look into the mystery of TextEdit. Specifically, how TextEdit glues the attachment files together. With your help, Shane, I did it.

I got RTF data by setting posixPath to file TXT.rtf inside the bundle.

Here is output I got in raw NSData form:

(NSData) {length = 128, bytes = 0x7b5c7274 66315c61 6e73695c 616e7369 … 626c3b5c 72656432 }<<truncated at 128 bytes out of 747>>

And here is output in plain text form:

{\rtf1\ansi\ansicpg1252\cocoartf2513
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{*\expandedcolortbl;;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

\f0\fs24 \cf0 {{\NeXTGraphic Hopstarter-Hard-Drive-Device-Hard-Drive-Mac.icns \width2560 \height2560 \appleattachmentpadding0 \appleembedtype0 \appleaqc
}¨}{{\NeXTGraphic iphone_hdr_NO-Berry_thumb.jpg \width2560 \height1920 \appleattachmentpadding0 \appleembedtype0 \appleaqc
}¨}{{\NeXTGraphic iphone_hdr_NO-Berry.jpg \width65280 \height48960 \appleattachmentpadding0 \appleembedtype0 \appleaqc
}¨}}

You can also read it directly using the NSFileWrapper class:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- needed for used rtf methods

-- classes, constants, and enums used
property NSFileWrapper : a reference to current application's NSFileWrapper
property NSData : a reference to current application's NSData
property NSAttributedString : a reference to current application's NSAttributedString
property NSDictionary : a reference to current application's NSDictionary
property NSString : a reference to current application's NSString
property NSRTFTextDocumentType : a reference to current application's NSRTFTextDocumentType

set theFile to (choose file with prompt "Choose an RTFD file" of type {"com.apple.rtfd"})
set theWrapper to NSFileWrapper's alloc()'s initWithURL:theFile options:0 |error|:(missing value)
set atString to NSAttributedString's alloc()'s initWithRTFDFileWrapper:theWrapper documentAttributes:(missing value)

Thanks. This example is even better.