MacOS – Applescript Command as Specified User to Delete All Files in a Particular Folder

applescriptfinderkeychainmacosterminal

I am looking at using the POSIX path to delete the contents of a particular folder on users computers but it must be performed as a particular user.

I can deploy the script silently from my side using LanRev (Absolute Manage) so the user won't see the password.

In my scenario below, I am trying to execute the script specifying the user as "secretAdmin" and the password will be "secretPass123".

The system would be deleting the contents of the folder ~/Library/Keychains/

tell application "Finder"
    delete (every item of folder "$HOME/Library/Keychains") with user secretAdmin password secretPass123
end tell

Unfortunately this doesn't work. There are different system setups with the $HOME directory being located across different volumes and thus it's always best to use the current user ~/ or $HOME POSIX path instead – unless there is another method that I have overlooked.

Unfortunately, this code results in errors, and I have become sloppy with my AppleScript over the past 2 years not keeping its practice up to scratch.

Any assistance would be greatly appreciated.

Best Answer

You can reference the files like this:

set myPath to (path to library folder from user domain as text) & "Keychains"
tell application "Finder" to get files of folder myPath