我正在通过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);
}
答案 0 :(得分:2)
在获取所选文件之前,您应准备一些帮助代码
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];
}