How do I script deletion of files with admin privileges?

I need to build an applescript to delete a handfull of files and folders scattered around the hard drive but i'm not sure how to go about it. This will basically uninstall an app i'm testing before installing a new build. Assume the files are the following:

/Library/Application Support/Foo
/Library/Preferences/Foo
/Library/Receipts/Foo
/Applications/Foo
/Users/<username>/Library/Preferences/Foo

The catch is, I need to elevate my privileges to delete files in the root library folder.

Mac OS X (10.5.5)

Posted on Nov 5, 2008 4:03 AM

Reply
3 replies

Nov 5, 2008 9:25 AM in response to AudioChris

There's two elements to elevating privileges - one is specifying the user to run as, the second is authenticating. In most cases the authentication element is going to prompt the user for a password.
If you know the admin password you can embed it in your script, but if this is going to run on a remote user's system then you'll need the prompt.

In addition, if this is running on other systems you cannot write it using hard-coded paths - what English systems know as 'Application Support' may be something completely different in other languages. Therefore you'll need to use specific AppleScript commands to find the files.

set file1 to POSIX path of (path to application support as text) & "Foo" -- /Library/Application Support/Foo
set file2 to POSIX path of (path to preferences from local domain as text) & "Foo" -- /Library/Preferences/Foo
set file3 to POSIX path of (path to library folder as text) & "Receipts/Foo" -- /Library/Receipts/Foo
set file4 to POSIX path of (path to applications folder as text) & "Foo" -- /Applications/Foo
set file5 to POSIX path of (path to preferences from user domain as text) & "Foo" -- /Users/username/Library/Preferences/Foo

do shell script "rm -rf " & quoted form of file1 & space & quoted form of file2 & space & quoted form of file3 & space & quoted form of file4 & space & quoted form of file5 with admistrative privileges


If you can/want to embed the password, just append 'password "blah" to the 'do shell script' command.

Nov 5, 2008 9:38 AM in response to AudioChris

set aUN to "AudioChris" -- Replace with appropriate administrator user name.
set aPW to "1234" -- Replace with appropriate administrator password.

set tUser to "UserA" -- Replace with appropriate user name.

set tDirs to {"/Library/Application Support/Foo/", "/Library/Preferences/Foo/", "/Library/Receipts/Foo/", "/Applications/Foo/", "/Users/" & tUser & "/Library/Preferences/Foo/"}

repeat with i in tDirs -- Cycle through list of directors to delete.
try -- Capture and ignore any AppleScript error message(s).
do shell script ("rm -rf " & quoted form of i) user name aUN password aPW with administrator privileges
end try
end repeat

Nov 5, 2008 5:56 PM in response to dev_sleidy

Thank you. This is what I ended up using. I hard coded some stuff because it will only be used on my machine. App name deleted so I don't violate my NDA.

display dialog "This will remove Application and install a new build." buttons {"Ok", "Cancel"} default button 2
if the button returned of the result is "Ok" then
set aUN to "adminuser" -- Replace with appropriate administrator user name.
set aPW to "password" -- Replace with appropriate administrator password.

set tUser to "user" -- Replace with appropriate user name.

set tDirs to {"/Library/Application Support/Foo", "/Library/Preferences/Foo", "/Library/Receipts/Foo.pkg", "/Applications/Foo", "/Users/" & tUser & "/Library/Preferences/Foo"}

repeat with i in tDirs -- Cycle through list of directors to delete.
try -- Capture and ignore any AppleScript error message(s).
do shell script ("rm -rf " & quoted form of i) user name aUN password aPW with administrator privileges
end try
end repeat

else

end if

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I script deletion of files with admin privileges?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.