Default Download Directory
Friday, July 7, 2006
Have you ever wondered how to access the user's default download directory/folder (as used by Safari for example) using Cocoa? You cannot directly. So we wrote a category for NSWorkspace calling into the legacy InternetConfig library which is now part of Carbon - so do not forget to link against Carbon when using this code.
Thanks to Uli Kusterer for the original!
@implementation NSWorkspace (OPExtensions)
- (NSString*) downloadDirectory
/*" Returns the user's std. download directory path
taken from Internet Config. "*/
{
// Code modified from Uli Kusterer's UKPathUtilities:
ICInstance inst = NULL;
ICFileSpec fSpec;
long length = kICFileSpecHeaderSize;
FSRef fref;
unsigned char fpath[PATH_MAX] = { 0 };
if (ICStart(&inst, 0L ) != noErr) goto cleanup;
ICGetPref(inst, kICDownloadFolder, NULL, &fSpec, &length);
ICStop(inst);
if (FSpMakeFSRef( &fSpec.fss, &fref ) != noErr ) goto cleanup;
if (FSRefMakePath( &fref, fpath, 1024 ) != noErr ) goto cleanup;
cleanup: ;
if (fpath[0] == 0) return [NSHomeDirectory()
stringByAppendingPathComponent: @"Desktop"];
return [NSString stringWithUTF8String: (char*)fpath];
}
@end
Have fun!
Dirk
2 Comments
On Nov 14, 2009,
Anonymous User
wrote:
"I No Have Default"
Yes you do. It's either the desktop or the downloads folder.
123
I No Have Default