PDA

View Full Version : Open a file on startup of app.




slooksterPSV
Jul 23, 2006, 12:12 AM
How do you open a file on startup of an application by reading the file from the preferences?

So i read the preferences file and it gets the file path. I need to open the file directly on startup, how do I do this?



HiRez
Jul 23, 2006, 12:45 AM
I would look at putting that code into the applicationDidFinishLaunching: method, called when this notification is sent by NSApplication to the app delegate. This notification is sent when...well..your app is finished launching. There are several minor variations on this having to do with the timing of what gets called first but it sounds like this would suffice in your case. See the documentaion for NSApplication for more details. You could also perhaps put it into the +initialize class method of your application delegate, but I'd recommend the notification instead.

slooksterPSV
Jul 23, 2006, 06:43 AM
But how would I go about doing that, I'm not sure how I'd use it with applicationDidFinishLoading - what command would I use?

caveman_uk
Jul 23, 2006, 08:57 AM
I'm assuming you're asking how you'd read the file not how to do it when the apps launched?

If so, it depends on what sort of file it is. Otherwise you make the controller object the app delegate (with [NSApp setDelegate:self] or whatever) and the applicationDidFinishLoading method gets called automatically.

slooksterPSV
Jul 23, 2006, 02:33 PM
I'm assuming you're asking how you'd read the file not how to do it when the apps launched?

If so, it depends on what sort of file it is. Otherwise you make the controller object the app delegate (with [NSApp setDelegate:self] or whatever) and the applicationDidFinishLoading method gets called automatically.
The file is a file I created using NSArchiver its an MDM - MoneyData Manager file. Would I just open the file and populate the array on startup? Or... do you want me to post some code or... see I'm not sure how to do this.

Edit: or do I need to override the openDocument method or create my own open method or what

EDIT2: LET's try this
My document is a Document-based Objective-C Cocoa app. In other words, I can have multiple documents open at the same time.

Now I have class called Transactions which contains information about transactions, in MyDocument.h I define an array for those transactions to store multiple transactions, also in MyDocument there are these two items:

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
[transactionController commitEditing];
return [NSKeyedArchiver archivedDataWithRootObject:transactions];
}

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
// Insert code here to read your document from the given data. You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
NSLog(@"About to read data of type %@", aType);
NSMutableArray *newArray;
newArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

if(newArray == nil) {
return NO;
} else {
[self setTransactions:newArray];
return YES;
}
}


Now I have an AppController (AppController.h & .m) so I can open my PreferenceController's (PreferenceController.h & .m) Window (which is a panel), the user can select a file to automatically open when the application starts. I want to be able to open this file, the window to show the file's content (e.g. NSData) and yeah, that's it.

slooksterPSV
Jul 23, 2006, 03:59 PM
Oh heck, I just implemented it myself, I did it in the init section of the MyDocument.m file and the AppController.m in the applicationShouldOpenUntitledFile, I got it YAY!

HiRez
Jul 24, 2006, 04:56 PM
Oh heck, I just implemented it myself, I did it in the init section of the MyDocument.m file and the AppController.m in the applicationShouldOpenUntitledFile, I got it YAY!Oh, you didn't say you wanted to open a blank document. That would have been useful information. :rolleyes:

slooksterPSV
Jul 24, 2006, 08:12 PM
Oh, you didn't say you wanted to open a blank document. That would have been useful information. :rolleyes:
Not a BLANK file, but an ACTUAL file =P, yeah I jimmy rigged it, is that good or bad?