well activate in both cases... you should deactivate. BEFORE you activate, save old active app
_oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
later activate that
[_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
--- full source
@implementation DDAppDelegate {
NSStatusItem *_item;
NSRunningApplication *_oldApp;
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);
_item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
_item.title = @"TEST";
_item.target = self;
_item.action = @selector(toggle:);
}
- (void)applicationWillBecomeActive:(NSNotification *)notification {
NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);
}
//---
- (IBAction)toggle:(id)sender {
if(!_oldApp) {
NSLog(@"%@", [[NSWorkspace sharedWorkspace] frontmostApplication].bundleIdentifier);
_oldApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
[NSApp activateIgnoringOtherApps:YES];
}
else {
[_oldApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
_oldApp = nil;
}
}
@end