Search code examples
shellapplescriptequivalentjavascript-automation

JXA Equivalent of With Administrator Privileges


In AppleScript, you can do:

do shell script "echo 'I am (G)root'" with administrator privileges

Sorry. Anyway, a shell script can be run in JXA like this:

var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript("echo I am not root :(");

However, there doesn't seem to be an equivalent of with administrator privileges in JXA. Is there one that I'm missing, or how else should I do it? And don't tell me to use sudo. It won't work.


Solution

  • Put option in {} --> app.doShellScript("ls -l", {administratorPrivileges:true});

    Example with multiple options : app.doShellScript("ls -l", {userName:'MyUserName', password:'0123456', administratorPrivileges:true, alteringLineEndings:false});


    Look at the StandardAdditions dictionary, select "JavaScript" in the popup instead of "AppleScript" (the default).

    enter image description here