FinderSync扩展 - 永远不会调用requestBadgeIdentifierForURL

hma*_*ass 4 xcode objective-c osx-yosemite findersync osx-extensions

我已经测试了Xcode中提供的模板来制作FinderSync扩展.一切都很好,除了两件事:

a)当监视文件夹时,系统永远不会调用requestBadgeIdentifierForURL方法,因此不会设置标记.这里出了什么问题?我是正确的假设当我在Finder中移动或滚动受监视的文件夹时应该调用此方法?顺便说一句,在此上下文中正确调用beginObservingDirectoryAtURL和endObservingDirectoryAtURL方法.

#import "FinderSync.h"

@interface FinderSync ()

@property NSURL *myFolderURL;

@end

@implementation FinderSync

- (instancetype)init {
    self = [super init];

    NSLog(@"%s launched from %@ ; compiled at %s", __PRETTY_FUNCTION__, [[NSBundle mainBundle] bundlePath], __TIME__);

    // Set up the directory we are syncing.
    self.myFolderURL = [NSURL fileURLWithPath:@"/Users/hmaass/Downloads"];
    [FIFinderSyncController defaultController].directoryURLs = [NSSet setWithObject:self.myFolderURL];

    // Set up images for our badge identifiers. For demonstration purposes, this uses off-the-shelf images.
    [[FIFinderSyncController defaultController] setBadgeImage:[NSImage imageNamed: NSImageNameColorPanel] label:@"Status One" forBadgeIdentifier:@"One"];
    [[FIFinderSyncController defaultController] setBadgeImage:[NSImage imageNamed: NSImageNameCaution] label:@"Status Two" forBadgeIdentifier:@"Two"];

    return self;
}

#pragma mark - Primary Finder Sync protocol methods

- (void)beginObservingDirectoryAtURL:(NSURL *)url {
    // The user is now seeing the container's contents.
    // If they see it in more than one view at a time, we're only told once.
    NSLog(@"beginObservingDirectoryAtURL:%@", url.filePathURL);
}


- (void)endObservingDirectoryAtURL:(NSURL *)url {
    // The user is no longer seeing the container's contents.
    NSLog(@"endObservingDirectoryAtURL:%@", url.filePathURL);
}

- (void)requestBadgeIdentifierForURL:(NSURL *)url {
    NSLog(@"requestBadgeIdentifierForURL:%@", url.filePathURL);

    // For demonstration purposes, this picks one of our two badges, or no badge at all, based on the filename.
    NSInteger whichBadge = [url.filePathURL hash] % 3;
    NSString* badgeIdentifier = @[@"", @"One", @"Two"][whichBadge];
    [[FIFinderSyncController defaultController] setBadgeIdentifier:badgeIdentifier forURL:url];
}

#pragma mark - Menu and toolbar item support

- (NSString *)toolbarItemName {
    return @"testfifi";
}

- (NSString *)toolbarItemToolTip {
    return @"testfifi: Click the toolbar item for a menu.";
}

- (NSImage *)toolbarItemImage {
    return [NSImage imageNamed:NSImageNameCaution];
}

- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu {
    // Produce a menu for the extension.
    NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
    [menu addItemWithTitle:@"Example Menu Item" action:@selector(sampleAction:) keyEquivalent:@""];

    return menu;
}

- (IBAction)sampleAction:(id)sender {
    NSURL* target = [[FIFinderSyncController defaultController] targetedURL];
    NSArray* items = [[FIFinderSyncController defaultController] selectedItemURLs];

    NSLog(@"sampleAction: menu item: %@, target = %@, items = ", [sender title], [target filePathURL]);
    [items enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"    %@", [obj filePathURL]);
    }];
}

@end
Run Code Online (Sandbox Code Playgroud)

b)运行上面的模板时,我在Xcode的日志控制台中收到以下消息:

2015-08-25 15:33:00.300 testfifi [855:8134]无法连接(colorGridView)插座(NSApplication)到(NSColorPickerGridView):缺少setter或实例变量2015-08-25 15:33:00.300 testfifi [855 :8134]无法连接(查看)插座(NSApplication)到(NSColorPickerGridView):缺少setter或实例变量2015-08-25 15:33:00.321 testfifi [855:8134] - 从/ Users /启动[FinderSync init] hmaass/Library/Developer/Xcode/DerivedData/testtest-egudnxkifjxirpbrjkohnatmjuro/Build/Products/Debug/testtest.app/Contents/PlugIns/testfifi.appex; 编译于20:38:18

有人可以帮我摆脱这个消息吗?

谢谢!

dej*_*now 7

我已经对您的问题发表了评论,但我认为我应该发布一个更完整的答案.

听起来你正在遇到的问题是另一个Finder Sync扩展程序"贪婪地"观察所有文件夹,很可能是Dropbox Finder集成.尝试禁用所有其他Finder Sync扩展(在"系统偏好设置" - >"扩展程序" - >"查找程序​​"下)并重新运行测试.

如果这样可以解决问题,则问题是Dropbox(或其他应用程序)已经为您尝试监视的文件夹调用了beginObservingDirectoryAtURL.不幸的是,Apple的API缺乏,因为当存在冲突的扩展时,谁没有智能逻辑来监视文件夹.目前,无论哪个Finder Sync扩展程序首先启动都将"赢".

Dropbox贪婪地监视用户主目录下的所有文件夹.我写信给Apple和Dropbox来解决这个问题,但没有听到任何回应.目前,我实现的(丑陋)解决方法是关闭已知的"贪婪"扩展,启动我自己的扩展,然后重新启动贪婪的扩展.