phased_array
phased_array

Reputation: 229

How to get the file item right-clicked in Finder on OSX

I'm writing a plugin via mach_inject to add an item to Finder context menu. I have successfully add it by hooking NSMenu. But now i need to get the item that is right-clicked. Someone said we could use the following code, but it can only get the selected items instead of right-clicked item (They are different!!!! In Finder, if you select one item and right-clicked another item, the selected one won't change). Anyone knows how to get right-click item in Finder? Thanks!

SBElementArray * selection = [[finder selection] get];

NSArray * items = [selection arrayByApplyingSelector:@selector(URL)];
for (NSString * item in items) {
    NSURL * url = [NSURL URLWithString:item];
    NSLog(@"selected item url: %@", url);
}

Upvotes: 1

Views: 520

Answers (1)

6 1
6 1

Reputation: 1074

Before get the selected files, you should prepare some help code

struct TFENode {
struct OpaqueNodeRef *fNodeRef;
};

struct TFENodeVector {
    struct TFENode *_begin;
    struct TFENode *_end;
    struct TFENode *_end_cap;
};

- (NSArray *)arrayForNodeVector:(const struct TFENodeVector *)vector
{
    NSInteger capacity = vector->_end - vector->_begin;
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
    struct TFENode *node;
    for (node = vector->_begin; node < vector->_end; ++node) {
        [array addObject: [self pathForNode:node]];
    }
    return array;
}

You can get files like this

// Snow Leopard & Lion
// gNodeHelper is where you put above code
// override_handleContextMenuCommon: is your override function

+ (void)override_handleContextMenuCommon:(unsigned int)context
                                   nodes:(const struct TFENodeVector *)nodes
                                   event:(id)event
                                    view:(id)view
                        windowController:(id)windowController
                              addPlugIns:(BOOL)flag
{
    NSArray *paths = [gNodeHelper arrayForNodeVector:nodes];

    [self override_handleContextMenuCommon:context
                                     nodes:nodes
                                     event:event
                                     view:view
                         windowController:windowController
                               addPlugIns:flag];

}

Upvotes: 2

Related Questions