Opening a file by double-clicking it in objective-c

I would like to open a file with my application when the file is double clicked. I registered filetype with the application and the method application: openFile:works fine if I delete the file on the application icon. Apple's documentation says that this method should also work when the user double-clicks the file.

What I see, the application is activated when the file is opened by double-clicking, but Safari will open the file instead (attempt to open the .webloc file). But the method is application: openFile:not called: (

This is how I installed it in xcode: file type reg

Edit:

I installed .webloc to open as standard for my application. Also the right click β€œOpen With”> β€œMy Application” does not work.

Edit 2: If I rename the file extension to .mp3 or any other, my application will open the file as needed by double-clicking it! Strange ...

+3
source share
4 answers

In Xcode, create a new project using the "Mac OS X :: Cocoa Application" template. Then do

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{ static NSString *content = @"Empty File";
   return [NSData dataWithBytes: [content UTF8String] 
                         length: 1 + strlen ([content UTF8String])]; }

You will have a working example to compare with your project.

0
source

, ( , , JPEG?) - . Safari webloc. ( Get Info), , .

+1

, , . , .webloc -application:openFile:, , URL-, .webloc .

The selected answer to this post describes the process that solved my problem: How to install the Cocoa application as the default web browser?

+1
source

It is possible to have several applications that everyone knows how to open the same file. If you want your application to process files .webloc, Get Info for a file of this type. Then select your application in the pop-up menu in the "Open With:" section of the information window. If you want to use the application for all such files, click the "Change all ..." button.

0
source

All Articles