使用CLI在Mac Finder中添加服务

时间:2013-09-01 14:27:08

标签: macos finder automator

我编写了一个Python脚本,我希望将其添加为Mac Finder中的服务项。 我知道我可以打开Automator窗口并进行设置。 但我需要一个命令行方式。我搜索了很长时间的方法,但仍然没有得到任何结果......

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

1 个答案:

答案 0 :(得分:0)

这里的评论是一个非常基本的例子。

这是向您展示如何注册服务,甚至是Automator服务。 为什么我认为你可能无法做你想做的事。

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

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

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

enter image description here

<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>public.item</string>个对象

第一次运行应用程序(双击将服务注册到系统。但我确保在键盘快捷方式中检查它。

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


所以,即使我可以使用类似默认值的内容添加项目,也可以将plist文件写入系统prefs服务中的注册应用程序。如果没有上述任何内容,我怀疑任何事情都会有效。

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

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

考虑一下。您可以从命令行编写和构建应用程序。并让它运行你python但是要去很多麻烦。

有关服务的更多信息,请参阅apple docs here

相关问题