OS X Finder 同步扩展

OS X Finder Sync Extension

我无法创建简单的 Finder 同步扩展。

我创建了一个新的 OS X 项目并添加了 Finder 同步扩展目标,并且我 运行 将扩展附加到 finder。代码似乎是 运行 正在调用初始化方法和工具栏项方法,但 finder 中没有显示任何内容。

终端在运行时显示这个

2015-04-20 12:45:52.700 pcssyncextension[3196:62451] Failed to connect (colorGridView) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable 2015-04-20 12:45:52.701 pcssyncextension[3196:62451] Failed to connect (view) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable 2015-04-20 12:45:58.887 pcssyncextension[3196:62451] -[FinderSync init] launched from /Users/user/Library/Developer/Xcode/DerivedData/findersynctest-dkyjmfmqzedkquhbhqxejzlzzukn/Build/Products/Debug/findersynctest.app/Contents/PlugIns/pcssyncextension.appex ; compiled at 12:36:01

除了创建一个空项目并添加 Finder Sync Extension 之外,我还需要做些什么来让它工作吗?

我找到了一些对我有帮助的东西。默认情况下,工具栏项目不会添加到取景器 window,除非用户将其拖入。我无法找到一种方法以编程方式将项目添加到取景器 window 工具栏。

将项目添加到查找器侧边栏

// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);

// Check Items
if (favoriteItems)
{
    // Get CFURL for Application
    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];

    // Add Item
    LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, url, NULL, NULL);

    // Release
    if (item)
        CFRelease(item);
}

// Release
if (favoriteItems != NULL)
    CFRelease(favoriteItems);

从边栏中删除项目的代码

// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);

// Check Items
if (favoriteItems)
{
    // Get Login Items
    CFArrayRef favoriteItemsArray = LSSharedFileListCopySnapshot(favoriteItems, NULL);

    // Loop Through Items
    for (id item in (__bridge NSArray *)favoriteItemsArray)
    {
        // Get Item Ref
        LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;

        // Get Item URL
        CFURLRef itemURL = LSSharedFileListItemCopyResolvedURL(itemRef, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, NULL);
        if (itemURL != NULL)
        {
            // If Item Matches Remove It
            if ([[(__bridge NSURL *)itemURL path] hasPrefix:path])
                LSSharedFileListItemRemove(favoriteItems, itemRef);

            // Release
            if (itemURL != NULL)
                CFRelease(itemURL);
        }
    }

    // Release
    if (favoriteItemsArray != NULL)
        CFRelease(favoriteItemsArray);
}

// Release
if (favoriteItems != NULL)
    CFRelease(favoriteItems);

在 Finder 中重新加载目录

// Reload Finder (change the word directory to file if updating file)
NSAppleScript * update = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"tell application \"Finder\" to update POSIX directory \"%@\"",path]];
[update executeAndReturnError:nil];

启用扩展的代码(包 ID)

system("pluginkit -e use -i com.mycompany.finderExt")

禁用扩展的代码(包 ID)

system("pluginkit -e ignore -i com.mycompany.finderExt")