Skip to content

Instantly share code, notes, and snippets.

@nanoant
Created September 27, 2011 10:52
Show Gist options
  • Save nanoant/1244807 to your computer and use it in GitHub Desktop.
Save nanoant/1244807 to your computer and use it in GitHub Desktop.
Change your Lion sidebar item icons
// (l) copyleft 2011 Adam Strzelecki nanoant.com
//
// Usage:
// sidebar - to see the ids of your favorite sidebar items
// sidebar ItemName - to remove item specific icon
// sidebar ItemName sbBj - to set specific item icon, see:
// /System/Library/CoreServices/CoreTypes.bundle/Contents/Info.plist
// Compile it with:
// gcc -o sidebar sidebar.m -F /Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks -framework Foundation -framework CoreServices
//
// Some interesting IDs:
// com.apple.LSSharedFileList.SpecialItemIdentifier
// com.apple.LSSharedFileList.TemplateSystemSelector
#import <Foundation/Foundation.h>
#import <LaunchServices/LaunchServices.h>
extern CFTypeRef LSSharedFileListItemCopyAliasData(LSSharedFileListItemRef inItem);
extern int _IconRefIsTemplate(IconRef iconRef);
extern CFStringRef kLSSharedFileListSpecialItemIdentifier;
extern CFStringRef kLSSharedFileListItemTargetName;
extern CFStringRef kLSSharedFileListItemManaged;
// I am so smart, this symbol isn't exposed by LaunchServices framework, but I get it anyhow with help of "nm" ;)
#define kLSSharedFileListItemTemplateSystemSelector (CFStringRef)((char *)kLSSharedFileListItemTargetName + (3 * 0x40))
#define kLSSharedFileListItemClass (CFStringRef)((char *)kLSSharedFileListItemBeforeFirst + (3 * 0x40))
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
LSSharedFileListRef sflRef = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListFavoriteItems, NULL);
UInt32 seed;
SInt32 type = 0;
CFStringRef desiredNameRef = NULL;
if(argc >= 2) {
desiredNameRef = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, argv[1], kCFStringEncodingUTF8, NULL);
if(argc >= 3) {
type = atol(argv[2]);
if(!type && strlen(argv[2]) != 4) {
NSLog(@"type must be 4 characters");
return 254;
}
if(!type) type = (argv[2][3] << 0) + (argv[2][2] << 8) + (argv[2][1] << 16) + (argv[2][0] << 24);
}
}
if(!sflRef) {
NSLog(@"No list!");
return 1;
}
NSArray *list = [(NSArray *)LSSharedFileListCopySnapshot(sflRef, &seed) autorelease];
LSSharedFileListItemRef sflItemBeforeRef = (LSSharedFileListItemRef)kLSSharedFileListItemBeforeFirst;
for(NSObject *object in list) {
LSSharedFileListItemRef sflItemRef = (LSSharedFileListItemRef)object;
CFStringRef nameRef = LSSharedFileListItemCopyDisplayName(sflItemRef);
CFURLRef urlRef = NULL;
LSSharedFileListItemResolve(sflItemRef, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, &urlRef, NULL);
if(!desiredNameRef) {
UInt32 itemId = LSSharedFileListItemGetID(sflItemRef);
NSLog(@"[%u] %@ -> %@", itemId, (id)nameRef, (id)urlRef);
if(urlRef) CFRelease(urlRef);
if(nameRef) CFRelease(nameRef);
CFStringRef props[] = {
// kLSSharedFileListItemClass,
kLSSharedFileListItemTemplateSystemSelector,
kLSSharedFileListSpecialItemIdentifier,
kLSSharedFileListItemManaged,
};
int i;
for(i = 0; i < sizeof(props)/sizeof(*props); i++) {
CFTypeRef propRef = LSSharedFileListItemCopyProperty(sflItemRef, props[i]);
NSLog(@" %p: %@ = %@ (%@)", props[i], (id)props[i], (id)propRef, propRef ? (id)CFCopyTypeIDDescription(CFGetTypeID(propRef)) : nil);
if(propRef) CFRelease(propRef);
}
} else {
if(CFEqual(nameRef, desiredNameRef)) {
if(!type) {
NSLog(@"Cleared: %@", nameRef);
LSSharedFileListInsertItemURL(sflRef, sflItemBeforeRef, nameRef, NULL, urlRef, NULL, (CFArrayRef)[NSArray arrayWithObject:(id)kLSSharedFileListItemTemplateSystemSelector]);
} else {
LSSharedFileListItemSetProperty(sflItemRef, kLSSharedFileListItemTemplateSystemSelector, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &type));
}
}
}
sflItemBeforeRef = sflItemRef;
}
CFRelease(sflRef);
[pool drain];
return 0;
}
@vangelion
Copy link

Hallelujah... After searching for a solution for whole f****** day your little app solved my problem with sidebar icons. You are a genius man. Thank you thousand times...

@Aeon
Copy link

Aeon commented Mar 25, 2013

Thanks for sharing, this helped tremendously in writing https://github.com/Aeon/sidebarFinagler

@steve-ross
Copy link

I am on ML 10.8.3 and compiled this but, I'm not seeing anything update on the sidebar I got it to run without an error but none of the icons are updating even after I 'killall Finder'

running it like: ./sidebar Video sbDl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment