OS X Finder同步扩展

3

我无法创建一个简单的Finder Sync Extension。

我已经创建了一个新的OS X项目,并添加了Finder Sync Extension目标,然后我运行了附加到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之外,我是否还需要做其他事情来让它工作?


1
这似乎是Finder的间歇性错误 - 使用“强制退出”对话框重新启动Finder将允许扩展运行。 - Anya Shenanigans
1个回答

9

我找到了一些有用的东西。默认情况下,除非用户拖动它,否则工具栏项目不会添加到finder窗口中。我无法找到一种以编程方式将该项目添加到finder窗口工具栏的方法。

将项目添加到finder侧边栏

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

启用扩展(bundle ID)的代码

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

禁用扩展程序的代码(bundle ID)

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

嗨...我正在尝试使用简单的Finder同步来添加工具栏按钮。第一次附加成功了。但是当我停止并重新运行时,它显示"等待附加"。你有任何想法为什么会发生这种情况吗? - Seema Kadavan
这种情况发生时,请确保你已启用了该扩展(见上面的示例),并确保你已在系统偏好设置中启用它。此外,如果你打开Finder并转到“查看”->“自定义工具栏”,确保已添加它,否则它可能不会添加到你的窗口中。有一种程序化的方法可以做到这一点,但我没有必要这样做,所以我不确定是什么。 - Douglas Cobb
如果我从头开始创建一个用于Finder同步扩展的Xcode项目,并从调试器中运行它,那么它可以正常工作。我能够在“系统偏好设置->扩展”中切换它。但是当我停止它并尝试再次运行时,它会显示“等待附加”。我注意到,在这种情况下,扩展条目仍然存在于“系统偏好设置->扩展”中,尽管它没有被选中。有没有办法将其从扩展中删除?我尝试从com.apple.finder.plist中删除它。但是在重新启动后,它会再次填充,不知道是从哪里填充的。 - Seema Kadavan
2
使用 killall Finder 命令重启 Finder 并重新加载扩展。 - Nickolay Olshevsky
嗨,我是mac开发的新手,从苹果指南中得知如果我添加图标,将文件夹添加到Finder侧边栏后文件夹图标会改变,但它并没有改变。请问你能告诉我如何做吗? - souvickcse
@DouglasCobb 你好,我们是否需要添加代码来启用和禁用扩展?苹果文档中没有提到这方面的内容。我问这个问题是因为我目前正在面临这个问题。每次从我的应用程序包启动应用程序时,扩展并不总是被启用。 - Sandeep T D S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接