Created
July 2, 2014 19:33
-
-
Save markd2/ff362c20463cd4ecc5fa to your computer and use it in GitHub Desktop.
Simple utility that uses Launch Services to see what applications would be interested in editing a given file.
This file contains 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; | |
@import CoreServices; | |
// clang -g -fobjc-arc -fmodules launchHandler.m -o launchHandler | |
int main (int argc, const char *argv[]) { | |
// Rudimentary argument checking. | |
if (argc != 2) { | |
printf ("usage: %s filename\n", argv[0]); | |
return -1; | |
} | |
const char *filename = argv[1]; | |
// Get a string of the full path of the file, using realpath() as the workhorse | |
char pathbuffer[MAXPATHLEN]; | |
char *fullpath = realpath (filename, pathbuffer); | |
if (fullpath == NULL) { | |
fprintf (stderr, "could not find %s\n", filename); | |
return -1; | |
} | |
NSURL *url = [NSURL fileURLWithPath: @( fullpath )]; | |
// Ask launch services for the different apps that it thinks could edit this file. | |
// This is usually a more useful list than what can view the file. | |
LSRolesMask roles = kLSRolesEditor; | |
CFArrayRef urls = LSCopyApplicationURLsForURL((__bridge CFURLRef)url, roles); | |
NSArray *appUrls = CFBridgingRelease(urls); | |
// Extract the app names and sort them for prettiness. | |
NSMutableArray *appNames = [NSMutableArray arrayWithCapacity: appUrls.count]; | |
for (NSURL *url in appUrls) { | |
[appNames addObject: url.lastPathComponent]; | |
} | |
[appNames sortUsingSelector: @selector(compare:)]; | |
// Finally emit to the user. | |
for (NSString *appName in appNames) { | |
printf ("%s\n", appName.UTF8String); | |
} | |
return 0; | |
} // main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment