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 > Cocoa : Get App to open one of it's files?

Cocoa : Get App to open one of it's files?
Thread Tools
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status: Offline
Reply With Quote
Apr 11, 2002, 04:10 AM
 
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
Reply With Quote
Apr 12, 2002, 01:45 AM
 
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.
Spectral Class
"Shedding Light on Innovation"
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Apr 12, 2002, 07:19 AM
 
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
Reply With Quote
Apr 12, 2002, 09:45 AM
 
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 ]
     
iNeusch  (op)
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status: Offline
Reply With Quote
Apr 12, 2002, 09:48 AM
 
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
Reply With Quote
Apr 12, 2002, 09:58 AM
 
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
     
iNeusch  (op)
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status: Offline
Reply With Quote
Apr 12, 2002, 10:10 AM
 
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
Reply With Quote
Apr 12, 2002, 11:04 AM
 
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 ]

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
iNeusch  (op)
Mac Elite
Join Date: Dec 2001
Location: Paris, France
Status: Offline
Reply With Quote
Apr 12, 2002, 11:08 AM
 
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
Reply With Quote
Apr 12, 2002, 11:15 AM
 
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...

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Apr 12, 2002, 12:39 PM
 
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-&gt;New menu command won't do anything.
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Apr 12, 2002, 05:43 PM
 
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-&gt;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 ]

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
   
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 09:43 AM.
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