views:

191

answers:

2

Hi,

I am working on a text editor and I want to add RTFD support. After adding the RTFD file type to my Info.plist, I got the message that "readFromFileWrapper:ofType:error: must be overridden for your application to handle file packages."

I had a look around in the documentation and found some things, but I didn't manage to do everything..Could someone be so kind and help me out please?

- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError {

    if ([typeName isEqualToString:@"Rich Text with Attachments"])  {   
        NSLog(@"Its RTFD");
        //it gets to here but I don't know how to load the rtfd data then :(
}

ofType:typeName]    return YES; }

Any help is apprechiated :)

A: 

RTFD is a particularly easy one, luckily. Check out the readRTFDFromFile: and writeRTFDToFile:atomically: methods of NSText.

From the NSText Class Reference:

readRTFDFromFile:

Attempts to read the RTFD file at path, returning YES if successful and NO if not.

- (BOOL)readRTFDFromFile:(NSString *)path

Discussion
path should be the path for an .rtf file or an .rtfd file wrapper, not for the RTF file within an .rtfd file wrapper.

If you're coming from NSURL, do something like this:

[textView readRTFDFromFile:[[self fileURL] path]];

To be safe, call [[self url] isFileURL] first.

Carl Norum
I tried your snippet and achieved the following: The NSDocument window was opened, the title was updated to the RTFD file's name but the NSTextView is still blank.This is my code: if ([typeName isEqualToString:@"Rich Text with Attachments"]) { NSString *urlString = [[self fileURL] absoluteString]; NSLog(@"%@",urlString); [textView readRTFDFromFile:urlString]; return YES; }
David Schiefer
@David, you don't want the URL string, just a path to the file. I made a quick test app, and this worked for me: `[textView readRTFDFromFile:@"/Users/carl/Desktop/file.rtfd"]`
Carl Norum
the reason i have an URL string is because my app is based on the NSDocument structure, NSDocument returns an URL as filepath only, which I then converted to a string...
David Schiefer
@David, so convert it....
Carl Norum
I have by doing this: NSString *urlString = [[self fileURL] absoluteString]; and then loading it via readRTFDFromFile:.The filepath is right on NSLog, but it still wont load :(
David Schiefer
@David, on another test, the trailing `/` is ok, just not the `file://localhost/`.
Carl Norum
@David, that's still a URL. Change it to `@"/Users/David/Documents/Coursework/Geography Coursework.rtfd"`. How are you getting the URL?
Carl Norum
still nothing...again the title of the Window updates, but the document remains blank.The URL is obtained through the [NSDocument fileURL] call which returns a NSURL.
David Schiefer
@David, it's not that hard to convert a URL to a local path: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURL/path
Carl Norum
@David, tested and works. I'll edit the answer. You should read some of the documentation, I think.
Carl Norum
i know it isn't,but i already tried that (it now returns "/ReadMe.rtfd").No avail.Same result?Title changes but NSTextView does not. I am seriously wondering why it fails :/
David Schiefer
I think my approach is wrong? because the implementation is: -(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outErrorMaybe I need to approach it differently ?
David Schiefer
Why would you override `readFromFileWrapper:` when you don't have to? I give up.
Carl Norum
The compiler tells me that readFromFileWrapper:ofType:error has to be implemented and overwritten to handle file packages.
David Schiefer
To get a path string from an `NSURL`, use `[yourURL path]` not `[yourURL absoluteString]`.
Rob Keniger
Issue solved, I barked up the wrong tree.This is the answer for NSDocument: mString = [[NSAttributedString alloc] initWithRTFDFileWrapper:fileWrapper documentAttributes:nil];
David Schiefer
A: 

I tried your snippet and achieved the following:

The NSDocument window was opened, the title was updated to the RTFD file's name but the NSTextView is still blank.

I was pretty convinced that my code would open it, since I got myself the URL from the NSDocument instance, converted it to an NSString and sent it to the textview...

if ([typeName isEqualToString:@"Rich Text with Attachments"]) {
        NSString *urlString = [[self fileURL] absoluteString];
        NSLog(@"%@",urlString);
        [textView readRTFDFromFile:urlString];

        return YES;
    }

The Log returned this:

file://localhost/Users/David/Documents/Coursework/Geography%20Coursework.rtfd/

So it should work?

David Schiefer
@David, it's a little weird to answer your own question like this; please edit your original post to add more details if you like.
Carl Norum
NSString *urlString = [[self fileURL] path];
geowar