How do you programmatically put folder icons on the Finder sidebar, given that you have to use a custom icon for the folder?

我的梦境 提交于 2019-11-28 07:05:55

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.

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];

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!