Search code examples
macoscocoafinder

How to get Finder sidebar favorites content cocoa?


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


Solution

  • 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);