How to launch application and bring it to front using Cocoa api?

后端 未结 7 1092
滥情空心
滥情空心 2020-11-29 07:27

I\'m very new to a cocoa programming and I can\'t find the way to do the following:

  • Start a particular application by name
  • Do some work
  • Later
7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 08:18

    In swift 4, you can use NSWorkspace.shared.launchApplication(appName:) to open an app. It also makes the launched app at front in my case.

    You also can try:

    do {
        try NSWorkspace.shared.launchApplication(at: yourAppURL,
                                                 options: .andHideOthers,
                                                 configuration: [:])
    } catch {
        printError("Failed to launch the app.")
    }
    

    Option andHideOthers: Hide all apps except the newly launched one.

提交回复
热议问题