3

I just wanted to see if anyone more experienced with MacOS (primarily develop on Windows) can help me with a small issue I'm currently trying to resolve. Based on the comments I just want to add some quick clarity that this question is in regards to running an executable as admin/sudo in macos NOT without having to run it via command line.

So I have an application that I wrote in golang for some users, and whenever I perform a:

chmod +x ./myGolangApp
sudo ./myGolangApp

The application functions as intended. With this myGolangApp it pretty much acts as it's own installer plus does a few more things, in which it needs to be ran as sudo/elevated permissions in order to install some services. Again, this all works great simply running the commands I listed above and our users are happy with it.

The problem I'm having is that I would like to be able to distribute this application by creating a .app package and signing it with my Developer ID and then having the user be able to simply double click the .app package and having them enter in the credentials so that the install can proceed.

I've tried to create a .app package structure and then creating a Info.plist file to invoke it like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleName</key>
    <string>myApp</string>
    <key>CFBundleExecutable</key>
    <string>myApp</string>
    <key>CFBundleIdentifier</key>
    <string>com.company.myApp</string>
    <key>CFBundleShortVersionString</key>
    <string>8.6.7.9</string>
    <key>CFBundleVersion</key>
    <string>8.6.7.9</string>
    <key>CFBundleIconFile</key>
    <string>SetupIcon</string>
    <key>LSUIElement</key>
    <true/>
    <key>NSHumanReadableCopyright</key>
    <string>Copyright 2020 XXXXX</string>
</dict>
</plist>

But when I double click on the app, it does absolutely nothing at all. If I try to run the .app from the command like:sudo ./myApp.app I immediately get: sudo: ./AgentSetup.app: command not found but if I run sudo ./myApp.app/Contents/MacOS/myGolangApp, it executes as normal. I'm trying to get around using the command line for this.

I saw some suggestions on apple.stackexchange, one of which got me the closest I suppose in which someone attempted to write a small AppleScript, export it as an application and run something like:

set currentPath to POSIX path of ((path to me as text) & "::")
set installerPath to currentPath & "testscript.app/Content/Resources/Scripts/myGolangApp"
do shell script installerPath with admin privileges

And this.. Kind of works, but this keeps causing my application to throw an exception an exception for some reason, but I can't tell why and to be honest, I don't really like this hacky way of clicking a .app bundle > having it call an applet executable > which then invokes this script > which then calls the executable to be ran with administrator privileges. This seems like such an unnecessary workaround, and maybe this is partially my ignorance to how OSX - Catalina works.

Is there a way I can simply invoke the .app to call my executable with sudo?

Chris
  • 131
  • Since this is entirely in bash, do a check for root priviliges, (user id = 0). If not, then issue the command sudo -s where the script will ask for a password. Here's an example of how to do it: https://apple.stackexchange.com/a/346713/119271 – Allan Feb 08 '20 at 00:20
  • @Allan thank you for your response but excuse my initial confusion here; how would this help my current situation? The application (executable) I've wrote is in Golang. I actually haven't written anything in bash. What I'm attempting to do is essentially wrap this golang executable in an .app bundle that a user will download. I would like to be able to simply double click it and have the .app bundle bring up a prompt for admins to be able to log in. Is that possible with the example you linked (or is it possible at all)? Reading through it, it doesn't seem like it. – Chris Feb 08 '20 at 00:28
  • My bad...perhaps you could write a short script that calls the app but checks for root privileges first. As for your App bundle, I (currently) don't know how to ask for root privileges in that environment. – Allan Feb 08 '20 at 00:32
  • @Allan Not a problem, just wanted to see if we were on the same page; I appreciate the attempt to answer. I'm going to keep playing around with scripts as well and see if I can get this to work. Hopefully someone else can come and enlighten us both on that aspect. – Chris Feb 08 '20 at 00:36
  • 2
    It's a good question and I disagree with the close vote (I will vote to "Leave Open" and a +1). Though there's a dev slat to this, I think it can remain here because "running an app as an admin" is a pretty global concept. For instance, what if you didn't write the app but still needed to have it run as admin? You might want to edit your question to focus on that angle to keep it on topic. – Allan Feb 08 '20 at 00:43

0 Answers0