Add a service in Mac Finder using CLI

人盡茶涼 提交于 2019-12-08 03:26:21

问题


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)


回答1:


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

<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://stackoverflow.com/questions/18559360/add-a-service-in-mac-finder-using-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!