Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LSSharedFileListInsertItemURL Not Changing Name

I'm running into an issue with LSSharedFileListInsertItemURL. I'm attempting to add an item to the Finder sidebar which works great. The only thing it doesn't do is change the name of the item in the sidebar. I'm pushing "FolderName" as an argument but after this function is run, the item is not renamed. It does flash for a second with the name but quickly changes back to its actual name. I've searched as much as I can to find a solution for this and have come up with nothing. If anyone sees an issue with my code or has a "hack" to get this working, please let me know.

-(void) addPathToSharedItem:(NSString *)path
{

    CFURLRef url = (__bridge 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.
        CFStringRef mdcName = CFSTR("FolderName");

        LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemLast, mdcName, NULL, url, NULL, NULL);

        if (item){

            CFRelease(item);
        }
    }

    CFRelease(favoriteItems);
}
like image 680
Sam Bantner Avatar asked May 07 '26 14:05

Sam Bantner


1 Answers

[I know this question was asked a long time ago but it appears no proper answer could be find elsewhere.]

Finder not refreshing Favorite name after an LSSharedFileListInsertItemURL is actually a known bug reported to Apple in 2013.

We've found that adding manually any other folder to favorites would refresh Favorites section and show proper name previously set through LSSharedFileListInsertItemURL.

A quite dirty workaround is simply to automatize this by inserting any other item then deleting it immediately.

The following code (in C++ sorry, but it can be easily ported to Objective C) implements this :

// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
if (!favoriteItems)
  return false;

//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL( favoriteItems, //Insert in this list
                                                             kLSSharedFileListItemBeforeFirst, //Here
                                                             (CFStringRef) shortCurtNameNS, //Shortcut name
                                                             NULL, //Icon
                                                             url,  //URL / path
                                                             NULL,
                                                             NULL);
if (item)
  CFRelease(item);


// Here it goes dark. Really dark.
// Finder does not refresh Favorites until another one is inserted "manually".
// The following lines just emulates this : insert another item then immediately remove it. This will refresh favs.
// KarmaPoints--;
CFURLRef dummy = (__bridge CFURLRef)[NSURL fileURLWithPath:@"/"];
NSString * dummyName = [NSString stringWithCString:"Root" encoding:[NSString defaultCStringEncoding]];
LSSharedFileListItemRef dummyItem = LSSharedFileListInsertItemURL( favoriteItems, //Insert in this list
                                                             kLSSharedFileListItemLast, //Here
                                                             (CFStringRef) dummyName, //Shortcut name
                                                             NULL, //Icon
                                                             dummy,  //URL / path
                                                             NULL,
                                                             NULL);
// Remove it
LSSharedFileListItemRemove(favoriteItems, dummyItem);
if (dummyItem)
  CFRelease(dummyItem);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!