Follow the steps below to add custom context menu command in macOS Finder:

1. Implement Context Menu Interface

Implement the IMenuCommand interface. The IMenuCommand.InvokeAsync() method of this interface is called when the menu is executed. It receives a list of selected items as an input parameter:

[Guid("28d0e0cb-5df7-432c-a06f-297c06b5ec5d")]
public class MyMenuCommand : IMenuCommand
{
    public async Task InvokeAsync(IEnumerable<string> filesPath)
    {
        foreach (string userFileSystemPath in filesPath)
        {

        }
    }
}

 

2. Return Context Menu Implementation from the Engine

Implement the IEngine.GetMenuCommandAsync() method and return your menu implementation. The Engine will call this method passing the menu GUID parameter, so you can implement multiple menu commands on the first level of the menu:

public class VirtualEngine : EngineMac
{
    ...
    public override async Task<IMenuCommand> GetMenuCommandAsync(Guid menuGuid)
    {
        Guid myMenuCommandGuid = typeof(MyMenuCommand).GUID;

        if (menuGuid == myMenuCommandGuid)
        {
            return new MyMenuCommand();
        }

        throw new NotImplementedException();
    }
}

 

3. Register Menu in the Info.plist File

Add menu registration in info.plist file. Note the GUID specified as NSExtensionFileProviderActionIdentifier, it will be passed to the IEngine.GetMenuCommandAsync() method.

<plist version="1.0">
<dict>
	...
	<key>NSExtension</key>
	<dict>
		...
		<key>NSExtensionFileProviderActions</key>
		<array>
			<dict>
				<key>NSExtensionFileProviderActionIdentifier</key>
				<string>28d0e0cb-5df7-432c-a06f-297c06b5ec5d</string>
				<key>NSExtensionFileProviderActionName</key>
				<string>Action 1</string>
				<key>NSExtensionFileProviderActionActivationRule</key>
				<string>TRUEPREDICATE</string>
			</dict>
		</array>
	</dict>
	<key>NSPrincipalClass</key>
	<string>NSApplication</string>
</dict>
</plist>