5

I have developed a Mac OS app. There is a main window app and a menubar helper app in the same bundle.

There is a menu item of helper app called "Show main app". The helper app should launch the main app if it is not launched after i click the menu item. And the helper app should bring the main app to front if it is hidden in dock or its main window is closed.

I know how to launch the main app. But I have no idea how to implement the re-active function. I used the code like below.

let apps = NSRunningApplication.runningApplications(withBundleIdentifier: "com.xxx.yyy")
if let mainApp = apps.first {
       mainApp.activate(options: [ .activateIgnoringOtherApps ])
}

It seems activate method does nothing when the main app is just launched and hide in dock. I noticed the main app is actually activated, because the main menu bar has changed to main app's main menu. But the "applicationShouldHandleReopen" method of its AppDelegate class is not called. So the main window can't be ordered front.

How can I make it works?

morphinewan
  • 434
  • 2
  • 6
  • 17

1 Answers1

0

No matter the target application is launched or not, just call

NSWorkspace.shared().launchApplication("xxxx")

It works.

morphinewan
  • 434
  • 2
  • 6
  • 17
  • 1
    why the downvote and no comment? is anyone else able to clarify here? doesn't really help the community when we downvote something that "should" work.. – Shaun Wilson Apr 11 '17 at 17:22
  • 1
    @ShaunWilson I agree – morphinewan Apr 12 '17 at 00:52
  • 1
    after some testing this did not bring the target app to front if the app is already running and there is a window occluding the app. part of our solution was to pend this logic until after any occluding window was removed. we needed this so that another app could bring its own panel to the top of the z-order (by virtue of its main window being at the top, and also 'active'.) Part of our final solution included the use of `activateIgnoringOtherApps` from [SO#2333078](http://stackoverflow.com/questions/2333078/how-to-launch-application-and-bring-it-to-front-using-cocoa-api?rq=1) – Shaun Wilson Apr 14 '17 at 22:04