 |
 |
Cocoa : Get App to open one of it's files?
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status:
Offline
|
|
Made a Cocoa app...
I'm trying to get it to open one of it's own files instead of opening a default new file..
How can I do this ?
Thanks
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
I think that you would have to override the method:
- (id)initWithContentsOfFile  NSString *)fileName ofType  NSString *)docType
in your NSDocument subclass although I am not sure if that would work. My assumption is that when initializing a new file by default if passes nil to this as the filename so you could catch that and then call super's implementation with the path to your template file. That could, of course, be done by using NSBundle.
I am not completely sure of this but that seems like the logical way that it would work by default. Give it a try.
Hope that helps,
Jeff.
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status:
Offline
|
|
Make your controller object the delegate of the NSApplication object and implement the method that controls whether a new document shoudl be created on application start (I don't remember which it is, check the docs about NSApplictaion delegates). Have it return NO and in the process open an Open Panel instead.
|
|
The low-end Mac Pro is the most overpriced Mac since the IIvx
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2002
Status:
Offline
|
|
Or try it this way:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
- (BOOL)applicationShouldOpenUntitledFile  NSApplication *)sender
{
return NO;
}
</font>[/code]
Cheers
[ 04-12-2002: Message edited by: Cocoala ]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status:
Offline
|
|
Originally posted by Cocoala:
<STRONG>Or try it this way:
</STRONG>
this will just stop the app from making a new doc
what I want is to read from a file (identified by a NSString *path) created by this app
[ 04-12-2002: Message edited by: iNeusch ]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2002
Status:
Offline
|
|
Read this in another forum, might work:
"connect to the "open" menu item and send it a "performClick:" message."
I'll keep looking to find an answer, i might need it myself in the near future 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status:
Offline
|
|
Originally posted by Cocoala:
<STRONG>Read this in another forum, might work:
"connect to the "open" menu item and send it a "performClick:" message."
I'll keep looking to find an answer, i might need it myself in the near future  </STRONG>
Hum... don't know
Was wondering if I shouldn't put the content of the file in an NSData and then decode my objects from there...
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Dec 2000
Status:
Offline
|
|
You could put this in your NSDocumentController subclass if you have one:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>- (id)openUntitledDocumentOfType  NSString *)docType display  BOOL)display {
return [self openDocumentWithContentsOfFile:theFileYouWantToOpe n display:display];
}</font>[/code]
Haven't tested it, but that ought to work. If you're not using an NSDocumentController subclass, it will probably be easier to put the above code in the applicationShouldOpenUntitledFile method in the delegate, returning YES if the object returned by the method is not nil.
[edit: smilies need to be disabled when writing code! Also, my indent wasn't showing up for some reason]
[edit: more UBB silliness]
[ 04-12-2002: Message edited by: CharlesS ]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status:
Offline
|
|
Originally posted by CharlesS:
<STRONG>You could put this in your NSDocumentController subclass if you have one:
Haven't tested it, but that ought to work.
</STRONG>
Thanks, I'll try that
[edit: I love UBB  ]
[ 04-12-2002: Message edited by: iNeusch ]
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Dec 2000
Status:
Offline
|
|
You could use a delegate method to do the same thing if you don't already have an NSDocumentController subclass. The reason I thought of the subclass first is that I already use one for other reasons...
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
First, implement the applicationShouldOpenUntitledFile: delegate method to return NO, then implement the applicationDidFinishLaunching: method to open up the file you want when you launch. You can use NSDocumentController's openDocumentWithContentsOfFile:display: method to do that part (no subclass needed, just use the shared controller). Also, if you want to be able to make new files after the launch, you should probably set a flag when your app finishes launching, otherwise if you always return NO to open an untitled file, the File->New menu command won't do anything.
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Dec 2000
Status:
Offline
|
|
Originally posted by bewebste:
<STRONG>First, implement the applicationShouldOpenUntitledFile: delegate method to return NO, then implement the applicationDidFinishLaunching: method to open up the file you want when you launch. You can use NSDocumentController's openDocumentWithContentsOfFile:display: method to do that part (no subclass needed, just use the shared controller). Also, if you want to be able to make new files after the launch, you should probably set a flag when your app finishes launching, otherwise if you always return NO to open an untitled file, the File->New menu command won't do anything.</STRONG>
Or, you could simply make the applicationOpenUntitledFile: delegate method open your file and return YES, like I've already explained. If you want code, here it is. This should work:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>- (BOOL)applicationShouldOpenUntitledFile: (NSApplication *)sender
{
NSDocumentController *dc = [NSDocumentController sharedDocumentController];
return ([dc openDocumentWithContentsOfFile:someFilePath display:YES] != nil);
}</font>[/code]
[edit: UBB is weird]
[ 04-12-2002: Message edited by: CharlesS ]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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