4

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 some applications actually add folders to the sidebar.

If someone has any advice/pointers on what I should look up, it would be greatly appreciated

(This is for Snow Leopard and Leopard... hopefully it didn't change)

mystro
  • 347
  • 3
  • 11

1 Answers1

11

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);
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
mself
  • 119
  • 1
  • 3
  • 3
    how to do it in sandbox? above fails there and only a hack to directly write to that folder works for me :D – Daij-Djan Nov 08 '12 at 09:47
  • i have try but it's worked only when sandbox mode is disable.. (:(: do you anyone know how can add folder in favorites in sandbox mode..? – nitin kachhadiya Apr 04 '15 at 05:51
  • 1
    This is deprecated on MAC OS 10.11. Any idea on supported APIs on ElCapitan. – Lakshmi Dec 02 '15 at 15:54
  • @LakshmiS did you figure out what to use instead? I'm in a similar boat – Ben J Apr 22 '16 at 10:44
  • @Ben, Though the code mentioned here is deprecated, it is working fine on OS 10.12.x too. I am accessing the above code in try-catch block on a safer side. – Lakshmi May 10 '17 at 16:56