USB Cleaner Automator app

Hi All, I found an AppleScript to clean a USB of the dot files and safely eject it. Im trying to use it in an Automator app on my desktop. The one I found set the path to a picked folder from a window, but I just wanted to drag and drop the USB icon into the Automator App on my desktop. I keep getting this error and I’m not sure why as this was supposedly a working applescript. Can someone help please. Thank you in advance, Adown

sh:
if: command not found
sh: then
: command not found

Here’s the script and Im running OSX Sonoma:

set selectedVolume to (POSIX file "/Volumes")
set volumePath to POSIX path of selectedVolume
do shell script "
if [ -d '" & volumePath & "' ]; then
  dot_clean -m '" & volumePath & "'
  find '" & volumePath & "' \\( -name .DS_Store -o -name .apdisk \\) -type f -delete
  rm -rf '" & volumePath & "'/{.Trashes,.Spotlight-V100,.fseventsd,.TemporaryItems}
  diskutil eject '" & volumePath & "'
fi"

display dialog "			Volume is clean and safe to unplug" buttons {"OK"}

/Volumes is the folder where the disk items are located. At least you have to append the name of the USB flash disk.

And this

set selectedVolume to (POSIX file "/Volumes")
set volumePath to POSIX path of selectedVolume

is back and forth. One line is sufficient, a literal path starting with / is a POSIX path

set volumePath to "/Volumes"

Thank you for your input. I do see the redundancy, but I still get the the “if” and “then” command not found. I am reading up trying to make sense of the path in those arguments. Have a great day and thanks again!

I’ve put your code in Script Debugger and (after commenting rm section) execute it without errors.

Do you run it from Automator? Maybe here in the problem. If yes , show this part of Automator flow.

Can yo modify your script like below and send what yo see in the dialog:

set selectedVolume to (POSIX file "/Volumes")
set volumePath to POSIX path of selectedVolume
-- do shell script 
set theScript to "
if [ -d '" & volumePath & "' ]; then
  dot_clean -m '" & volumePath & "'
  find '" & volumePath & "' \\( -name .DS_Store -o -name .apdisk \\) -type f -delete
  rm -rf '" & volumePath & "'/{.Trashes,.Spotlight-V100,.fseventsd,.TemporaryItems}
  diskutil eject '" & volumePath & "'
fi"
display dialog theScript buttons {"OK"}
-- display dialog "			Volume is clean and safe to unplug" buttons {"OK"}

Another question is - do you have script/command called in 2nd line - dot_clean

Your need to get the USB Volume as passed input from your Automator drop/input action and then pass that thru as a variable to your AppleScript