问题
I need to get paths of objects displayed in Favorites section of Finder Sidebar (for current user). How can I achieve this?
回答1:
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);
回答2:
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).
回答3:
Use LSSharedFileList
API(LaunchServices/LSSharedFileList.h.)
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
kLSSharedFileListFavoriteItems, NULL);
来源:https://stackoverflow.com/questions/10751199/how-to-get-finder-sidebar-favorites-content-cocoa