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."


https://superuser.com/questions/319553/does-anyone-have-a-mac-terminal-script-to-remove-hidden-files/814104#814104


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


Posted on Jul 17, 2019 7:15 AM

Reply

Similar questions

4 replies

Jul 17, 2019 2:09 PM in response to Matti Haveri

Using dot_clean as part of the answer might be easier.


From an older discussion, the following bash shell script:


dot_clean -m -keep=mostrecent "$@"

find "$@" -name ".DS_Store" -exec rm {} \;

rm -f "$@"/.VolumeIcon.icns
rm -Rf "$@"/.fseventsd
rm -Rf "$@"/.TemporaryItems
rm -Rf "$@"/.Spotlight-V100
rm -Rf "$@"/.Trashes

diskutil eject "$@">/dev/null



Jul 18, 2019 8:56 AM in response to MrHoffman

Thanks all, I tested the extended error messages which seemed to complain about limited access to Spotlight directory (the error message was too long to be displayed completely on screen). It seems the Terminal must be granted Full Disk Access at Security & Privacy to access that folder but even that or omitting Spotlight or other folders from the script didn't solve my problem.


Anyway, I made a very simple Automator application that runs dot_clean. It seems to remove ._* files fine without -nm switches so I left them out at least for now.


Now I don't have to launch the Terminal or other apps for this simple task. I just have to remember that the target drive must be named UNTITLED for the app to work. I guess dot_clean command can do no harm even when it is accidentally used for a wrong drive. Currently the app gives no feedback even if the UNTITLED disk is missing.


The old "DiskCleaner.app" mentioned in my original post was more sophisticated because it had a prompt for the disk name, it deleted also .DS_Store and .FBC* files and .fseventsd, .Spotlight and .Trashes folders and it ejected the disk in the end. I might try to fine-tune the dot_clean script to do all that but my knowledge about this is unfortunately currently very limited.


A cookbook for this little Automator application:


Make an Automator application that deletes ._* files from a mounted disk "UNTITLED":


Applications > Automator > New Document > Application > drag "Run Shell Script" to the area on the right > type without quotes "dot_clean /Volumes/UNTITLED" to the text area > File > Save... > save with a name like "dot_clean UNTITLED" to /Applications.

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.

Remove dot files with an Automator AppleScript

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