开发者

Is it possible to hide the dock icon programmatically

Is it possible to hide dock icon pro开发者_运维技巧grammatically on demand. I know one way by which defining property "Application is agent (UIElement)" in plist we make the cocoa app as user agent. But this result in hiding the dock icon permanently.

I am looking for a way where i can control visibility of dock icon. Any idea ?


Unfortunately not. You can transform a background-only app to a foreground app using the TransformProcessType() function but you can't go from a foreground app to a background app.

Here's how to go from background to foreground:

ProcessSerialNumber psn = { 0, kCurrentProcess }; 
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if( returnCode != 0) {
    NSLog(@"Could not bring the application to front. Error %d", returnCode);
}


It is possible. However, it also hides the menu.

NSApplication.shared.setActivationPolicy(.accessory)
DispatchQueue.main.async {
    NSApplication.shared.activate(ignoringOtherApps: true)
    NSApplication.shared.windows.first!.makeKeyAndOrderFront(self)
}

And back:

NSApplication.shared.setActivationPolicy(.regular)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜