Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.