What am I doing wrong with this readRTFDFromFile?

Here’s what I tried:

set contents of text view "textView" of scroll view "scrollView" of window "main" to (call method "readRTFDFromFile:" of file theFilePath)

No-go, Flight.

What am I missing? theFilePath is set up to (path to desktop folder as string) & “FOO.rtfd”.

Offhand, I would try something like this:

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

See also: readRTFDFromFile:

Thanks, Bruce.

These did not work. The error ststes that the variable “result” is not defined.

I read the reference you posted before and after posting… I just can’t seem to get this to work…

Edit: Check out Dominik’s reply below.

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.

Back to square 0… Thanks for your help, though.

Cheers,

Doug

Hi Doug,

this one should work:


	tell text view "textView" of scroll view "scrollView" of window "main"
		call method "readRTFDFromFile:" of it with parameter (POSIX path of theFilePath)
	end tell

D. :slight_smile:

Thanks Dominik. :cool:

I tried that before and it didn’t work! It turns out I lost a little information in a crash earlier.

:smiley:

It works! Thanks a lot!

I just realized it would be helpful to call just the first paragraph of the RTF. I tried:

tell textBox -- my vriable for the text view
			call method "readRTFDFromFile:" of it with parameter paragraph 1 of (POSIX path of theFilePath)
		end tell

As I’m sure you can tell by looking at it, that didn’t work…

Any ideas? (Then I swear I’ll leave you alone :slight_smile: )

Thanks again!

Hi Doug,

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 :wink:

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:

//  myNSTextAddition.h

#import <Cocoa/Cocoa.h>

@interface NSText (myNSTextAddition) 
-(void)readRangeOfRTFDfile:(NSString *)thePath range:(NSRange)theRange;
-(void)readParagraphsOfRTFDfile:(NSString *)thePath numberOfParagraphs:(int)paragraphs;
@end

//  myNSTextAddition.m

#import "myNSTextAddition.h"

@implementation NSText (myNSTextAddition)

-(void)readRangeOfRTFDfile:(NSString *)thePath range:(NSRange)theRange{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSText *importedFile = [[NSText alloc] init];
	[importedFile readRTFDFromFile:thePath];
	NSData *rangeFromFile = [importedFile RTFDFromRange];
	[importedFile release];
	NSRange replacementRange = [self selectedRange];
	[self replaceCharactersInRange:replacementRange withRTFD:rangeFromFile];	
	[pool release];
}

-(void)readParagraphsOfRTFDfile:(NSString *)thePath numberOfParagraphs:(int)paragraphs{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSText *importedFile = [[NSText alloc] init];
	[importedFile readRTFDFromFile:thePath];

	NSArray *stringParagraphs = [[importedFile string] componentsSeparatedByString:@"\n"];
	int paraCount = [stringParagraphs count];
	if (paragraphs > paraCount) { // edited here! sry - this line was missing after copying the code ...
		paragraphs = paraCount;
	}
	int rangeLength = 0;
	int n;
	for (n=0; n<paragraphs; n++) {
		rangeLength += [[stringParagraphs objectAtIndex:n] length] + 1;
	}

	NSRange importRange = NSMakeRange(0,rangeLength);
	
	NSData *rangeFromFile = [importedFile RTFDFromRange:importRange];
	[importedFile release];
	NSRange replacementRange = [self selectedRange];
	[self replaceCharactersInRange:replacementRange withRTFD:rangeFromFile];	
	[pool release];
}

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 …

D.

Would RTFDFromRange: or RTFFromRange: be useful for this?

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. :slight_smile:

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.

Thanks to both of you.

Doug