Silicone
Silicone

Reputation: 673

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 have registered the filetype with the application and the application: openFile: method works fine if I drop a file onto the applications icon. In the apple documentation it says, that this method should also work when the user double clicks a file.

What I am seeing is, the app will activate when the file is opened by double click, but Safari will open the file instead (Trying to open a .webloc file). But the method application: openFile: does not get called :(

Here is how i set it up in xcode: file type reg

Edit:

I have set the .webloc to open as standart to my application. Also Right clicking "Open With" > "My App" does not work.

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

Upvotes: 3

Views: 2021

Answers (4)

Ian K
Ian K

Reputation: 137

Since I just went through a similar problem myself, I will add what worked for me. I found that .webloc files never call -application:openFile:, instead you must do several things to allow for your application to be the default role handler for URLs if you just want to double click on the .webloc and have your application handle it.

The chosen answer on this post describes the process that solved my problem: How do you set your Cocoa application as the default web browser?

Upvotes: 2

GoZoner
GoZoner

Reputation: 70165

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

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

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

Upvotes: -1

Caleb
Caleb

Reputation: 125007

It's possible to have several applications that all know how to open the same type of file. If you want your application to handle .webloc files, Get Info for a file of that type. Then select your application in the popup menu in the "Open with:" section of the info window. If you want to use your application for all such files, click the "Change All..." button.

Upvotes: 0

Chuck
Chuck

Reputation: 237060

Double-clicking on a file doesn't open in it every application that can handle the file type (can you imagine what would happen when you double-clicked on a JPEG?) — double-clicking uses the default app for the file type. In this case, Safari is the default reader for webloc files. You need to change that to be your application (you can do this from the Get Info window) if you want it to be the one called.

Upvotes: 1

Related Questions