Open a file on startup of app.

Discussion in 'Mac Programming' started by slooksterPSV, Jul 22, 2006.

  1. macrumors 68030


    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?
  2. macrumors 601


    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.
  3. macrumors 68030


    But how would I go about doing that, I'm not sure how I'd use it with applicationDidFinishLoading - what command would I use?
  4. Guest


    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.
  5. macrumors 68030


    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.
  6. macrumors 68030


    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!
  7. macrumors 601


    Oh, you didn't say you wanted to open a blank document. That would have been useful information. :rolleyes:
  8. macrumors 68030


    Not a BLANK file, but an ACTUAL file =P, yeah I jimmy rigged it, is that good or bad?

Share This Page