Know the last used documents in Cocoa
Is there a way to list the last used files using cocoa (or objective-c) functions?
I would like to h开发者_Go百科ave something like "recent documents"
Thanks for your help.
Regards,
Use -[NSDocumentController recentDocumentURLs]
. It returns an array of URLs representing the locations of the documents recently opened by your application.
Edit based on comment: In that case, you need to use the Launch Services API. For example:
- (NSArray *)globalRecentDocumentsURLs {
LSSharedFileListRef recentDocsFileList;
NSArray *recentDocsFiles;
NSMutableArray *recentDocsURLs = nil;
UInt32 seed;
recentDocsFileList = LSSharedFileListCreate(NULL,
kLSSharedFileListRecentDocumentItems, NULL);
if (! recentDocsFileList) return nil;
recentDocsFiles = (NSArray *)LSSharedFileListCopySnapshot(recentDocsFileList,
&seed);
if (recentDocsFiles) {
recentDocsURLs = [NSMutableArray array];
for (id file in recentDocsFiles) {
CFURLRef fileURL = NULL;
LSSharedFileListItemResolve((LSSharedFileListItemRef)file, 0,
&fileURL, NULL);
if (fileURL) [recentDocsURLs addObject:[(id)fileURL autorelease]];
}
[recentDocsFiles release];
}
CFRelease(recentDocsFileList);
return recentDocsURLs;
}
精彩评论