Here is my solution, which is triggered on checkbox click in Preferences window. The only side effect is short flashing of the app's windows:
- (void) hideDockIcon:(bool) hide
{
ProcessSerialNumber psn = { 0, kCurrentProcess };
ProcessApplicationTransformState state = hide ? kProcessTransformToUIElementApplication : kProcessTransformToForegroundApplication;
OSStatus err = TransformProcessType(&psn, state);
if (err) {
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
NSLog(@"TransformProcessType failed: %@", error);
}
}
- (void) toggleDockIcon
{
[NSApp hide:self];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hideDockIcon:self.preferences.hideDockIcon];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[NSApp activateIgnoringOtherApps:YES];
});
});
}