我是 Mac 编程新手,所以如果我的问题太愚蠢,请原谅我。
我正在编写一个小型应用程序,我需要在其中设置目标文件夹。
我认为,不只是使用 NSButton
“选择文件夹”,而是使用 Firefox 或 Safari 的“另存为...” 方法对话框是一种非常用户友好的对话框。
使用NSPopUpButton
,可以从用户的收藏夹或上次使用的文件夹中选择一个文件夹。另外,我会添加一个最上面的条目“选择...”,这将打开一个NSOpenPanel
。
我的问题是:如何获取用户最喜欢的文件夹,例如显示的在 Finder 应用程序中,并用它们填充我的 NSPopUpButton
?
这是它在 Firefox 中的样子:
最佳答案
您可以在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/
对应于:
关于objective-c - 如何使用用户最喜欢的文件夹填充 NSPopUpButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12026438/