Justin
Justin

Reputation: 2142

How to open custom files?

I have successfully made a custom file type based on a text file. I have put an open command in my application, which works perfectly. My problem is that when I double click on one of my files, it opens my application, but it tells me that I app cannot open those files.

My code is as follows.

Files.h

@interface Files : NSObject {
    IBOutlet id name;
}

- (IBAction) open: (id) sender;

Files.m

- (IBAction) open: (id) sender; {
    NSOpenPanel *theOpenPanel = [NSOpenPanel openPanel];
    NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"hsc", @"HSC", nil];
    [theOpenPanel setAllowedFileTypes:fileTypes];
    if ([theOpenPanel runModal] == NSOKButton) {
        NSString *theFileName = [theOpenPanel filename];
        NSString *data = [NSString stringWithContentsOfFile:theFileName];
        [name setStringValue:data];

    }
}

When I tell my program to open the file, it puts all of the data in as needed. When I double click on my file, nothing happens.

Important Note: I just found out that ".hsc" is a file used in Windows 7. I am going to change my file extension after I solve this problem.

Upvotes: 0

Views: 809

Answers (1)

Stephen Poletto
Stephen Poletto

Reputation: 3655

You should define

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename

in your AppDelegate class to handle loading the file. Make sure to return YES from this method to indicate the file was successfully opened.

Upvotes: 2

Related Questions