1

Hi I want to write an app that includes a few buttons for scripts I use daily. I want to click a button and Terminal to open with a script (or multiple scripts) running, like:

let scripts = [
    "cd ~/Documents/Websites/someApp/",
    "ng serve --port 4300 -o"
]
performInTerminal(scripts)

I know how to... Open Terminal:

private func openTerminal(){
    let url = NSURL(fileURLWithPath: "/System/Applications/Utilities/Terminal.app", isDirectory: true) as URL
    let path = "/bin"
    let configuration = NSWorkspace.OpenConfiguration()
    configuration.arguments = [path]
    NSWorkspace.shared.openApplication(at: url, configuration: configuration, completionHandler: nil)
}

and how to ... Perform a Script:

Unfortunately this doesn't seem to work with anything other than ls and it has no connection to the terminal window, which makes sense.

private func performScript(_ args: String...){
    let process = Process()
    process.launchPath = "/usr/bin/env"
    process.arguments = args
    process.launch()
    process.waitUntilExit()
}

Any ideas on how to merge those two?

UPDATE

I now added the function using Apple Script:

private func performInTerminal(_ scripts:[String]){
    let concatScript = scripts.joined(separator: "; ")
    let source = """
    tell application "Terminal"
        if not (exists window 1) then reopen
        activate
        do script "\(concatScript)"
    end tell
    """
    let appleScript = NSAppleScript(source: source)
    var possibleError: NSDictionary?
    appleScript?.executeAndReturnError(&possibleError)
    if let error = possibleError {
        for key in error.allKeys{
            if let key = key as? String{
                print("\(key): \(error.value(forKey: key)!)")
            }
        }
        
    }
}

Problem now are Entitlements: With the following in AppName.entitlements it works, but It requests permissions every time:

<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>com.apple.Terminal</string>
</array>

With the following I get -10004: Terminal got an error: A privilege violation occurred.:

<key>com.apple.security.scripting-targets</key>
<dict>
     <key>com.apple.Terminal</key>
     <array>
          <string>*</string>
     </array>
</dict>
ferdyyy
  • 515
  • 4
  • 16
  • Possible duplicate: https://stackoverflow.com/questions/43014600/how-to-execute-terminal-command-in-swift – Sweeper Jul 03 '20 at 08:10
  • 1
    Does this answer your question? [Open a terminal window to a specified folder from a Cocoa app](https://stackoverflow.com/questions/1446814/open-a-terminal-window-to-a-specified-folder-from-a-cocoa-app) – Willeke Jul 03 '20 at 08:20
  • @Sweeper this doesn't open the Terminal app. It is "basically" the same as the performing a script from a mac app section in my question, just with an external script. – ferdyyy Jul 03 '20 at 08:30
  • @Willeke, Ahh nice Apple Script thanks for the hint. Will try – ferdyyy Jul 03 '20 at 08:31
  • @Willeke I used the post you suggested and updated my question – ferdyyy Jul 03 '20 at 09:26
  • I believe the solution is to set com.apple.security.scripting-targets properly. Did you managed to find out how? – PerfectGamesOnline.com May 03 '21 at 19:36

0 Answers0