【问题标题】:How to get Finder sidebar favorites content cocoa?如何获取 Finder 侧边栏收藏夹内容可可?
【发布时间】:2012-05-25 08:41:31
【问题描述】:

我需要获取 Finder 侧边栏的收藏夹部分中显示的对象路径(针对当前用户)。我怎样才能做到这一点?

【问题讨论】:

    标签: macos cocoa finder


    【解决方案1】:

    获取共享文件列表只是第一部分,您仍然可能希望获取带有路径的实际字符串对象。这是一个小代码 sn-p,它可以让您在 finder 侧边栏的收藏夹部分中获取每个对象的路径。

    UInt32 seed;
    LSSharedFileListRef sflRef = LSSharedFileListCreate(NULL,
                                                        kLSSharedFileListFavoriteItems,
                                                        NULL);
    CFArrayRef items = LSSharedFileListCopySnapshot( sflRef, &seed );
    for( size_t i = 0; i < CFArrayGetCount(items); i++ )
    {
        LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(items, i);
        if( !item )
            continue;
        CFURLRef outURL = NULL;
        LSSharedFileListItemResolve( item, kLSSharedFileListNoUserInteraction, (CFURLRef*) &outURL, NULL );
        if( !outURL )
            continue;
        //The actual path string of the item
        CFStringRef itemPath = CFURLCopyFileSystemPath(outURL,kCFURLPOSIXPathStyle);
        // TODO: Do whatever you want to do with your path here!!!!
        CFRelease(outURL);
        CFRelease(itemPath);
    }
    CFRelease(items);
    CFRelease(sflRef);
    

    【讨论】:

    • 请注意,由于 macOS Monterey LSSharedFileListItemResolve() 将触发桌面、下载和文档文件夹的 TCC 权限对话框的显示,因此在首次运行时,之前未向应用授予任何权限。
    【解决方案2】:

    本身并没有 Cocoa API。您将使用 LSSharedFileList API。 API 是公开的,但唯一的文档是头文件 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Headers/LSSharedFileList.h。您想要kLSSharedFileListFavoriteItems(可能还有kLSSharedFileListFavoriteVolumes)列表类型。

    【讨论】:

    • 你能写代码示例吗?我不确定我是否了解如何使用它。
    【解决方案3】:

    使用LSSharedFileList API(LaunchServices/LSSharedFileList.h.)

     LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                                kLSSharedFileListFavoriteItems, NULL);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-23
      • 2015-08-24
      • 2013-02-06
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多