Remove dot files with an Automator AppleScript
I have used an Automator.app AppleScript below to delete ._* dot-files and other garbage on USB sticks before using them on a TV or Windows where they produce unnecessary clutter and errors.
This "DiskCleaner.app" worked very fine in El Capitan but in Mojave I have to authenticate (no problem) but then it fails with "Can't clean, Permission denied".
I have allowed that app in Security & Privacy > Automation and have tried to fine-tune the script and other system settings to the best of my very limited knowledge but it still fails.
I'd be grateful if someone with more experience could take a look why it fails and how it could be fixed.
If I can find no solution, I'll use a dedicated app like "CleanMyDrive 2" for this. Any other suggestions for a simple application for this task?
"Create an Automator "Application" to "Run AppleScript". Use the script below and save the app as "DiskCleaner.app". This script will ask you to choose the USB disk to clean and eject. Then it will clean the USB disk (tries with admin credentials if failed with current ones), then eject if possible."
on run {input, parameters}
try
tell application "Finder" to set allDrives to the name of every disk whose local volume is true and startup is false
set driveName to {choose from list allDrives with title "DiskCleaner" with prompt "Disk to Clean and Eject:" OK button name "Clean and Eject" cancel button name "Abort"} as text
set getDriveIdentifier to "diskutil info " & quoted form of driveName & " | grep Ident | awk '{print $3}'"
set driveIdentifier to do shell script getDriveIdentifier
try
do shell script "find /Volumes/" & quoted form of driveName & " -name '.DS_Store' -type f -delete"
do shell script "find /Volumes/" & quoted form of driveName & " -name '.FBC*' -type f -delete"
do shell script "find /Volumes/" & quoted form of driveName & " -name '._*' -type f -delete"
do shell script "rm -rf /Volumes/" & quoted form of driveName & "/.{,_.}{fseventsd,Spotlight-V*,Trashes}"
display notification "Disk Cleaned"
on error
try
do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '.DS_Store' -type f -delete" with administrator privileges
do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '.FBC*' -type f -delete" with administrator privileges
do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '._*' -type f -delete" with administrator privileges
do shell script "sudo rm -rf /Volumes/" & quoted form of driveName & "/.{,_.}{fseventsd,Spotlight-V*,Trashes}" with administrator privileges
display notification "Disk Cleaned"
on error
display notification "Can't Clean, Permission Denied"
end try
end try
delay 1
try
do shell script "diskutil eject " & driveIdentifier
display notification "Disk Ejected"
on error
display notification "Can't Eject, Disk in Use"
end try
on error
display notification "No Disk to Clean and Eject"
end try
end run