Finder file icon badging (icon overlay) on Mac OS X >= 10.6

后端 未结 5 1295
鱼传尺愫
鱼传尺愫 2020-12-30 15:44

I\'m searching for a solution to do File icon overlays (icon badging) (like Dropbox does it on mac) with cocoa on Mac.

Does anyone knows a way to do this?

I\

5条回答
  •  一整个雨季
    2020-12-30 16:18

    You can use the following two methods to have icon overlay over the folders/files.

    1. You can use -setIcon:forFile:options: method on NSWorkspace if you want to change the icon of a file or folder in Mac OS X.
      However after you apply icon overlay using this method, overlay exits even though you moved that file/folder outside. This may not be the exact solution.

    2. Instead use the Finder Sync Extension target (File - New - Target - Finder Sync Extension) inside your app.
      Once you created the extension, your application doesn't have direct communication with this target. In order to activate, use AppleScript command (I don't think there is a better alternative for this.)

    To activate

    NSString *pluginPath = [[[NSBundle mainBundle] builtInPlugInsPath] stringByAppendingPathComponent:@"yourextension.appex"];
    
    NSString *pluginkitString = [NSString stringWithFormat:@"pluginkit -e use -a \"%@\"", pluginPath];
    system([pluginkitString cStringUsingEncoding:NSUTF8StringEncoding]);
    

    Once target activated, there are couple of ways were our application can communicate with that extension. Few of them are:

    Using NSDistributedNotificationCenter. This class provides a way to send notifications to objects in other tasks(like the extension here).

    Other way is to use:

    [[NSUserDefaults alloc] initWithSuiteName:@"teamid.com.company.test"];
    

    Both your application and the target should have common group identifier(i.e "teamid.com.company.test").

    For this enable "App Groups" under Target - Capabilities - App Groups and give the identifier like the above (i.e"teamid.com.company.test") were teamid is the id that you will get it from your apple developer portal. Do the same steps for your Extension target as well.

    Before to conclude, be sure that extension is activated or not. To check that go to System Preference - Extensions - Your App Finder. This is the global point were user can enable/disable icon overlay for your application.

提交回复
热议问题