Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Write OS X Finder plugin

I'm looking for a guide or sample code for writing Mac OS X Finder plugins? It would like to know how to do some simple actions:

  1. adding image overlayers to icons
  2. adding context menu items
  3. listen to file changes

I found the following two resources:

  • Writing Contextual Menu Plugins for OS X: An outdated document from 2002 that uses the COM API targeting Mac OS X 8/9.
  • SCPlugin: Open-source SVN Mac application that includes a Finder plug-in.

I am tempted to review the SCPlugin code, but was hoping to find an easier sample to digest.

like image 282
notnoop Avatar asked Aug 18 '09 14:08

notnoop


People also ask

How do I customize my Mac Finder?

Change what's in the toolbar: Choose View > Customize Toolbar in the menu bar. You can drag items into and out of the toolbar, add a space between items, and choose whether to show text with the icons. Rearrange the items in the toolbar: Press and hold the Command key, then drag an item to a new location.

What are Finder Extensions on Mac?

FinderUtilities is a macOS App Extension (Finder Extension), which enables you to easily launch Terminal. app to the selected directory, create empty files in Finder's folder hierarchy and also enables copying of selected file or directory paths to the pasteboard (clipboard) using right-click (or control-click).

How do I add applications to Finder on Mac?

In the Finder menu bar, select Go and choose Applications in the menu. Locate the application you want to add to the sidebar, press and hold the Command key, and drag the application to the Favorites section of the Finder sidebar.


2 Answers

The Finder Icon Overlay example project represents a small and very basic but actually working example of the answer below.

https://github.com/lesnie/Finder-Icon-Overlay

I know this is so old, but some may be still interested in topic (?)

Here is what I have it done under Leopard (10.6). At first proper Finder's headers are needed. Use class-dump tool to get it. Then write your code as a SIMBL plugin (refer to documentation how to do it), swizzling some methods. For instance to draw something over icon in ListView, drawIconWithFrame: method of TIconAndTextCell method must be overriden.

Here's the code for method swizzling:

+ (void) Plugin_load {     Method old, new;     Class self_class = [self class];     Class finder_class = [objc_getClass("TIconAndTextCell") class];      class_addMethod(finder_class, @selector(FT_drawIconWithFrame:),                     class_getMethodImplementation(self_class, @selector(FT_drawIconWithFrame:)),"v@:{CGRect={CGPoint=dd}{CGSize=dd}}");      old = class_getInstanceMethod(finder_class, @selector(drawIconWithFrame:));     new = class_getInstanceMethod(finder_class, @selector(FT_drawIconWithFrame:));     method_exchangeImplementations(old, new);  } 

I am overriding "drawIconWithFrame:" method with my method "FT_drawIconWithFrame:". Below is sample implementation for this method.

- (void) FT_drawIconWithFrame:(struct CGRect)arg1 {     [self FT_drawIconWithFrame:arg1];     if ([self respondsToSelector:@selector(node)]) {         if ([[[[NSClassFromString(@"FINode") nodeWithFENode:[(TNodeIconAndNameCell *)self node]] fullPath] lastPathComponent] hasPrefix:@"A"])             [myPrettyIconOverlayImage drawInRect:NSMakeRect(arg1.origin.x, arg1.origin.y, arg1.size.height, arg1.size.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];     } } 

Essentially it draws "myPrettyIconOverlayImage" over every icon for file with filename starts with letter "A". This logic is up to you.

Pay attention to this line: [self FT_drawIconWithFrame:arg1]; this is how to call 'super' in order to get normal icon and name etc. I know, looks weird, like loop, but actually it isn't. Then wrap in into SIMBL plugin, install SIMBL and ...run.

Due to changes in Lion some work have to be done from scratch (make new "Finder.h" file with all declarations needed in it, find proper classess and methods to override), but this technique still works.

Happy hacking!

like image 89
Les Nie Avatar answered Sep 30 '22 08:09

Les Nie


For Yosemite (MacOS 10.10 & newer), you can use Apple's FinderSync framework, which allows Finder extensions to:

  • Express interest in specific folder hierarchies
  • Provide "badges" to indicate the status of items inside those hierarchies
  • Provide dynamic menu items in Finder contextual menus, when the selected items (or the window target) are in those hierarchies
  • Provide a Toolbar Item that displays a menu with dynamic items (even if the selection is unrelated)
like image 24
Michael Dautermann Avatar answered Sep 30 '22 08:09

Michael Dautermann