Default Download Directory

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

Print this page  PDF version

2 Comments

On Jun 27, 2008, Anonymous User wrote:

123

I No Have Default


On Nov 14, 2009, Anonymous User wrote:

"I No Have Default"

Yes you do. It's either the desktop or the downloads folder.


Leave a comment...

Your Name

Your Web site URL (optional)

Your e-mail address (optional)

Title

Your comment here please:
Note: You can use Wiki markup to format your comment. No HTML please.