0

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

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Pripyat
  • 2,937
  • 2
  • 35
  • 69

2 Answers2

0

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
  • 219,201
  • 40
  • 422
  • 469
  • 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; } – Pripyat Feb 16 '10 at 12:32
  • @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 Feb 16 '10 at 18:15
  • 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... – Pripyat Feb 16 '10 at 18:48
  • 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 :( – Pripyat Feb 16 '10 at 19:01
  • @David, on another test, the trailing `/` is ok, just not the `file://localhost/`. – Carl Norum Feb 16 '10 at 19:03
  • @David, that's still a URL. Change it to `@"/Users/David/Documents/Coursework/Geography Coursework.rtfd"`. How are you getting the URL? – Carl Norum Feb 16 '10 at 19:04
  • 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. – Pripyat Feb 16 '10 at 19:14
  • @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 Feb 16 '10 at 19:26
  • @David, tested and works. I'll edit the answer. You should read some of the documentation, I think. – Carl Norum Feb 16 '10 at 19:38
  • 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 :/ – Pripyat Feb 16 '10 at 19:38
  • I think my approach is wrong? because the implementation is: -(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError **)outError Maybe I need to approach it differently ? – Pripyat Feb 16 '10 at 19:40
  • Why would you override `readFromFileWrapper:` when you don't have to? I give up. – Carl Norum Feb 16 '10 at 19:42
  • The compiler tells me that readFromFileWrapper:ofType:error has to be implemented and overwritten to handle file packages. – Pripyat Feb 16 '10 at 19:44
  • 2
    To get a path string from an `NSURL`, use `[yourURL path]` not `[yourURL absoluteString]`. – Rob Keniger Feb 16 '10 at 23:57
  • Issue solved, I barked up the wrong tree.This is the answer for NSDocument: mString = [[NSAttributedString alloc] initWithRTFDFileWrapper:fileWrapper documentAttributes:nil]; – Pripyat Feb 17 '10 at 11:58
-1

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?

Pripyat
  • 2,937
  • 2
  • 35
  • 69