Add a service in Mac Finder using CLI
Question: I write a Python script and I want to add it as a service item in Mac Finder.
I know I can open `Automator` window and set it. But I need a command line
way. I search the method for a long time and still get nothing...

Do anyone know how to achieve this? Thanks a lot. (Using python, apple script
or any builtin CLI tool is also acceptable to me)


Answer: To go with my comment here is a very basic example.

Which is to show you how services are registered, even the Automator ones. And
why I think you may not be able to do what you want.

Using Xcode I have created a new project.

Code in the .m file:

    
    
        //
    //  AppDelegate.m
    //  testServiceExample
    //
    //  Created by Mark Hunte on 02/09/2013.
    //  Copyright (c) 2013 Mark Hunte. All rights reserved.
    //
    
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        [NSApp setServicesProvider:self];//Registers a given object as the service provider.
        NSUpdateDynamicServices();//Causes the services information for the system to be updated.
    }
    
    
    
    -(void) runAService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
    
        if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
            NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
    
    
             NSLog(@" files %@", files);
    
        }
    
        [[NSApplication sharedApplication] terminate:nil];
        }
    
    @end
    

Note the :

    
    
    [NSApp setServicesProvider:self];//Registers a given object as the service provider.
            NSUpdateDynamicServices();//Causes the services information for the system to be updated.
    

Then I added the Array to the applications info-plist

![enter image description here](http://i.stack.imgur.com/IxsXO.png)

    
    
    <array>
        <dict>
            <key>NSMenuItem</key>
            <dict>
                <key>default</key>
                <string>Service Test</string>
            </dict>
            <key>NSMessage</key>
            <string>runAService</string>
            <key>NSSendTypes</key>
            <array>
                <string>public.item</string>
            </array>
        </dict>
    </array>
    

`<string>Service Test</string>` menu displayed

`<string>runAService</string>` method to run in the application

`<string>public.item</string>` objects that the service will work on

The first run of the app (double clicking will register the service to the
system. But I make sure it is checked in the keyboard shortcuts.

The result from the NSLog can be seen in the console.app which will list the
files paths

* * *

So even if I could add a item using something like defaults write to the plist
file that holds the registered applications in the system prefs services. I
doubt anything would actually work without any of the above.

Obviously the Automator application can write the correct info to a the plist
file and the **runAservice** method.

The actual method in a service workflows name is: **_runWorkflowAsService_**.

Thinking about it. You can write and build apps from the command line. And
have it run you python But it is a lot of trouble to go to.

For more info on services see the apple docs
[here](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/SysServices/introduction.html#//apple_ref/doc/uid/10000101-SW1)

