Exporting to rtfd on Mac

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
micmac
Posts: 60
Joined: Mon May 30, 2011 9:00 am

Exporting to rtfd on Mac

Post by micmac »

Has anyone done exporting some rtf text and images to a rtfd file on Mac?

Mic
PaulDaMacMan
Posts: 687
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Exporting to rtfd on Mac

Post 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.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList
micmac
Posts: 60
Joined: Mon May 30, 2011 9:00 am

Re: Exporting to rtfd on Mac

Post by micmac »

Thank you for the reply.

I am trying to make rtfd work.

Mic
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10483
Joined: Fri Feb 19, 2010 10:17 am

Re: Exporting to rtfd on Mac

Post 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.
Klaus
Posts: 14339
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Exporting to rtfd on Mac

Post 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
micmac
Posts: 60
Joined: Mon May 30, 2011 9:00 am

Re: Exporting to rtfd on Mac

Post 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
bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4222
Joined: Sun Jan 07, 2007 9:12 pm

Re: Exporting to rtfd on Mac

Post 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
Post Reply