No internet connection
  1. Home
  2. How to

How to execute commandLine with administrative privileges

By Marco Bernardo @mbernardo2020-09-08 02:48:14.411Z2020-09-08 05:36:15.905Z

Desired Workflow

I'm trying to find a way for the username and password window appear before execute the command line script!

Question

In Applscript we can execute a bash script with administrative privileges.
This means that the default window asking for the username and password appear before execute the script.

I don't know if this make any sense for you. I've tried with osascript but I didn't figure out the right sentence

Here is a video showing what I have but I want to add it to soundflow instead.

check at 37 second

https://www.dropbox.com/s/b17993kq8qu8dz3/Pro_Tools_Installer_2020.R2.0x117-AppInstall.mp4?dl=0

In this case I or we (users) already have the source and destination created, there's no need to choose them.

Thanks

Command Info

ID: user:-LEzGPe-OhWscuIf2MRu:cketc1vhx00029l10qcs30xn1

Source


//change the var to your path
var pythonScriptPath = "/Users/marco.bernardo/Documents/Scripts/Python/InstPTwVbeta_v07.py "
var sourceTarPath = "/Users/marco.bernardo/Downloads/ "
var destinationDMGPath = "/Users/marco.bernardo/Desktop/"

var executePythonScript = "python " + pythonScriptPath + sourceTarPath + destinationDMGPath

//log(executePythonScript)

//I need to find a way to execute python with administrator privileges (sudo) it needs to pop up the window asking for the user and password admin like Applescript can do
sf.system.exec({
    commandLine: "python " + pythonScriptPath + sourceTarPath + destinationDMGPath
    //commandLine: "osascript -e \"do shell script \\\"python " + pythonScriptPath + sourceTarPath + destinationDMGPath" & " -e \"with administrator privileges"
});



Links

User UID: zeVjM6FHntfwFzm79GRGlipkNSB3

Feedback Key: sffeedback:zeVjM6FHntfwFzm79GRGlipkNSB3:-MGfbGfNnOxBIOuBQHLQ

Feedback ZIP

  • 18 replies
  1. The easiest thing is probably to use this:

    sf.system.execAppleScript({ script: `do shell script "python ${pythonScriptPath} ${sourceTarPath} ${destinationDMGPath}" with administrator privileges` });
    
    1. Way more clean and PRO!! WoW

      It didn't work.
      I've open script editor and paste this

      SCRIPT EDITOR
      do shell script "python /Users/marco.bernardo/Documents/Scripts/Python/InstPTwVbeta_v07.py /Users/marco.bernardo/Downloads/ /Users/marco.bernardo/Desktop/" with administrator privileges
      and I got this.

      error "/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/marco.bernardo/Documents/Scripts/Python/InstPTwVbeta_v07.py': [Errno 1] Operation not permitted" number 2
      IF I use the same command directly in the Terminal it works.

      TERMINAL
      sudo python /Users/marco.bernardo/Documents/Scripts/Python/InstPTwVbeta_v07.py /Users/marco.bernardo/Downloads/ /Users/marco.bernardo/Desktop/

      I already check the path of my Python 2.7 and all seems correct!
      I open the terminal and paste Python and is opening the correct version!

      `Python 2.7.15 (default, Jan 12 2019, 21:43:48)
      [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
      Type "help", "copyright", "credits" or "license" for more information.

      `

      Weird...

      Thank you Christian

      1. Hm yea that's weird. You could try saving the python call into a shell script (and save as a .sh file) and then run that instead.

        1. Oh no I see the issue. SoundFlow will normally make shell script execution work flawlessly so if it works in Terminal it'll work in our shell script execution --- but, here you're running the shell script via Apple Script and the Apple Script interpretation probably doesn't set up the proper environment variables and/or permissions for python to execute correctly.

          Try instead writing a small shell script and place it in the same folder as your python file, and call it for example InstPTwVbeta_v07.sh. Make sure to run chmod +x InstPTwVbeta_v07.sh. The script should just be the call to your python interpreter - make sure to pass the full path instead of using just python.
          Try running that shell script with sudo. Once you have everything working, then try running all that from osascript / SF's execAppleScript.

          1. What's the reason for running this via Python in the first place, by the way?
            Python is known to be extremely tricky to get working reliably across different machines - so maybe there's a more stable alternative possible :)

            1. Ah wait. I think you still need the sudo in there:

              sf.system.execAppleScript({ script: `do shell script "sudo python ${pythonScriptPath} ${sourceTarPath} ${destinationDMGPath}" with administrator privileges` });
              
              1. Yeah it's very weird.

                This is an Applescript app that I created a long time ago that executes a Python script with admin privileges and now I'm trying to use it with Soundflow without the hassle to convert it to SF pure code (when I start that I will need help for sure!!) hehe

                It could be some security "issue" with macOS Catalina, I need to test this with macOS Mojave too but I think I will get the same issue.

                My py is already with chmod +x (as I said above it works if I run in the terminal).

                The issue is to execute the py with admin privileges (pop up window)

                The sudo before the python didn't work too, I already tested before ;)

                Here is a video showing what the Applescript and python is doing

                I will send the link in private.

                about Python I didn't use anything in the code besides what the 2.7 have by default and it's the default in all macOS since I can't remember.

                But yes, if I want to use this with SF the best is to convert the py into SF

                1. Hm yea then I think the issue is probably that the Apple Script runs as an actual super user..
                  Have you tried chmod a+x on the file to make sure the group+other permission bits are set for execute?

                  1. I didn't thought on that but It didn't work too.

                    1. Hm yea.. Then it's something going on with the Apple Script & the python environment not liking each other.

                      If you can get an osascript call to work in Terminal with the privileges dialog, then SF can easily just wrap that for you. So the real issue is coming up with the proper Apple Script -> Python call (outside of my SF expertise).

                      1. I just arrive at home where I have macOS Mojave the script works as it should.
                        Something in macOS have changed!! Argh....

                        1. Marco Bernardo @mbernardo2020-09-08 18:47:29.940Z2020-09-08 19:26:16.787Z

                          Question:

                          What is the best way to get a variable of the home user of macOS?

                          I don't know how to do it with javascript! :/

                          I did search for home user and I didn't find it in the forum. I need to check again!!

                          Forget it!! duhhhhh

                          ~/Desktop
                          Where I got the head !!

                          1. Yea you can use tilde in all SF paths. If you need the USER or any other environment variable, you can use this:

                            var username = sf.system.exec({ commandLine: 'echo $USER' }).result;
                            
                            1. Found the "issue"!!

                              Is macOS Catalina f#"$#%%$& SIP
                              I disable SIP and all it's working.
                              Gonna enable it again to see if it continue to work.

                              1. Nah If I enable SIP again the script stops working in macOS Catalina.
                                I already add almost I can remember into the Security & Privacy - Full disk Access.

                                1. If I use it on a terminal window it work (macOS Catalina)

                                  sudo python /Users/marco.bernardo/Documents/Scripts/Python/InstPTwVbeta_v07.py /Users/marco.bernardo/Downloads/ /Users/marco.bernardo/Desktop/

                                  The "issue" is really the SIP in macOS Catalina.

                                  1. :/

                                    1. I need to create a macro for the macOS Catalina and beyond that opens the terminal paste the code line and execute then we enter the password in the terminal instead of the pop window that is standard in all the apps.

                                      For macOS Mojave the script works ok.

                                      and that's all "folks" "We got to love" SIP!! eheheh