objective-ccocoaosx-mountain-lionautomator

Cocoa + Automator : service to generate text files not working


I am trying to create an automator service using cocoa, which will simply create a text file with the name of files selected, at the same path, for which I wrote below code:

- (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;
}

I added below values in info.plist file:

  • AMAccepts: Types: Item 0: com.apple.cocoa.url
  • AMCategory:AMCategoryPhotos

I imported the action to the automator, and added it to the default service template.

The options selected in input template are:

  1. Service receives selected: URLs
  2. in: Finder
  3. Input is: only URLs

My problem is the service created is not appearing when I am trying to right click a file in finder.

Can anyone suggest if I missed anything?


Solution

  • Sometimes I have the problem of my Services not showing up because I have so many of them. Try going into the following window and disabling some of the other Services by unchecking their checkboxes:

    System Preferences > Keyboard > Services

    If you have any Services that you want to delete (or archive), you can do the following:

    1. Right-click on the name of the Service
    2. Choose "Show in Finder"
    3. This will take you to ~/Library/Services/

    You can either delete the Services or move them somewhere for safe-keeping.

    ---Edit---

    I've been looking at this some more. Maybe, what would work is to specify the input of the workflow for the Service, in Automator, to be "Files and Folders > Image Files" instead of being "Text > URLs".

    You would have to adjust your Objective-C code in your action to use path names instead of using URLs, but this is an idea.