【发布时间】:2012-05-25 08:41:31
【问题描述】:
我需要获取 Finder 侧边栏的收藏夹部分中显示的对象路径(针对当前用户)。我怎样才能做到这一点?
【问题讨论】:
我需要获取 Finder 侧边栏的收藏夹部分中显示的对象路径(针对当前用户)。我怎样才能做到这一点?
【问题讨论】:
获取共享文件列表只是第一部分,您仍然可能希望获取带有路径的实际字符串对象。这是一个小代码 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);
【讨论】:
本身并没有 Cocoa API。您将使用 LSSharedFileList API。 API 是公开的,但唯一的文档是头文件 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Headers/LSSharedFileList.h。您想要kLSSharedFileListFavoriteItems(可能还有kLSSharedFileListFavoriteVolumes)列表类型。
【讨论】:
使用LSSharedFileList API(LaunchServices/LSSharedFileList.h.)
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
kLSSharedFileListFavoriteItems, NULL);
【讨论】: