Created
November 12, 2013 02:19
-
-
Save ku/7424364 to your computer and use it in GitHub Desktop.
OSX10.9 tag manipulation util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#include <ctype.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
// clang tag.m -o metatag -framework "foundation" -fobjc-arc && ./metatag | |
int main(int argc, char **argv) { | |
char *tagToAdd = NULL; | |
char *tagToRemove = NULL; | |
int c; | |
while ((c = getopt (argc, argv, "a:r:")) != -1) { | |
switch (c) | |
{ | |
case 'a': | |
tagToAdd = optarg; | |
break; | |
case 'r': | |
tagToRemove = optarg; | |
break; | |
case '?': | |
fprintf (stderr, | |
"Unknown option character `\\x%x'.\n", | |
optopt); | |
return 1; | |
default: | |
abort(); | |
} | |
} | |
NSError *error; | |
NSMutableArray *files = [NSMutableArray array]; | |
for (int index = optind; index < argc; index++) { | |
NSString *path = [NSString stringWithCString:argv[index] encoding:NSUTF8StringEncoding]; | |
NSURL *url = [[NSURL fileURLWithPath:path] absoluteURL]; | |
if (tagToAdd) { | |
NSString *tag = [NSString stringWithCString:tagToAdd encoding:NSUTF8StringEncoding]; | |
id tags; | |
[url getResourceValue:&tags forKey:NSURLTagNamesKey error:&error]; | |
if (tags) { | |
tags = [tags mutableCopy]; | |
[tags addObject:tag]; | |
} else { | |
tags = @[tag]; | |
} | |
[url setResourceValue:tags forKey:NSURLTagNamesKey error:&error]; | |
} | |
if (tagToRemove) { | |
NSString *tag = [NSString stringWithCString:tagToRemove encoding:NSUTF8StringEncoding]; | |
id tags; | |
[url getResourceValue:&tags forKey:NSURLTagNamesKey error:&error]; | |
if (tags) { | |
tags = [tags mutableCopy]; | |
tags = [tags objectsAtIndexes:[tags indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { | |
return ([tag isEqualToString:obj] == NO); | |
}]]; | |
[url setResourceValue:tags forKey:NSURLTagNamesKey error:&error]; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment