PDA

View Full Version : What method is called when double click launch an App




petron
Oct 14, 2009, 04:17 PM
Hi,
I wonder which method is called when an user click twice on an file icon in the Finder. The application I talk about is not document based.

I have an application that I use to manipulate some data base files, I associated a file type to it. I do pick up the Doc name into the App and then manipulate it. Works fine, but I would like to double click on a file, start the App and have ready to go setup.

When clicking on that file type, the main application starts up correctly but I do get an error:

The document “create_db.dbmm” could not be opened. Create DB cannot open files in the “Create DB Doc” format.

I assume that I do lack a method that handles the notification during the application start up. Frankly I do not have an idea how to find it out.

I wondered for a while about the NSApplicationDelegate, but I am not sure how it should work.

A question arise as well, Should I rewrite the Application to be a Document based one, even if I do not really need it ?

I will appreciate some help in pushing me in the right direction

/petron



GorillaPaws
Oct 14, 2009, 04:38 PM
This document (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/OperatingSystem/OperatingSystem.html) might be a good jumping-off point. Hopefully someone else can give you a more specific answer. I'd be interested to hear what the best practices are for this type of interaction (i.e. using Apple Events vs. NSTask to interact with the OS and other applications).

kainjow
Oct 14, 2009, 06:27 PM
Are you getting that message in the Finder? If so, then somehow it hasn't been registered to open with your app yet. If your Info.plist is setup properly, then you might need to Get Info your file and change the Open With field.

petron
Oct 15, 2009, 01:14 AM
Thanks for the link GorillaPaws, I will try to look through it.

Kainjow,
The application has been started properly, and it seems like an opening of the document failured in the App, not in the finder.
Frankly, I am not very surprised since in my App Controller for this application I do not have any method that would handle such a event/callback/delegate.
But I assume that there is a mthod that I can over write to handle the event and pick up the file into the App. But I do not know it yet.

Thanks for replies.
/piotr

GorillaPaws
Oct 15, 2009, 01:25 AM
Thanks for the link GorillaPaws, I will try to look through it.

I think I misunderstood what you were trying to do. I got the impression that your app was trying to interact with data in a second app. After re-reading your post, I realized my mistake.

For certain, don't rewrite your app to be document-based if you don't need that functionality.

petron
Oct 15, 2009, 03:09 PM
A short notice...

I have verified that the Warning message is generated from my application.
In the window I do see the warning sign together with the icon of my program.

So the question is still valid, which method can I override in order to catch the opening file event from the Finder.

I hope there is an answer...

/piotr

rossipoo
Oct 16, 2009, 09:44 AM
An associated a file type seems odd for a non-document based app. Where you need to put loading code depends on what is being done in the loading code. NSApplicationDelegate has a method called applicationWillStart (you may want to double check the class documentation) which runs early in the startup process. At that point however, you can't do anything Cocoa related (creating views). For that you should probably use awakeWithNib, which runs late in the startup process, after the interface has been initialized. awakeWithNib is called for any class instance that is part of the Nib file.

NSDocument has a method called initWithURL: . The documentation says this in invoked by NSDocumentController. If your app is not document based then I don't know how this works.

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSDocument_Class/Reference/Reference.html

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Documents/Concepts/DocTypePList.html

petron
Oct 16, 2009, 12:23 PM
Thanks Rossipoo

I will take a look at the NSApplicationDelegate.

To be honest what I need when clicking on the file is not to open the file but to get its path into the application. Thanks again

/petron

kainjow
Oct 16, 2009, 03:26 PM
Have you looked at the application:openFiles: method?

petron
Oct 17, 2009, 04:16 PM
Thanks Kainjow for the idea.
No I did not look at it yet. I will try it soon. Right now I did started another cocoa project. I do write a simple log converter for amateur radio.

/petron

petron
Oct 17, 2009, 04:59 PM
Hi, Kainjow.

You are genie :D . It works like a charm now.
The trick was to add this line in init method

[NSApp setDelegate:self];

and then I added what I needed

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
[self setValue:filename forKey:@"sourceFile"];
[outField setStringValue:filename];
return YES;
}



Thanks again...I will send you soon a link to my page with the software.

/petron