Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

slooksterPSV

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
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

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
caveman_uk said:
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:
Code:
- (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

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
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

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
slooksterPSV said:
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

macrumors 68040
Original poster
Apr 17, 2004
3,544
306
Nowheresville
HiRez said:
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.