objective-c - 如何使用用户最喜欢的文件夹填充 NSPopUpButton

标签 objective-c cocoa directory favorites

我是 Mac 编程新手,所以如果我的问题太愚蠢,请原谅我。

我正在编写一个小型应用程序,我需要在其中设置目标文件夹。 我认为,不只是使用 NSButton “选择文件夹”,而是使用 Firefox 或 Safari 的“另存为...” 方法对话框是一种非常用户友好的对话框。

使用NSPopUpButton,可以从用户的收藏夹或上次使用的文件夹中选择一个文件夹。另外,我会添加一个最上面的条目“选择...”,这将打开一个NSOpenPanel

我的问题是:如何获取用户最喜欢的文件夹,例如显示的在 Finder 应用程序中,并用它们填充我的 NSPopUpButton

这是它在 Firefox 中的样子: "save as" dialog

最佳答案

您可以在Application Services框架内找到相关功能,并且可以获得如下项目列表:

LSSharedFileListRef favorites = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
CFArrayRef snapshot = LSSharedFileListCopySnapshot(favorites, NULL);

CFIndex snapshotCount = CFArrayGetCount(snapshot);
for (CFIndex i = 0; i < snapshotCount; ++i) {
    LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(snapshot, i);
    CFURLRef itemURL = NULL;
    LSSharedFileListItemResolve(item, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, &itemURL, NULL);

    NSLog(@"%@", itemURL);
    if (itemURL != NULL) {
        CFRelease(itemURL);
    }
}
CFRelease(snapshot);
CFRelease(favorites);

当我在我的计算机上运行它时,我得到:

nwnode://domain-AirDrop
file://localhost/Applications/
file://localhost/System/Library/CoreServices/Finder.app/Contents/Resources/MyLibraries/myDocuments.cannedSearch/
file://localhost/Users/dave/
file://localhost/Users/dave/Desktop/
file://localhost/Users/dave/Developer/
file://localhost/Users/dave/Documents/
file://localhost/Users/dave/Downloads/
file://localhost/Users/dave/Dropbox/

对应于:

enter image description here

关于objective-c - 如何使用用户最喜欢的文件夹填充 NSPopUpButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12026438/

相关文章:

objective-c - 执行选择器可能会导致泄漏解决方法快速崩溃

iphone - “类消息的接收者类型“CCParticleBatchNode”是前向声明”

macos - 带背景颜色的 NSTabView

iphone - 如何在 UITextView 中设置边距(填充)?

iphone - Objective-C 双委托(delegate)协议(protocol)

objective-c - ASIHTTPRequest 禁用缓存

cocoa - NSArrayController 的 NSSortDescriptor 按字母顺序和字符串长度排序

file - grails:将文件上传到所选文件夹中

python - 如何使用python检查目录中所有图像的尺寸?

c++ - 如何让 C++ 程序查找 DLL?