Search code examples
macosterminalapplescriptrootsudo

Apple Script to run an application as root (Part 2)


I am using the following Apple Script code to run an application:

do shell script "/Applications/MyApp.app/Contents/MacOS/myapp > /dev/null 2>&1 &" with administrator privileges
quit

This is working fine with one issue. The application requires another application to be in the $PATH environment variables. When the above code is run, it shows a message that the application that should be in the environment variables is not there. Although the application folder path is correct in environment variables and the required second application is there.

However, the following code works where it does not show a message that path is not found in environment variables.

tell application "MyApplication"
do script "/Applications/MyApp.app/Contents/MacOS/myapp > /dev/null 2>&1 &"
end tell

But this also need to run as administrator (root). How to achive this?


Solution

  • You need to add the path in the code itself. This is the code that worked:

    do shell script "export PATH=/path/to/folder:$PATH && /Applications/MyApp.app/Contents/MacOS/myapp > /dev/null 2>&1 &" with administrator privileges
        quit
    

    You need to add the path location separated by &&.