0

I'm writing a macOS Cocoa app using Swift that allows a user to build macOS installation media using a macOS installation .app package. It works by executing the createinstallmedia binary embedded in the .app package. I'm pretty new to macOS development in general (coming from C# and Python) and have no clue how to achieve this while capturing and reporting status & percentage to the user.

Here's some information that might be beneficial:

  • Using macOS 10.15.7 as dev environment
  • Programming under Xcode 10.3 (for compatibility reasons)
  • Output app is NOT sandboxed

The command needs to be run with administrator privileges, so I can't just create a Process() instance, call the command and expect it to work. I need macOS to prompt the user for their password so that the commands to write the installer can be run. The command to be run is formed like this:

let volumePrefix = "/Volumes/"

// Generate the command to run
let source = selectedInstaller // From path selector; in UI
let destination = volumePrefix + targetDevicePopupButton.titleOfSelectedItem!

// Make path bash-friendly
let sourcePath = source.replacingOccurrences(of: " ", with: "\\\\ ")
let destinationPath = destination.replacingOccurrences(of: " ", with: "\\\\ ")

let toolexec = sourcePath + createinstallmedia_executablePath + " --volume " + destinationPath + " --nointeraction"

TL;DR:

Basically, the question boils down to this: How can I run commands with admin privileges, and capture output/progress from them? (Without using osascript; I've been having output problems with that). Is there a way to show the default macOS authentication dialog with a custom prompt?

I suck at asking questions, and I really hope this doesn't get poorly received. If I wrote/did something wrong, please inform me, because this is new territory for me.

Joseph .M 101
  • 211
  • 1
  • 6
  • 1
    https://stackoverflow.com/questions/42493290/launch-sudo-command-from-macos-app-swift ? – Larme Apr 25 '22 at 12:36
  • That's one of the topics I had already come across, but it did not work for my purposes. For this, I can't run the process through AppleScript because any output from the process gets suppressed until it exits. – Joseph .M 101 Apr 25 '22 at 13:23
  • One answer use Apple script, but the other run command bash/terminal... – Larme Apr 25 '22 at 13:26
  • I did not see that... I will look into it. Thank you for bringing that to my attention. What I would also like to do is show the macOS authentication dialog with a custom prompt/purpose shown to the user, as I've seen other apps do. – Joseph .M 101 Apr 25 '22 at 13:37
  • Interesting discussion from a guy with awesome apps at https://eclecticlight.co/2016/12/22/more-fun-scripting-with-swift-and-xcode-cheating-with-applescript/ – matt Apr 25 '22 at 13:53
  • I now have a new problem: The command runs. However, the output gets stuck at "Erasing Disk:...100%", and does not produce output until the command is done, where it just prints everything. This output is visible when running the command in the terminal, but the Process() command cannot capture it. – Joseph .M 101 Apr 25 '22 at 16:28
  • @Joseph.M101 It seems your original question is solved? If so, either answer it or delete it. Don't leech in the comments. – matt Apr 26 '22 at 03:06

0 Answers0