Cocoa + Automator : service to generate text files not working

10 views
Skip to first unread message

Decoder

unread,
Aug 27, 2013, 5:03:54 AM8/27/13
to cocoa-...@googlegroups.com
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?
Reply all
Reply to author
Forward
0 new messages