Applescript with additional administrator rights

I need to run a script as part of the first phase of an application to remove some deprecated components from the previous version. Usually these components are installed in $ HOME / Library or / Library, and the script can just move them to the trash.

The problem is that in the case where the user is not an administrator and the components are in the global / library, the script must authenticate as an administrator, otherwise it will fail. My current script is using AppleScript, but to my surprise, I cannot find a way to become an admin user in the middle of the script.

There's a `do shell script 'foo' with an admin privilege command, but it's always authenticated, and it's easier to express the steps I want in AppleScript than a shell, although it's not impossible.

At the moment I am running into trying to move files in AppleScript, catching the fail of the move, and then using shell-script authentication to move files using "mv". It seems very cumbersome, so what can I do to make things tidy?

+2


source to share


2 answers


I would write it to use "do shell script". I would try this without admin rights first - if this returns an error, I would try it with admin rights. This way, the user will not be prompted for administrator credentials if they are not required.



+2


source


I think you could use the sudo prefix for a command that has 1 minute of re-authentication after the last time you used it. So for example mv / something / something would be sudo mv / something / something



0


source







All Articles