tell text view "textView" of scroll view "scrollView" of window "main"
call method "readRTFDFromFile:" with parameters file theFilePath
set contents to result
end tell
You may also need to use a POSIX path.
tell text view "textView" of scroll view "scrollView" of window "main"
call method "readRTFDFromFile:" with parameters (POSIX path of theFilePath)
set contents to result
end tell
OK. I’ll try finding another way to read the content of an RTF file into a text view while preserving its font info…
I’ve been digging for days on this. A search for RTF yields 0 hits on this board, and I can’t find any info on reading them (plenty on writing if I search for rich text). I was only using this method because in the one post I could find dealing with reading RTFs the poster mentioned he used it and it worked (he didn’t post the code, though). I just can’t seem to find the technique for parsing these files.
tell text view "textView" of scroll view "scrollView" of window "main"
call method "readRTFDFromFile:" of it with parameter (POSIX path of theFilePath)
end tell
unfortunately there is no readymade cocoa method (none that I knew of) to load only a part of an RTFD file. So the only thing you can do is writing it
Here two method additions for NSText for:
loading a range of a RTFD file
loading the n first paragraphs of a RTFD file:
Just add a new Objective-C class file to your project and name it “myNSTextAddition” (for example). Then replace the content of the two files myNSTextAddition.h and myNSTextAddition.m with this code:
Now you can use the two methods from your AppleScript using ‘call method’ - like any other NSText method:
set importRange to {4,15}
tell text view "textView" of scroll view "scrollView" of window "main"
call method "readRangeOfRTFDfile:range:" of it with parameters {(POSIX path of theFilePath), importRange}
-- loads characters 4 thru 15 of the RTFD file to your text view
end tell
set numberOfParagraphs to 5
tell text view "textView" of scroll view "scrollView" of window "main"
call method "readParagraphsOfRTFDfile:numberOfParagraphs:" of it with parameters {(POSIX path of theFilePath), numberOfParagraphs}
-- loads the first 5 paragraphs of the RTFD file to your text view
end tell
I haven’t had time to thorroughly test this code … i hope it’ll works as expected …
That’s exactly what I used in the methods above. What I made was nothing else than creating a temporary NSText to load the whole file, ‘RTFDFromRange:’ to cut the portion of RTFD data I need and ‘replaceCharactersInRange:withRTFD:’ to replace the TNSText’s contents with the result.
Thanks again, Dominik. I can’t test this right now, but I didn’t want your help to go unnoticed.
I’m trying so hard to learn this stuff quickly… I’m trying to learn C to give me a base for C++ then Objective-C. It’s tough, but with help like this it’s MUCH better.