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

后端 未结 7 1087
滥情空心
滥情空心 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:26

    For Swift2 version

    Launch App:

    let task = NSTask()
    task.launchPath = "/usr/bin/env"
    task.arguments = ["/Applications/TextEdit.app/Contents/MacOS/TextEdit"]
    task.launch()
    

    To get the app using bundle Identifier:

        var apps: [AnyObject] = NSRunningApplication.runningApplicationsWithBundleIdentifier("com.apple.TextEdit")
        var MyApp: [NSRunningApplication] = apps as! [NSRunningApplication]        
                for app in MyApp        
                {
                }
    

    I am still trying to find the way to know "active", "hide" etc state of app, But not succeed till now.

提交回复
热议问题