Delete selected files

Hi guys!!!

I am just starting out using applescript and i wanted to write a script that inserted into automator would take the selected files and list them asking me if i wanted to permanently delete them. I am doing this because i hate always having to empty the trash and so i want this application to automatically send the selected files to the trash and then empty it.
So the part im stuck on is after having used the Get Selected FInder Icons command, i want a list to appear with the names of the files to delete, and a simple message “Are you sure you want to delete these file?” and two options Yes or No.

Is it possibile to do something like this??

Thanks a lot for the help!!!

Hi,

welcome to MacScripter.

This is a solution without Automator. Save the script as application bundle and
drag it into any Finder window next to the search field.
You can select file(s) and click it or drag files onto it


on run
	tell application "Finder"
		set sel to selection
		display dialog "you've selected " & (count sel) & " files." & return ¬
			& "do you want to delete them permanently?" buttons {"Cancel", "OK"} default button 1
	end tell
	open sel
end run

on open theseFiles
	repeat with i in theseFiles
		do shell script "rm -rf " & quoted form of POSIX path of (i as alias)
	end repeat
end open

What I do is:

  1. Uncheck “Show warning before emptying the Trash” in Finder preferences, Advanced.
  2. Move stuff to trash by either drag & drop, or command-delete.
  3. Empty trash using command=shift=delete.

No need for a script. In a matter of days it becomes an automatic process that you don’t even think about.

Hi StefanK!

Thanks for the reply. Trying out the script, i now have a few questions:

  1. When i drag files onto the application, the warning message doesn’t come up and the files are just deleted.

  2. I actually wanted to see the file names in the warning message, not just how many items i am deleting, is it possibile to list them?

  3. I don’t understand the code that you used on the last part of the script…from ‘on open theseFiles’ onwards…could you please explain this code to me as i am new to applescript.

I thankyou in advance for the help!

You are perfectly right, but the whole point of this script is not the actually utility, but the fact that using silly examples i am trying to learn a bit of applescript so hopefully i will be able to do something more useful. :slight_smile:


on run
	tell application "Finder" to set sel to selection
	open sel
end run

on open theseFiles
	set nameList to {}
	repeat with i in theseFiles
		set end of nameList to name of (info for i as alias)
	end repeat
	set {TID, text item delimiters} to {text item delimiters, return}
	set nameList to nameList as text
	set text item delimiters to TID
	
	tell application "Finder"
		display dialog "do you want to delete the files" & return & return & nameList & ¬
			return & return & "permanently?" buttons {"Cancel", "OK"} default button 1
	end tell
	repeat with i in theseFiles
		do shell script "rm -rf " & quoted form of POSIX path of (i as alias)
	end repeat
end open

the open handler take the items one by one and deletes them using the shell command rm