5

Am currently working on a project, and though I can add sidebar items by adding to the com.apple.sidebarlists.plist file then restarting Finder, I can't seem to know how to make the folder icons show up using its assigned custom icon.

Can anyone point me to a link or a solution that would make me do this? Thanks a bunch in advance!

jopes
  • 245
  • 2
  • 13

3 Answers3

7

There's no supported way to do this using pure Cocoa. Use the LSSharedFileList API, instead. It's the supported way to manage the sidebar lists, and you can provide a custom icon (as an IconRef from Icon Services) when you insert an item.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Is this API available when running in a sandbox? – David Kocher Sep 27 '15 at 08:08
  • @DavidKocher Unfortunately, no. – dejuknow Apr 26 '16 at 18:55
  • @dejuknow: wrong. You use com.apple.coreservices.sharedlileslistd.xpc service for that. Basically, you create an NSXPCServiceConnection, create an SFLListChange object with SFLListItem for the FileURL you add, w/bookmarkdata containing the icons property (or grab existing one with FsRef/UrlBookmarkData/whatever) then publish your change to the SFLList via the XPC service et voila. Read app sandboxing guide for details. Google for coreservices/sharedfileslist.framework headers (or dump them with nm/otool/classdump). Good luck. – kervich Aug 06 '17 at 04:20
1

I don't know about the icons but I add items to the sidebar using the EBLaunchServices wrapper which is available open source here:

https://github.com/Eric-Bro/EBLaunchServices

It is very easy to use. For instance to add a mounted OSXFUSE volume to the sidebar you call:

NSURL* volumeUrl = [NSURL fileURLWithPath:@"/Volumes/MyVolume"];
[EBLaunchServices addItemWithURL:volumeUrl toList:kLSSharedFileListFavoriteVolumes];
codingFriend1
  • 6,487
  • 6
  • 45
  • 67
0

I was too stubborn to not do the obvious. I did:

[[NSWokspace sharedWorkspace] setIcon:someIcon forFile:someFile options:0];

to set the image icon for the folder. I from then I did com.apple.sidebarlists.plist NSUserDefaults appending, and restarted Finder through Apple Events. That made it all work.

jopes
  • 245
  • 2
  • 13