I have a statusbar application, which runs in the menu bar. Therefore I set Application is agent (UIElement) to true in the info.plst.
Based on the fix (or workaround) from OP. The key is really to toggle out and back to focus (thanks @Daniel!).
Some small improvements:
No force unwrapping, no need to set ActivationPolicy and call activate twice.
NSApp.setActivationPolicy(.regular)
window.makeKeyAndOrderFront(nil)
NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate(options: [])
OperationQueue.current?.underlyingQueue?.asyncAfter(deadline: .now() + .milliseconds(200), execute: {
NSApp.activate(ignoringOtherApps: true)
})