Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Getting filename of opened document

Getting filename of opened document
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 29, 2004, 03:08 PM
 
Hi guys,

I'm trying to do something special depending on the format of the file a user opens. So in openDocument: I try this:

Code:
- (void)openDocument:(id)sender { [[NSDocumentController sharedDocumentController] openDocument:self]; NSLog(@"%@", [self fileName]); if ([[[self fileName] pathExtension] isEqualToString:@"extension"]) { // Special stuff goes here } }
Now the code builds fine, but when I open a document, the NSLog shows that fileName is (null). Now, I realize that it may be null because it doesn't know which fileName you are going to choose. But, fileName does not appear in the run log until after I've chosen the document. Any ideas?

Thanks,
Oliver
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 29, 2004, 09:58 PM
 
I don't really understand what you're doing here. I'd assume this is in an NSDocumentController subclass (since that's the only class I know with an openDocument: method), but then you're calling NSDocumentController and NSDocumentController does not have a fileName instance method.

You're going to have to provide more details as to exactly what we're looking at and what it should be doing.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 30, 2004, 03:14 AM
 
This is in my NSDocument subclass, so I assume [self fileName] should work.

I also tried it in my windowControllerDidLoadNib and I got null there aswell.

Thanks,
Oliver
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 30, 2004, 04:23 AM
 
I'm afraid I don't understand. NSDocument does not declare an openDocument: method, so I don't know what this method is supposed to do, when it's supposed to be called, how the document the message is being sent to was created — anything like that.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 30, 2004, 05:30 AM
 
Originally posted by Chuckit:
I'm afraid I don't understand. NSDocument does not declare an openDocument: method, so I don't know what this method is supposed to do, when it's supposed to be called, how the document the message is being sent to was created — anything like that.
Ok, I just realized what you were saying, and came up with this code:

Code:
- (id)initWithContentsOfFile:(NSString *)fileName ofType:(NSString *)docTypes { self = [super initWithContentsOfFile:fileName ofType:docTypes]; [self setSyntaxColoringFromFileName:fileName]; return self; } - (void)setSyntaxColoringFromFileName:(NSString *)fileName { if ([[fileName pathExtension] isEqualToString:@"css"]) { NSLog(@"CSS"); } if ([[fileName pathExtension] isEqualToString:@"html"]) { NSLog(@"HTML"); } }
But the problem with this is, the file contents are not loaded into the document, its just a blank document. I thought the super call should handle that?

Thanks,
Oliver
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 30, 2004, 03:17 PM
 
Originally posted by iOliverC:
But the problem with this is, the file contents are not loaded into the document, its just a blank document. I thought the super call should handle that?
I assume you've implemented some method that reads in the file (e.g. loadDataRepresentation:ofType:), right?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 30, 2004, 07:49 PM
 
When you created the project, did you use the "Document Based Cocoa Application" template (or whatever it's called). If you did, then it would have automatically created an NSDocument subclass for you with some very helpful comments and stub methods explaining what you need to implement. If you did not use this template, and are unsure what you're doing, I'd strongly recommend starting from scratch using the template.

Also, read the Apple documentation on Document based applications... it's very good!
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 31, 2004, 04:06 AM
 
Originally posted by Brass:
When you created the project, did you use the "Document Based Cocoa Application" template (or whatever it's called). If you did, then it would have automatically created an NSDocument subclass for you with some very helpful comments and stub methods explaining what you need to implement. If you did not use this template, and are unsure what you're doing, I'd strongly recommend starting from scratch using the template.

Also, read the Apple documentation on Document based applications... it's very good!
I did yes, so basically I just have to call loadDataRep on the initWithFile method?

Thanks,
Oliver
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 31, 2004, 05:20 PM
 
Originally posted by iOliverC:
I did yes, so basically I just have to call loadDataRep on the initWithFile method?

Thanks,
Oliver
Again, I STRONGLY recommend you read the documentation on this. You don't actually have to call loadDataRep... directly yourself, it will be called when needed by another NSDocument method (can't recall exactly which one).

The document-based system is not difficult to understand, but without reading the documentation, or going through some tutorial, etc, it would be difficult to grasp.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 31, 2004, 05:24 PM
 
The documentation you need to read is:

Document-Based Applications

and

NSDocument
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:33 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2