4

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?

5

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: enter image description here

I hope this helps someone.

  • Wow. I have been looking for this all day long. This is a total life saver. – rubynorails Jan 8 '18 at 21:26
2

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.

  • I'm doing exactly that... but I also want to return a value from my AppleScript. I've tried error, return and log, which all return values in the Script Editor, but don't affect $? in the shell. If I use osascript, I can get the return values, but then the dialog box has "osascript" in it instead of "My Cool Name". Any ideas? – Eric Oct 18 '17 at 21:55
0

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.

  • You can also just make the hard link to a location that doesn't need the use of sudo or disabling SIP. Then use the hard links pathname. – user3439894 May 11 '17 at 17:57
  • I tried this before reading your comment (both linking and copying), and it did not work. I am using High Sierra, so this may have been disabled in newer releases, but this is not a reliable or valid option. The accepted answer did the trick for me. – rubynorails Jan 8 '18 at 21:25
  • @klanomath hmm, seems you are correct that no answer has been accepted. Strange! I guess I just looked at the highest-voted answer and my brain just thought it had been accepted for some reason. That's the method I am using to accomplish this. But yeah, requiring a reboot and having a user disable SIP from recovery or single user would be a tough sell on this. Lol. Also, could you clarify in your answer why disabling SIP is required? I thought it only protected files in /System. Obviously I could be wrong though. I usually disable SIP for various reasons. – rubynorails Jan 10 '18 at 14:59
  • 1
    @rubynorails The list of SIP-protected items is much longer (e.g 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. – klanomath Jan 10 '18 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .