Skip to content

Instantly share code, notes, and snippets.

@kch
Created January 28, 2011 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kch/799722 to your computer and use it in GitHub Desktop.
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.
// ## 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;
}
@kch
Copy link
Author

kch commented Jan 28, 2011

Sample run:

$ ./target_browser 
/Applications/WebKit.app

@kch
Copy link
Author

kch commented Jan 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment