diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59ca61a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Browter.app diff --git a/Info.plist b/Info.plist new file mode 100644 index 0000000..1c8d771 --- /dev/null +++ b/Info.plist @@ -0,0 +1,49 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleExecutable + main + CFBundleIconFile + Calculator.icns + CFBundleIdentifier + com.schoonology.Browter + CFBundleInfoDictionaryVersion + 1.0 + CFBundleName + Browter + CFBundlePackageType + APPL + CFBundleShortVersionString + 0.1 + CFBundleSignature + ???? + CFBundleURLTypes + + + CFBundleTypeRole + Shell + CFBundleURLName + com.schoonology.browter + CFBundleURLSchemes + + file + http + https + + + + CFBundleVersion + 0.1 + LSHasLocalizedDisplayName + + LSMinimumSystemVersion + 10.7 + NSPrincipalClass + NSApplication + NSSupportsSuddenTermination + + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ffde7a5 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: app + +clean: + rm -rf Browter.app + +app: + mkdir -p Browter.app/Contents/MacOS + clang -lobjc -fobjc-arc -framework Foundation -framework AppKit main.m -o Browter.app/Contents/MacOS/main + cp Info.plist Browter.app/Contents/. + rm -rf Browter.app/Contents/MacOS/main.dSYM diff --git a/main.m b/main.m new file mode 100644 index 0000000..b0f19f9 --- /dev/null +++ b/main.m @@ -0,0 +1,57 @@ +#import +#import + +/** + * Top-level object to give NSAppleEventManager something to call, as it cannot + * call arbitrary functions. + */ +@interface URLHandler : NSObject {} +- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent; +@end + +@implementation URLHandler +/** + * Initializes the URLHandler, wiring up the one and only event we care about. + */ +- (id)init +{ + self = [super init]; + + if (self) { + [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self + andSelector:@selector(handleGetURLEvent:withReplyEvent:) + forEventClass:kInternetEventClass + andEventID:kAEGetURL]; + } + + return self; +} + +/** + * Event handler fired whenever any http://, https://, or file:// URL is opened + * system-wide (as defined in Info.plist). + */ +- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent +{ + NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; + NSString* app = @"Firefox"; + + NSLog(@"Opening %@ with %@...", url, app); + + [NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:@[url, @"-a", app]]; +} + +@end + +/** + * Initializes the process and creates our top-level object, which will handle + * the actual URL routing. + */ +int main(int argc, const char * argv[]) { + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToBackgroundApplication); + + URLHandler *handler = [[URLHandler alloc] init]; + + return NSApplicationMain(argc, argv); +}