OS X Finder Sync Extension
Asked Answered
P

1

3

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 to finder. The code appears to be running the init methods and the toolbar items methods are getting called but nothing is displaying in finder.

The terminal is showing this when it runs

2015-04-20 12:45:52.700 pcssyncextension[3196:62451] Failed to connect (colorGridView) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable 2015-04-20 12:45:52.701 pcssyncextension[3196:62451] Failed to connect (view) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable 2015-04-20 12:45:58.887 pcssyncextension[3196:62451] -[FinderSync init] launched from /Users/user/Library/Developer/Xcode/DerivedData/findersynctest-dkyjmfmqzedkquhbhqxejzlzzukn/Build/Products/Debug/findersynctest.app/Contents/PlugIns/pcssyncextension.appex ; compiled at 12:36:01

Is there anything else I need to do to get this to work other that just creating an empty project and adding the Finder Sync Extension?

Platform answered 20/4, 2015 at 17:49 Comment(1)
This seems to be an intermittent bug with finder - restarting finder using the force quit dialog will allow the extension to run.Depredation
P
9

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")
Platform answered 23/4, 2015 at 14:59 Comment(6)
hi ... I am trying out a simple finder sync to add tool bar button.It gets attached the first time. It says "waiting to attach", when I stop and run again. Any idea why this is happening?Circumgyration
This happens make sure you are enabling the extension (see above example) also make sure you have it enabled in system preferences. Also it may not be added to your window in finder if you open finder and go to View->Customize Toolbar and make sure it is added. There is a way to do that programmatically but I haven't needed to do it so I am not sure what it isPlatform
If I create a Xcode project for finder sync extn from scratch, and running from debugger it works fine. I am able to toggle it in Sys Preferences->Extensions. But when I stop it from XCode and try to run again, it says "Waiting to Attach". I noticed that in this case the extension entry is still present in Sys Preferences->Extensions, though it is not checked. Is there any way to remove it from extensions. I tried removing from com.apple.finder.plist. But on relaunch it populates it again, dont know from where.Circumgyration
Use killall Finder to restart the finder and reload extension.Wilsonwilt
Hey, I am new to the mac dev, from the apple guideline it is told that if i add icons, the folder icon will change once i added the folder to the finder side bar, but it is not changing. Can you please tell me how to do that?Erminna
@DouglasCobb Hi, Is it essential that we add code to enable and disable the extension? The apple documentation doesn't mention anything of the sort. I ask this because, I currently am facing this issue. On launching the app from my app bundle, the extension isn't enabled everytime.Gader

© 2022 - 2024 — McMap. All rights reserved.