objective-c - cocoa +自动化: service to generate text files not working

标签 objective-c cocoa osx-mountain-lion automator

我正在尝试使用 cocoa 创建一个自动化服务,它将简单地创建一个文本文件,其名称与所选文件的名称相同,路径相同,我为此编写了以下代码:

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo
{
    // Add your code here, returning the data to be passed to the next action.

    NSArray *fileURLs = (NSArray *)input;

    size_t count = [fileURLs count];

    dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {
        NSURL *fileURL = [fileURLs objectAtIndex:i];

        NSData *data = [@"some crude text ;)" dataUsingEncoding:NSUTF8StringEncoding]; 

        //changing extension of file
        NSString *filePathWithoutExtension = [[fileURL path] stringByDeletingPathExtension];
        NSString *filePathWithNewExtension = [[NSString alloc] initWithFormat:@"%@.%@",filePathWithoutExtension,@"txt"];

        [data writeToFile:filePathWithNewExtension atomically:NO];

    });

    // no need to return anything
    return nil;
}

我在 info.plist 文件中添加了以下值:

  • AMAccepts:类型:项目 0:com.apple.cocoa.url
  • AMCategory:AMCategory照片

我将操作导入到自动化程序中,并将其添加到默认服务模板中。

输入模板中选择的选项是:

  1. 服务接收选定的:URL
  2. 在:Finder
  3. 输入为:仅网址

我的问题是当我尝试右键单击查找器中的文件时,创建的服务没有出现。

如果我错过了什么,有人可以建议吗?

最佳答案

有时我会遇到服务未显示的问题,因为我有很多服务。尝试进入以下窗口并通过取消选中某些其他服务的复选框来禁用它们:

系统偏好设置 > 键盘 > 服务

如果您有任何想要删除(或存档)的服务,您可以执行以下操作:

  1. 右键单击服务名称
  2. 选择“在 Finder 中显示”
  3. 这将带您前往 ~/Library/Services/

您可以删除服务或将它们移动到安全的地方。

---编辑---

我一直在关注这个问题。也许,有效的方法是在 Automator 中将服务工作流程的输入指定为“文件和文件夹 > 图像文件”,而不是“文本 > URL”。

您必须在操作中调整 Objective-C 代码以使用路径名而不是 URL,但这是一个想法。

如果我的评论不起作用,您可以给我发电子邮件:kaydell@yahoo.com 我有兴趣了解有关 Automator 的更多信息。

关于objective-c - cocoa +自动化: service to generate text files not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18461385/

相关文章:

iphone - 带有图像和文本的 UIBarButtonItem

ios - 如何检索所有 NSNotificationCenter 观察者?

macos - info.plist 的 LSEnvironment 部分不起作用。

python - 实际上,在 OSX Mountain Lion 上安装了太多的 Python

ios - Web View 非法接口(interface)限定符

ios - 无法访问 UICollectionViewCell 中的 UITextField

xcode - 基于文档的 Cocoa 应用程序中的文档类型

macos - 将长行粘贴到 Mac OS X 终端

python - 操作系统 10.8 升级后的 pydev 调试器 : unable to find real location for python 2. 7

objective-c - 我可以安全地使用这个弱自自动类型的快捷方式吗?