【问题标题】:How to get the file item right-clicked in Finder on OSX如何在 OSX 上的 Finder 中右键单击文件项
【发布时间】:2011-12-22 04:50:42
【问题描述】:

我正在通过 mach_inject 编写一个插件来将一个项目添加到 Finder 上下文菜单。我已经通过挂钩 NSMenu 成功添加了它。但现在我需要获取右键单击的项目。 有人说我们可以使用下面的代码,但是它只能获取选中的项目而不是右键单击的项目(它们是不同的!!!!在Finder中,如果您选择一个项目并右键单击另一个项目,则选中的项目获胜'不改变)。任何人都知道如何在Finder中获得右键单击项目?谢谢!

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);
}

【问题讨论】:

  • +1,我也想知道这个。

标签: objective-c macos finder contextmenu


【解决方案1】:

在获取选中的文件之前,你应该准备一些帮助代码

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;
}

你可以得到这样的文件

// 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];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    相关资源
    最近更新 更多