doctor_ew
doctor_ew

Reputation: 123

How to use NSWorkspace to be able to open a filetype? - Cocoa

My app is able to extract .deb files but only if you specify where with buttons in the app. But I want to be able to just click on a .deb file and have my app open up, run the actions I already have, then quit when it's done. Right now I'm using NSWorspace to tell my app it can open .deb files but I don't know how to have it run my actions from that. Is using NSWorkspace even the right thing to do? I also need to be able to get the Filepath of the file being opened in string format and I can't seem to fighure this out?

Upvotes: 0

Views: 713

Answers (2)

Living Skull
Living Skull

Reputation: 136

I did some testing and this might be of interest to others, too. Solution is lined out by a little example.

Create a new Cocoa project in Xcode, go to the xib and add a label to the window. We will use it for a proof of concept. Next, some stuff to your app delegate. Interface:

#import <Cocoa/Cocoa.h>

@interface fileClickerAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *fileName;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *fileName;

@end

The implementation is pretty straight forward:

#import "fileClickerAppDelegate.h"

@implementation fileClickerAppDelegate

@synthesize window;
@synthesize fileName;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames {

    [fileName setStringValue:[NSString stringWithFormat:@"Wow, even multiple files like %d :-)", [filenames count]]];
}

@end

There is one method to keep an eye on:

application:openFiles.

Guess what it does? It receives a list of files passed from either having clicked on an item on the desktop or in the finder, or having dragged files onto the icon. It will be passed an array with the filenames, along with the paths, UNIX style.

My tests did not require to set "Document types" in the plist of the app, or doing fancy registration stuff with the finder. Of course, this could add...

That should fix your problem and you can do whatever you want with your .deb files (I assume debian packages?).

Good luck!

Living

P.S.: Just do not forget to hook up the NSTextField to the GUI control ;-) Otherwise, it will work but nothing is displayed....

Upvotes: 1

Living Skull
Living Skull

Reputation: 136

Please supply a bit more information on what exectly are you doing and maybe why it does not work, e. g. crash dump, gdb output, etc.

It is hard to guess what might help if you don't tell us what i going on.

Upvotes: 0

Related Questions