Learn Courses My Dashboard

How to access hosts file through macOS app?

Hi all,
I currently have this code where i run /usr/bin/say in the terminal, but I stead of doing that, I want to open the sudo nano /private/etc/hosts file and then edit it.
When I change the executableURL variable, to the one that is commented right now, I get this error

Any ideas on how to edit the hosts file through a macOS app?

Button(action: {
          let executableURL = URL(fileURLWithPath: "/usr/bin/say")
    //    let executableURL = URL(fileURLWithPath: "sudo nano /private/etc/hosts")
    
           self.isRunning = true
           do {
               try Process.run(executableURL,
                               arguments: ["Hello World"],
                               terminationHandler: { _ in self.isRunning = false })
               } catch{
                     print(error)
               }
                    
            }) {
                Text("Say")
            }

Overall this forum and Chris’ tutorials are more dedicated towards iOS and Swift, I’m not sure how many others will be able to help.

You may have better luck in a different forum. Best of luck!!

Hacking with Swift has a MacOS section in their group.

i don’t think you can use “URL” to execute a command like sudo nano

maybe try searching up on how to execute command on mac os programmatically using swift