 |
 |
Getting filename of opened document
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|