文本用Xcode替换服务 - 不替换选定的文本 [英] Text Replace Service with Xcode - Not replacing selected text

查看:240
本文介绍了文本用Xcode替换服务 - 不替换选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个独立的系统服务(带有.service扩展名的应用程序,保存到〜/ Library / Services /),以替换Mac OS X中用户选择的文本。

I am trying to build a standalone system service (app with .service extension, saved to ~/Library/Services/) to replace user-selected text in Mac OS X.

我想用Xcode和而不是构建它。因为我比Applescript更熟悉Objective-C。

I want to build it with Xcode and not Automator, because I am more accustomed to Objective-C than Applescript.

在互联网上找到几个例子,例如以及 Apple的文档。我得到了Xcode项目适当配置和构建没有问题。

I found several examples on the internet, e.g. this and also Apple's documentation. I got the Xcode project appropriately configured and building without problems. However, when I install my service and try to use it, nothing happens.

服务方法本身被执行:我放置代码以在其方法体中显示NSAlert,并且表明。但是,选择的文本不会被替换。

The service method itself is executed: I placed code to show an NSAlert inside its method body and it shows. However, the selected text does not get replaced.

任何想法可能缺少什么?这是实现服务的方法:

Any idea what might be missing? This is the method that implements the service:

- (void) fixPath:(NSPasteboard*) pboard
        userData:(NSString*) userData
           error:(NSString**) error
{
    // Make sure the pasteboard contains a string.
    if (![pboard canReadObjectForClasses:@[[NSString class]] options:@{}])
    {
        *error = NSLocalizedString(@"Error: the pasteboard doesn't contain a string.", nil);
        return;
    }

    NSString* pasteboardString = [pboard stringForType:NSPasteboardTypeString];


    //NSAlert* alert = [[NSAlert alloc] init];
    //[alert setMessageText:@"WORKING!"];
    //[alert runModal];

    // ^ This alert is displayed when selecting the service in the context menu


    pasteboardString = @"NEW TEXT";

    NSArray* types = [NSArray arrayWithObject:NSStringPboardType];

    [pboard clearContents];
    [pboard declareTypes:types owner:nil];

    // Set new text:
    [pboard writeObjects:[NSArray arrayWithObject:pasteboardString]];

    // Alternatively:
    [pboard setString:pasteboardString forType:NSStringPboardType];

    // (neither works)

    return;
}


推荐答案

仔细阅读 Apple's文档,我发现答案:我的服务应用程序的plist文件在服务部分下缺少一个键:

After careful reading of Apple's documentation, I found the answer: My service app's plist file was missing a key under the Services section:

<key>NSReturnTypes</key>
<array>
    <string>NSStringPboardType</string>
</array>

我只有相反的 NSSendTypes 它允许您将数据从客户端应用程序发送到服务。这一个需要发送修改的文本(在另一个方向)。

I only had the opposite NSSendTypes key, which lets you send data from the client app to the service. This one is needed to send the modified text back (in the other direction).

这很奇怪,因为苹果的文档似乎暗示指定这两个不再需要从10.6(雪豹)。

It is weird because, Apple's documentation seems to imply that specifying these two is no longer necessary since 10.6 (Snow Leopard).

这篇关于文本用Xcode替换服务 - 不替换选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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