how to make a disk to show in finder device without adding "local" option

152 views
Skip to first unread message

ElvisCheng

unread,
Jun 11, 2016, 10:44:02 PM6/11/16
to OSXFUSE
Hi all,
I have a question,when mount a disk with "local" option  through FUSE, the disk can show  in finder device Sidebar. But I am developing a network disk and not able to add "local" option, so how to show the disk in the finder device Sidebar??

thanks

mixtly

unread,
Sep 30, 2020, 5:08:22 PM9/30/20
to OSXFUSE
You could add Finder Sidebar Favorite item programmatically. Maybe this can help:

+ (void) addDriveToFinderFavorites
{
    @try
    {
        // this method will freeze at CFBridgingRetain if called on main queue
        assert(![NSThread isMainThread]);

        NSString* path = SREG.context.mountPath;
        CFURLRef url = CFBridgingRetain([NSURL fileURLWithPath:path]);

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

        Log(kLogCore, @"Attempting to add mount disk to Finder Favorites sidebar: %@", X(path));
        BOOL alreadyAddedToFinder = [self isMountUrl:url addedTo:favoriteItems];
        if (favoriteItems && !alreadyAddedToFinder)
        {
            Log(kLogCore, @"Adding mount disk to Finder Favorites sidebar.");
            LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems,
                                                                         kLSSharedFileListItemLast, NULL, NULL,
                                                                         url, NULL, NULL);
            if (item)
            {
                CFRelease(item);
            }
        }

        if (!favoriteItems)
        {
            CDLogWarn(kLogCore, @"FinderFavoritesSidebar: No favorite items found.");
        }

        if (alreadyAddedToFinder)
        {
            Log(kLogCore, @"FinderFavoritesSidebar: item already added");
        }

        CFRelease(favoriteItems);
    }
    @catch (NSException* e)
    {
        CDLogError(kLogCore, @"Exception trying to add drive to Finder favorites sidebar: %@", e);
    }
}

+ (void) removeDriveFromFinderFavoritesForMountPath:(NSString*)path
{
    @try
    {
        assert(![NSThread isMainThread]);

        Log(kLogCore, @"Removing Finder sidebar path: %@", X(path));
        LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
        CFArrayRef favoriteItemsCfArray = LSSharedFileListCopySnapshot(favoriteItems, NULL);
        NSArray* favoriteItemsArray = (__bridge NSArray*) favoriteItemsCfArray;

        NSString* mountPath = [NSString stringWithFormat:@"%@/", path];
        for (id item in favoriteItemsArray)
        {
            if ([[[[item valueForKey:@"_internalItem"] valueForKey:@"bookmark"] description] hasSuffix:mountPath])
            {
                LSSharedFileListItemRef k = (__bridge LSSharedFileListItemRef) (item);
                LSSharedFileListItemRemove(favoriteItems, k);
                return;
            }
        }
    }
    @catch (NSException* e)
    {
        CDLogError(kLogCore, @"Exception in removeDriveFromFinderFavoritesForMountPath: %@", e);
    }
}

+ (BOOL) isMountUrl:(CFURLRef)url addedTo:(LSSharedFileListRef)favoriteItems
{
    NSString* lastPathComponent = ((__bridge NSURL*) url).lastPathComponent;
    NSArray* favoriteItemsArray = (__bridge NSArray*) LSSharedFileListCopySnapshot(favoriteItems, nil);
    for (NSUInteger i = 0; i < favoriteItemsArray.count; i++)
    {
        LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef) favoriteItemsArray[i];
        NSString* name = (__bridge NSString*) LSSharedFileListItemCopyDisplayName(currentItemRef);
        if ([name isEqualToString:lastPathComponent])
        {
            return YES;
        }
    }
    return NO;
}


Reply all
Reply to author
Forward
0 new messages