Search code examples
macosshellapplescriptadmin

osx - Permission denied (126) using AppleScript with Administrator Privileges


I am trying to run a script to enable the screensaver password on my mac (El Capitan), but it requires administrator privileges. I am using an applescript to get the script to run with administrator privileges automatically, but I still get a 126 permission denied error. Here are my files:

The script to activate the password (requires permission):

# ~/bin/passtrue

osascript -e 'tell application "System Events" to set require password to wake of security preferences to true'

Then I created an applescript to run that script with administrator privileges:

# ~/bin/trueapplescript.scpt

do shell script "/Users/user/bin/passtrue.sh" with administrator privileges

Finally I created a shell script to call the applescript file:

# ~/bin/change-via-applescript

osascript /Users/user/bin/trueapplescript.scpt

I keep getting the following error when I run the change-via-applescript file:

/Users/user/bin/trueapplescript.scpt:30:107: execution error: /bin/sh: /Users/user/bin/passtrue.sh: Permission denied (126)

Thanks for any advice!


Solution

  • Maybe I have misunderstood what you are trying to achieve. But if you open the "AppleScript Editor" and paste in the following code, it should work:

    set myAppleScriptAsShellScript to "osascript -e 'tell application \"System Events\" to set require password to wake of security preferences to true'"
    do shell script myAppleScriptAsShellScript with administrator privileges