title: How can i run a shell script with administrator privileges within SwiftUI using osascript
body: <p>I’m trying to make a simple macOS App which runs a shell script <strong>with administrator privileges</strong> when a specific button is pressed. To run the shell script in terminal i successfully tested this command:</p>
  
  <p><em>osascript -e 'do shell script "/Users/andreas/checkertool.sh" with administrator privileges'</em></p>
  
  <p>Unfortunately i'm not able to execute this command when pressing the button. Following error message appears:</p>
  
  <p><em>0:2: execution error: The variable „do“ is not defined. (-2753)</em></p>
  
  <p>This is what my code looks like so far. Any help would be highly appreciated!</p>
  
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @State var script = "/Users/andreas/checkertool.sh"
      @State var isRunning = false
  
      var body: some View {
          VStack {
              Text("Checker Tool")
                  .font(.largeTitle)
                  .padding()
              HStack {
                  Button(action: {
                      let process = Process()
                      process.launchPath = "/usr/bin/osascript"
                      process.arguments = ["-e", "do", "shell script", "/Users/andreas/checkertool.sh", "with administrator privileges"]
                      process.launch()
                      process.waitUntilExit()
                  }) {
                      Text("Check")
                  }.disabled(isRunning)
                      .padding(.trailing)
              }
          }.frame(maxWidth: .infinity, maxHeight: .infinity)
      }
  }
  
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>