Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created November 10, 2015 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccgus/91c9e7cad695ccdd0626 to your computer and use it in GitHub Desktop.
Save ccgus/91c9e7cad695ccdd0626 to your computer and use it in GitHub Desktop.
#import <Carbon/Carbon.h>
static OSStatus handleAppFrontSwitched(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);
EventHandlerRef MySkankyGlobalFrontAppSwitchedHandlerRef;
@implementation JSTWhatever
+ (void)load {
EventTypeSpec spec = { kEventClassApplication, kEventAppFrontSwitched };
OSStatus err = InstallApplicationEventHandler(NewEventHandlerUPP(handleAppFrontSwitched), 1, &spec, nil, &MySkankyGlobalFrontAppSwitchedHandlerRef);
if (err) {
NSLog(@"Error looking for front app.");
}
}
@end
static OSStatus handleAppFrontSwitched(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) {
if ((GetCurrentKeyModifiers() & shiftKey) != 0) {
return 0;
}
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSDictionary *activeAppDict = [workspace activeApplication];
ProcessSerialNumber psn;
psn.highLongOfPSN = [[activeAppDict objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
psn.lowLongOfPSN = [[activeAppDict objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
SetFrontProcess( &psn );
// this .. causes a bit o' recursion for some reason.
//ProcessSerialNumber xpsn = { 0, kCurrentProcess };
//SetFrontProcess( & xpsn );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment