OS X Finder Sync Extension

前端 未结 1 1847
时光说笑
时光说笑 2021-01-14 10:20

I am not able to create a simple Finder Sync Extension.

I have created a new OS X project and added the Finder Sync Extension target and I ran the extension attached

相关标签:
1条回答
  • 2021-01-14 10:36

    I was able to find a few things that helped me. By default the toolbar item is not added to the finder window unless the user drags it in. I was not able to find a way to programmatically add the item to the finder window toolbar.

    Add Item to the finder side bar

    // Create a reference to the shared file list.
    LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
    
    // Check Items
    if (favoriteItems)
    {
        // Get CFURL for Application
        CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
    
        // Add Item
        LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, url, NULL, NULL);
    
        // Release
        if (item)
            CFRelease(item);
    }
    
    // Release
    if (favoriteItems != NULL)
        CFRelease(favoriteItems);
    

    Code to Remove Item From Sidebar

    // Create a reference to the shared file list.
    LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
    
    // Check Items
    if (favoriteItems)
    {
        // Get Login Items
        CFArrayRef favoriteItemsArray = LSSharedFileListCopySnapshot(favoriteItems, NULL);
    
        // Loop Through Items
        for (id item in (__bridge NSArray *)favoriteItemsArray)
        {
            // Get Item Ref
            LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
    
            // Get Item URL
            CFURLRef itemURL = LSSharedFileListItemCopyResolvedURL(itemRef, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, NULL);
            if (itemURL != NULL)
            {
                // If Item Matches Remove It
                if ([[(__bridge NSURL *)itemURL path] hasPrefix:path])
                    LSSharedFileListItemRemove(favoriteItems, itemRef);
    
                // Release
                if (itemURL != NULL)
                    CFRelease(itemURL);
            }
        }
    
        // Release
        if (favoriteItemsArray != NULL)
            CFRelease(favoriteItemsArray);
    }
    
    // Release
    if (favoriteItems != NULL)
        CFRelease(favoriteItems);
    

    Reload Directory in Finder

    // Reload Finder (change the word directory to file if updating file)
    NSAppleScript * update = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"tell application \"Finder\" to update POSIX directory \"%@\"",path]];
    [update executeAndReturnError:nil];
    

    Code to Enable Extension (bundle ID)

    system("pluginkit -e use -i com.mycompany.finderExt")
    

    Code to Disable Extension (bundle ID)

    system("pluginkit -e ignore -i com.mycompany.finderExt")
    
    0 讨论(0)
提交回复
热议问题