I wrote this script yesterday. The only problem with it is that when I use “POSIX file [dropped file]”, it outputs “:Macintosh HD/path/to/file”. So I have the script write that to a temporary file and have it read everything but “:Macintosh HD”. Then I have it delete the file defined in the temporary file as well as the temporary file itself.
on open theFileDropped
do shell script "echo " & POSIX file (theFileDropped as string) & ": > /tmp/temp.trashData"
set theTemporaryDeletionDataFile to POSIX file "/tmp/temp.trashData"
open for access theTemporaryDeletionDataFile
set garbage to read theTemporaryDeletionDataFile before "/"
set theFileToDelete to read theTemporaryDeletionDataFile before ":"
do shell script "rm -Rf " & (quoted form of theFileToDelete) & "; rm /tmp/temp.trashData"
end open
the parameter of the on open handler is always a list of alias specifers.
POSIX file coerces a POSIX path to a file URL, you want a kind of the opposite
do shell script "echo " & POSIX path of item 1 of theFileDropped & " > /tmp/temp.trashData"
If you just want to delete the dropped files, this is sufficient
on open theFileDropped
repeat with aFile in theFileDropped
do shell script "rm -Rf " & quoted form of POSIX path of aFile
end repeat
end open
BTW, I added a sound effect. (the same sound you hear when you drag a file into the trash!)
on open theFileDropped
repeat with aFile in theFileDropped
do shell script "rm -Rf " & quoted form of POSIX path of aFile
end repeat
do shell script "afplay \"/System/Library/Components/CoreAudio.component/Contents/Resources/SystemSounds/dock/drag to trash.aif\""
end open
do shell script "afplay " & quoted form of POSIX path of ((path to the "dlib" from system domain) & "Components:CoreAudio.component:Contents:SharedSupport:SystemSounds:dock:drag to trash.aif" as string)
I just updated it. It now asks for confirmation before deletion.
on open theFileDropped
display alert "Are you sure you want to permanently delete " & the length of theFileDropped & " file(s)?" message "You cannot undo this action." as warning buttons {"Cancel", "OK"} default button 2
if the button returned of the result is "OK" then
repeat with aFile in theFileDropped
do shell script "rm -Rf " & quoted form of POSIX path of aFile
end repeat
do shell script "afplay \"/System/Library/Components/CoreAudio.component/Contents/Resources/SystemSounds/dock/drag to trash.aif\""
end if
end open