appchemist
appchemist

Reputation: 375

How to check file info, which is double-clicked in finder, in preferred application? This is for os x

I want to implement some application, which is called as preferred app when users double click file of some type (ex. ".test") in finder.

To implement this, I have a question.

I want to know the file infos in the preferred application which is implemented by me.

To test this, I make an application.

I only implement a main function which has to print "argc" and "argv".

I register this app as preferred app for ".test" file extension (ex. something.test).

When I double-click "something.test" file in finder, it executed my app and my app printed that argc is "2" and argv are "/Users/appchemist/myApp.app/Contents/MacOS/myApp" and "-psn_0_225954"

So, I have no idea.

Upvotes: 0

Views: 112

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90671

An app usually does not receive the file to open as an argument to its main() function.

When you open a file, your app is launched if it's not already running.

Whether it was or wasn't already running, it is then sent an "open documents" ('odoc') Apple Event. The standard handlers for this Apple Event within Cocoa will call your application delegate's -application:openFiles: or -application:openFile: method, depending on which it implements.

The reason that Apple Events are used rather than arguments to main() is precisely because apps need to be able to open documents even when they are already running. Arguments to main() are only useful at launch, which is not good enough, so they aren't particularly useful at all for this purpose.

See Cocoa Scripting Guide: How Cocoa Applications Handle Apple Events – Apple Events Sent by the Mac OS for more details.

Upvotes: 2

Related Questions