I have used the command.
/usr/bin/osascript -e 'do shell script "echo hello args 2>&1 etc" with administrator privileges'
It asks for password saying osascript want to make changes. What I want is, how do I change the word osascript?
I found a much better way of doing this, by digging through the Applescript doc on the Apple Developer's Site. I found this release note for Applescript in MacOS 10.10
It says:
do shell script can now specify a custom prompt to use in the password dialog. [15194980]
However, it neglects to say how you can do that. After some guesswork, I figured out that there's a prompt
clause that lets you replace osascript wants to make changes...
with whatever you want when using do shell script...with administrator privileges
in a script invoked by osascript
. For example:
osascript -e 'do shell script "ls -l" with prompt "The Great And Powerful OZ " with administrator privileges'
generates a dialog box that looks like:
I hope this helps someone.
The above methods all require administrator privileges in the first place, but if you are trying to do this as part of a bash script you want to distribute without requiring administrative privileges or extra setup, you could do something like the following:
TMP=$(mktemp -d)
pushd "$TMP" > /dev/null 2>&1
/usr/bin/osacompile -e 'do shell script "echo hello args 2>&1 etc" with administrator privileges' -o 'My Cool Name.app'
'My Cool Name.app/Contents/MacOS/applet'
popd
rm -rf "$TMP"
This will create a temporary directory, compile the script as an applet, execute it and then delete the temporary directory and applet.
Assuming either El Capitan or Sierra is installed, first disable SIP and boot to your main system again.
Open Terminal and enter:
sudo ln /usr/bin/osascript /usr/bin/butterfly
Enable SIP again.
Now butterfly will ask to make changes after entering
/usr/bin/butterfly -e 'do shell script "echo hello args 2>&1 etc" with administrator privileges'
Instead of butterfly you can use almost any other name. The name shouldn't be the name of an executable already existing on your Mac (especially it mustn't be one already existing in your PATH).
So ls or diskutil is a big NoNo but necyria_bellona is OK.
This doesn't work for APFS volumes (High Sierra and later) , because hard links don't exist in this file system.
sudo
or disabling SIP. Then use the hard links pathname.
Commented
May 11, 2017 at 17:57
cat /System/Library/Sandbox/rootless.conf
). In High Sierra even some disk blocks seems to be protected (e.g block0=MBR/pMBR). ls -lO /usr/bin/ | grep restricted
.
Commented
Jan 10, 2018 at 15:12