Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

swift - How to execute command after mac app opened

I want to execute a command after my app opens the system terminal app. I open the terminal app with the following:

let url = NSURL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app", isDirectory: true) as URL
let configuration = NSWorkspace.OpenConfiguration()
NSWorkspace.shared.openApplication(at: url, configuration: configuration, completionHandler: { app, error in
    //app.executeMyCommand("echo hello")
})

And after it opened I want to execute the command "echo hello", as shown in the completionHandler. How can achieve this?

question from:https://stackoverflow.com/questions/65925082/how-to-execute-command-after-mac-app-opened

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One possibility would be to use 'AppleScriptObjc' to control the terminal via an AppleScript from Swift.

Here a complete self-contained example:

ViewController.swift

import Cocoa
import AppleScriptObjC

class ViewController: NSViewController {

    var terminalBridge: TerminalBridging?

    override func viewDidLoad() {
        super.viewDidLoad()
        Bundle.main.loadAppleScriptObjectiveCScripts()
        let terminalBridgeClass: AnyClass = NSClassFromString("TerminalBridge")!
        self.terminalBridge = terminalBridgeClass.alloc() as? TerminalBridging
    }

    @IBAction func onExecute(_ sender: Any) {
        self.terminalBridge?.executeCommand("echo hello")
    }
    
}

TerminalBriding.swift

import Cocoa

@objc(NSObject) protocol  TerminalBridging {

    func executeCommand(_ cmd: String)
    
}

TerminalBridge.applescript

Note the suffix .applescript is important. Customize it to your preferences.

script TerminalBridge

    property parent : class "NSObject"

    to executeCommand:cmd
        tell application "Terminal"
            do script (cmd as text)
        end tell
    end executeCommand:

end script

Setup Project in Xcode

  • in XCode select the root node in the project navigator
  • then select the target and under 'Frameworks, Libraries, and Embedded Content' add 'AppleScriptObjC.framework' with defult 'Do Not Embed' setting
  • under 'Signing & Capabilities' remove 'App Sandbox' and add 'Hardened Runtime' instead
  • scroll down and under 'Resource Access' choose 'Apple Events'
  • in project navigator select 'Info.plist'
  • add 'NSAppleEventsUsageDescription' key with some useful explaination

Demo

On first run you would see:

Demo

If you press 'OK' an entry is added automatically to 'System Preferences/Security & Privacy/Privacy' on first run. Afterward the command is executed.

Terminal Screen Shot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...