How to get Finder sidebar favorites content cocoa?

前端 未结 3 1894
情深已故
情深已故 2021-01-06 13:06

I need to get paths of objects displayed in Favorites section of Finder Sidebar (for current user). How can I achieve this?

相关标签:
3条回答
  • 2021-01-06 13:15

    Use LSSharedFileList API(LaunchServices/LSSharedFileList.h.)

     LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                                kLSSharedFileListFavoriteItems, NULL);
    
    0 讨论(0)
  • 2021-01-06 13:36

    There's not a Cocoa API, per se. You would use the LSSharedFileList API. The API is public but the only documentation is the header file, /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Headers/LSSharedFileList.h. You want the kLSSharedFileListFavoriteItems (and maybe kLSSharedFileListFavoriteVolumes) list type(s).

    0 讨论(0)
  • 2021-01-06 13:40

    Getting the shared file list is only the first part, you still may want to get an actual string object with your path. Here is a little code snippet that will let you get a path for each object in the favorites section of the finder sidebar.

    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);
    
    0 讨论(0)
提交回复
热议问题