Created
January 28, 2011 02:32
-
-
Save kch/799722 to your computer and use it in GitHub Desktop.
Get the path to the frontmost app if it's Safari or Webkit, fallsback to default browser.
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
// ## compile this with: | |
// clang \ | |
// -framework Foundation \ | |
// -framework AppKit \ | |
// -framework ApplicationServices \ | |
// -fobjc-gc-only \ | |
// -o target_browser \ | |
// target_browser.m | |
#import <Foundation/Foundation.h> | |
#import <AppKit/AppKit.h> | |
#import <ApplicationServices/ApplicationServices.h> | |
int main (int argc, const char * argv[]) { | |
NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; | |
NSDictionary *frontmostApplication = [workspace activeApplication]; | |
NSString *appName = [frontmostApplication objectForKey:@"NSApplicationName"]; | |
NSString *browserBundleId = [appName isEqualToString:@"WebKit"] || [appName isEqualToString:@"Safari"] ? | |
[frontmostApplication objectForKey:@"NSApplicationBundleIdentifier"] : | |
(NSString*) LSCopyDefaultHandlerForURLScheme((CFStringRef) @"http"); | |
printf("%s\n", [[workspace absolutePathForAppBundleWithIdentifier:browserBundleId] UTF8String]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample run: