How can I run macOS terminal commands from Swift?

ios coder

I want be able to run this code through Swift for Terminal in macOS:

cd ~/Desktop/
mkdir "New Folder"

Basically I want to go to level of desktop then create a new Folder there. I want press a button in Swift/SwiftUI to run a function to execute the code for Terminal. Using Swift/SwiftUI is just for UI. I do not have shell file saved some where, all my codes is that I want be able to run those 2 commands for Terminal from Swift.

func runBashCode() {

    // need help here
    cd ~/Desktop/
    mkdir "New Folder"

}
workingdog

try something like this:

Note: you need to delete the "App Sandbox" in the "Signing & Capabilities"

struct ContentView: View {
    let arg = NSHomeDirectory() + "/Desktop/newfolder"
    
    var body: some View {
        Button("mkdir") {
            runMkdir()
        }.frame(width: 333, height: 333)
    }
    
    func runMkdir() {
        let task = Process()
        task.executableURL = URL(fileURLWithPath: "/bin/mkdir")
        task.arguments = [arg]
        task.launch()
    }
    
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I execute terminal commands from a C program?

How To Run Mac OS Terminal Commands From Java (Using Runtime?)

How can I use swift in Terminal?

How can I run Command Line commands or tasks with Swift in iOS?

Can I run two ongoing npm commands in 1 terminal

How to run commands you can run on terminal in Java

How can I save some terminal commands as a file and run them?

How can I check my favorated commands from the terminal?

How do I run NetBeans from the terminal?

How can I get help on terminal commands?

How to run commands from a bash file the way they run from terminal?

How can I run a command from the terminal without blocking it?

How can I run commands in bash script?

I'd like to run a few terminal commands from inside vim

How can I run a process in one terminal from another terminal

How can I run python in terminal on a mac?

How can I run two commands in succession from cron?

Run commands in tmux from terminal

How can I run a terminal command in my swift menu bar app?

How do I run multiple terminal commands in Visual Studio Code?

How can I run terminal commands on my Ubuntu host from host Mac?

How do I run a set of Terminal commands from a spreadsheet?

How to run Applications from the terminal by only typing their name in macOS?

How can i run a linux executable from any directory in terminal?

How can I run commands in batches?

how can I use all scripts from custom folder like native shell commands in macOS

How can I run files on external terminal?

How can I remove all commands in macOS?

How can I permanently remove an added alias from zsh (MacOS Terminal)?