How to get the contents of the favorite sidebar finder cocoa?

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

+3
source share
3 answers

Getting a general list of files is only the first part, you can still get the actual string object with your path. Here is a small piece of code that will allow you to get the path for each object in the Favorites section of the crawler 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);
+3
source

API Cocoa, . API LSSharedFileList. API , , /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Headers/LSSharedFileList.h. kLSSharedFileListFavoriteItems (, , kLSSharedFileListFavoriteVolumes).

+3

Use API LSSharedFileList(LaunchServices / LSSharedFileList.h.)

 LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                            kLSSharedFileListFavoriteItems, NULL);
+2
source

All Articles