Programmatically add a folder to “Places” in Finder

后端 未结 1 794
闹比i
闹比i 2020-12-29 00:48

I\'m trying to figure out how to programatically add a folder to Finder\'s Places sidebar. I\'ve seen ways to modify it through the Finder Preferences, but I\'ve also seen s

相关标签:
1条回答
  • 2020-12-29 00:56

    Try this:

    -(void) addPathToSharedItem:(NSString *)path
    {
        CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:path]; 
    
        // Create a reference to the shared file list.
        LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                                kLSSharedFileListFavoriteItems, NULL);
        if (favoriteItems) {
            //Insert an item to the list.
            LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems,
                                                                         kLSSharedFileListItemLast, NULL, NULL,
                                                                         url, NULL, NULL);
            if (item){
                CFRelease(item);
            }
        }   
    
        CFRelease(favoriteItems);
    }
    
    0 讨论(0)
提交回复
热议问题