使用CLI在Mac Finder中添加服务 [英] Add a service in Mac Finder using CLI

查看:174
本文介绍了使用CLI在Mac Finder中添加服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了Python脚本,并希望将其添加为Mac Finder中的服务项. 我知道我可以打开Automator窗口并进行设置. 但是我需要一种命令行方式.我搜索该方法很长时间后仍然一无所获...

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...

有人知道如何实现这一目标吗?非常感谢. (我也可以使用python,apple脚本或任何内置的CLI工具)

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

推荐答案

在这里接受我的评论是一个非常基本的示例.

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

这将向您展示服务的注册方式,甚至包括Automator的注册方式. 以及为什么我认为您可能无法做您想做的事.

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.

使用Xcode我创建了一个新项目.

Using Xcode I have created a new project.

.m文件中的代码:

    //
//  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

请注意:

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

然后我将数组添加到应用程序info-plist

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>菜单

<string>runAService</string>方法在应用程序中运行

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

<string>public.item</string>对象

该应用程序的首次运行(双击将在系统中注册该服务.但是我确保已在键盘快捷方式中选中了该服务.

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.

NSLog的结果可以在console.app中看到,它将列出文件路径

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

因此,即使我可以使用默认值之类的方式添加项目,也要写入plist文件中,该文件将在系统prefs服务中保存已注册的应用程序.我怀疑如果没有上述任何条件,任何东西都将真正起作用.

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.

很明显,Automator应用程序可以将正确的信息写入plist文件和 runAservice 方法.

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

服务工作流名称中的实际方法是: runWorkflowAsService .

The actual method in a service workflows name is: runWorkflowAsService.

关于它的思考.您可以从命令行编写和构建应用程序.并让它运行在python上,但这很麻烦.

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.

有关服务的更多信息,请参阅Apple文档

For more info on services see the apple docs here

这篇关于使用CLI在Mac Finder中添加服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆