Add Item to Finder Sidebar
Asked Answered
L

3

7

I would like to add a new item to the Finder sidebar. I found out that the Finder keeps the list of "places" in `~/Library/Preferences/com.apple.sidebarlists.plist. I was able to read the file using Carbon API and saw that each item had Name, icon and alias.

Using a 3rd party application such as PlistEdit Pro I was able to update the alias. My question is how to update the alias using Carbon API. Was not able to find a way to create alias that will open in Finder. It seem that both Dropbox and PlistEditor Pro was able to find the way.

Linzy answered 31/1, 2011 at 15:16 Comment(0)
I
6

Take a look here:

The Shared File List API is new to Launch Services in Mac OS X Leopard. This API provides access to several kinds of system-global and per-user persistent lists of file system objects, such as recent documents and applications, favorites, and login items. For details, see the new interface file LSSharedFileList.h.

You want to look for the key kLSSharedFileListFavoriteItems, which handles the items under "Places" in the Sidebar. I guess you could try to do something similar to this, using LSSharedFileListCreate to create kLSSharedFileListFavoriteItems.

Or you could use the applescript posted here, which would be way easier, but not the "Right Way"©

Injudicious answered 31/1, 2011 at 16:43 Comment(1)
@Linzy Great! I guess could select my answer as solution then! :-)Injudicious
D
3

Update for 2015

The LSSharedFileList header says this has moved to the CoreServices framework. In fact, if you Cmd-Shift-O (in Xcode) and type LSSharedFileList, then navigate to the only result, you'll see in the jump bar that the header is indeed now contained within CoreServices.framework. In any case, the key is still kLSSharedFileListFavoriteItems.

Example:

+ (BOOL)appendFavoriteItemWithURL:(NSURL *)url {

  // Pessimism ...
  BOOL result = NO;

  // Do we have a file URL?
  if (url.isFileURL) {

    // Ask CoreServices for the favorite items list 
    // (kLSSharedFileListFavoriteItems)
    LSSharedFileListRef list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
    if (list) {

      // We've got the list, so try to append our item
      // (use kLSSharedFileListItemBeforeFirst vs. 
      // kLSSharedFileListItemLast if desired)
      LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(list,
                                     kLSSharedFileListItemLast,
                                     NULL,
                                     NULL,
                                     (__bridge CFURLRef)url,
                                     NULL,
                                     NULL);

      // Did it work?
      if (item) {

        // Release the item and flag success
        CFRelease(item);
        result = YES;

      }

      // Release the list
      CFRelease(list);

    }

  }

  return result;
}

Usage:

// Create the path to the favorite item to add
NSString * itemPath = [@"~/Music" stringByExpandingTildeInPath];
NSURL * itemURL = [NSURL fileURLWithPath:itemPath];

// Insert the item
[WhateverClassTheAboveFunctionIsIn appendFavoriteItemWithURL:itemURL];
Deformed answered 23/6, 2015 at 15:37 Comment(3)
I created this Gist that wraps this functionality into a simple NSURL category: gist.github.com/jnozzi/817f3276c55dbb7025beDeformed
Up-voted, I like this solution, but its a shame that some of those functions are now deprecated on 10.11. Any idea what to use instead?Eligible
Not at the moment, Ben. Are the deprecated methods annotated in the headers? They may suggest the alternatives.Deformed
P
1

@Asmus : By default 'command + T' is the shortcut to add a folder to sidebar in finder. Applescript pointed by u is working fine when keyboard shortcut key 'command + T' is assigned manually to other tasks.

The applescript fails if executed after setting 'command + T' as the shortcut key to show my other desktop in osx lion(10.7)

Update 2023: Due to the availability of Tabs in Finder and other applications, command + T creates a new Tab. The new default shortcut is command + ctrl + T!

Perse answered 24/5, 2012 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.