Page 1 of 1

Exporting to rtfd on Mac

Posted: Mon Oct 05, 2026 12:19 pm
by micmac
Has anyone done exporting some rtf text and images to a rtfd file on Mac?

Mic

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 1:23 am
by PaulDaMacMan
micmac wrote: Mon Oct 05, 2026 12:19 pm Has anyone done exporting some rtf text and images to a rtfd file on Mac?

Mic
I've exported styled text, Imported and exported .RTF and images separately, but not in.rtfd bundle format, which is a format unique to Apple I believe.

RTFD being one of Apple's / NeXTstep bundle folder-format, means that you can right-click it and select "show contents" in the finder, and it will open as if it were a folder (which it is) and inside there you will find a normal .rtf file along with any images or any other content that's been used/embedded in the formatted document. Of course it's Apple's proprietary interpretation of the RTF spec, so you might want to read any docs they may provide on their dev site about the specifics of RTFD (the 'D' is for Directory?), you may have to dig far back in the archives because I think .rtfd format goes way back to NeXT inc. just as "Text Edit.app" does.

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 8:35 am
by micmac
Thank you for the reply.

I am trying to make rtfd work.

Mic

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 10:05 am
by richmond62
From a purely mechanical point of view you probably need to:

1. Access the rtf file inside the rftd 'folder'.

2. Access any images.

3. Work out how the 'picture hooks' are arranged to get the images in the desired positons within the text.

I wonder how xTalk can 'see' inside .docx and .rtfd containers.

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 10:32 am
by Klaus
richmond62 wrote: Thu Oct 08, 2026 10:05 am From a purely mechanical point of view you probably need to:
1. Access the rtf file inside the rftd 'folder'.
2. Access any images.
3. Work out how the 'picture hooks' are arranged to get the images in the desired positons within the text.
I wonder how xTalk can 'see' inside .docx and .rtfd containers.
Since this is just a folder in disguise, like any macOS APP, you can just do this in LC, see screenshot.
So you can easily access the content of that folder.
rtfD_test.png

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 10:49 am
by micmac
Here is what Claude suggested. It is beyond me.

I provided an image name from the stack as suggested in the script.

It did not work but it didn't make any error either. No result in message box.


on mouseUp
answer folder "Save the RTFD in:"
if it is empty then exit mouseUp

put "text" into tBlocks[1]["type"]
put "Hello from LiveCode!" & return & "Here is a picture:" & return into tBlocks[1]["data"]
put "image" into tBlocks[2]["type"]
put "/Users/me/Pictures/photo.png" into tBlocks[2]["data"] -- or an image object name
put "text" into tBlocks[3]["type"]
put return & "Thanks for reading." into tBlocks[3]["data"]

put exportRTFD(it & "/MyDocument", tBlocks) into tResult
if tResult is empty then
answer "Done."
else
answer tResult
end if
end mouseUp





function exportRTFD pFolderPath, pBlocks
local tRTF, tSrc, tType, tData, tSize, tName, tCount, tW, tH, i

if char -5 to -1 of pFolderPath is not ".rtfd" then put ".rtfd" after pFolderPath
if there is not a folder pFolderPath then
create folder pFolderPath
if there is not a folder pFolderPath then
return "Error: could not create" && pFolderPath
end if
end if

put "{\rtf1\ansi\ansicpg1252\cocoartf2639" & return into tRTF
put "\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}" & return after tRTF
put "{\colortbl;\red255\green255\blue255;}" & return after tRTF
put "\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0" & return after tRTF
put "\pard\pardirnatural\partightenfactor0" & return & return after tRTF
put "\f0\fs24 \cf0 " after tRTF

repeat with i = 1 to the number of elements of pBlocks
put pBlocks["type"] into tType
put pBlocks["data"] into tSrc
switch tType
case "text"
put rtfEscape(tSrc) after tRTF
break
case "image"
if there is a file tSrc then
put url ("binfile:" & tSrc) into tData
put imageSizeOfFile(tSrc) into tSize
put item 1 of tSize into tW
put item 2 of tSize into tH
else if there is an image tSrc then
put the text of image tSrc into tData
put the formattedWidth of image tSrc into tW
put the formattedHeight of image tSrc into tH
else
return "Error: image not found:" && tSrc
end if

-- scale down to max 450 points wide, keep aspect ratio
if tW > 450 then
put round(tH * 450 / tW) into tH
put 450 into tW
end if

add 1 to tCount
put "image" & tCount & "." & imageExt(tData) into tName
put tData into url ("binfile:" & pFolderPath & "/" & tName)

-- sizes in RTFD are in twips (1 point = 20 twips)
put "{{\NeXTGraphic " & tName & " \width" & (tW * 20) & " \height" & (tH * 20) \
& " \appleattachmentpadding0 \appleembedtype0 \appleaqc" & return \
& "}\'ac}" after tRTF
break
end switch
end repeat

put "}" after tRTF
put tRTF into url ("binfile:" & pFolderPath & "/TXT.rtf")
return empty
end exportRTFD

Re: Exporting to rtfd on Mac

Posted: Thu Oct 08, 2026 9:56 pm
by bn
Hi micmac,

No wonder it does not work. There are a couple of function calls to functions that Claude did not provide. Ask him to provide those functions.

There is an erraneous repeat loop:

Code: Select all

repeat with i = 1 to the number of elements of pBlocks
   put pBlocks["type"] into tType
   put pBlocks["data"] into tSrc
that does not work because the array pBlocks has the structure
pBlocks[aKey]["type"]

This would work

Code: Select all

repeat for each key aKey in pBlocks
      put pBlocks[aKey]["type"] into tType
      put pBlocks[aKey]["data"] into tSrc
And so on.

I would pester Claude with the errors that you get when debugging / stepping through to code.

Unless of course it was Claude Van Damme

Image

Re: Exporting to rtfd on Mac

Posted: Fri Oct 09, 2026 9:50 am
by micmac
Thank you Bernd, always helpful!

You put it on the right track.

Still, I had to go through many iterations with Claude. 20 or so.

Now there is succes in exporting to rtfd.

Later today I will upload a stack with the code.



I could never have done it myself.

Mic

Re: Exporting to rtfd on Mac

Posted: Fri Oct 09, 2026 1:26 pm
by micmac
So with the kind help of Bernd Niggemann and Claude AI this is a first version of "Save to RTFD".
It's kind of proof of concept.

Just click the button to see it Work.
Save to RTFD v 0.1.livecode.zip
(8.85 KiB) Downloaded 53 times
Mic

Re: Exporting to rtfd on Mac

Posted: Fri Oct 09, 2026 2:52 pm
by bn
Michael,

That is fantastic. Very instructive code. RTF is a paine to handle.
I should start to use Claude also. I downloaded Claude but just tried a few very short things. Nothing compared to what you accomplished.

Kind regards
Bernd

Re: Exporting to rtfd on Mac

Posted: Fri Oct 09, 2026 3:02 pm
by micmac
Thank you Bernd

Next I will aim at styled text.

Mic

Re: Exporting to rtfd on Mac

Posted: Sat Oct 10, 2026 3:23 am
by PaulDaMacMan
micmac wrote: Fri Oct 09, 2026 3:02 pm Thank you Bernd

Next I will aim at styled text.

Mic
*Tip*
'the styleText' property returns an array, so if you:
put the styledText of fld tWhateverFld into tDataArray

you can then use the Tree Widget to visually examine that array in a hierarchical format with:
set the arrayData of widget "Tree View" to tDataArray

Incredibly underrated and useful widget that "Tree View" is!