Skip to content

Instantly share code, notes, and snippets.

@markd2
Created July 2, 2014 19:33
Show Gist options
  • Save markd2/ff362c20463cd4ecc5fa to your computer and use it in GitHub Desktop.
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.
@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