objective-c - OS X Finder 同步扩展

标签 objective-c macos finder findersync osx-extensions

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

我创建了一个新的 OS X 项目并添加了 Finder 同步扩展目标,然后我运行了附加到 finder 的扩展。代码似乎正在运行 init 方法,并且正在调用工具栏项方法,但 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 同步扩展之外,我还需要做些什么来让它工作吗?

最佳答案

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

将项目添加到查找器侧栏

// 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")

关于objective-c - OS X Finder 同步扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754914/

相关文章:

c - C 程序如何判断它是否是从 Finder 启动的?

java - 从 Java 控制台应用程序公开 macOS Finder 中的文件

objective-c - 在 C 或 Objective C 中将 int、double 转换为 char 数组

ios - 如何将数据从一个 View 传递到下一个 View ?

ios - 强制对象存活直到 block 被执行的正确方法

objective-c - nextKeyView socket 无法正常工作

objective-c - 10.5 上未触发鼠标拖动事件

Objective-C:超出十六进制值模式的正则表达式

bash - 无法在 macOS 上修改/etc/shells 以包含 brew 安装的 bash 版本

macos - 通过 Applescript 在 Finder 中打开 "Get Information"窗口(再次)