Friday, October 22, 2021

#1 2021-09-30 11:51:46 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

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:

Applescript:


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:

Applescript:


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.

Last edited by KniazidisR (2021-09-30 11:54:40 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2021-09-30 06:01:07 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6708

Re: Reading RTFD data from RTFD file with image file attachments

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-09-30 10:46:15 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: Reading RTFD data from RTFD file with image file attachments

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
}¨}}

Last edited by KniazidisR (2021-09-30 10:56:45 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-09-30 11:10:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6708

Re: Reading RTFD data from RTFD file with image file attachments

You can also read it directly using the NSFileWrapper class:

Applescript:

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)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-09-30 11:45:17 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: Reading RTFD data from RTFD file with image file attachments

Thanks. This example is even better.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)