Folder Action to erase SEVERAL files with the same name

Hi @all,

I spent my last 3 hours trying to solve an Applescript task without really knowing Applescript.

What I try is to set up a folder action that does the following:

  • delete file in the folder
  • delete a file with the same name in a different folder

My last attempt looks like this and - naturally - doesn’t do what I hope:

on adding folder items to this_folder after receiving added_items
	repeat with i from 1 to number of items in added_items
		set new_item to item i of added_items
		
		set trash_Folder to path to trash folder from user domain
--erasing the file triggering the folder action
		do shell script "mv " & quoted form of POSIX path of new_item & space & quoted form of POSIX path of trash_Folder
--failing attempt to erase the file with the same file name in a folder on my desktop
		do shell script "mv /Users/home/Desktop/toerase/" & quoted form of (get name of new_item) & space & quoted form of POSIX path of trash_Folder
	end repeat
end adding folder items to

If any of you could find the problem: it would be a HUGE help as this is the last step of creating a fully automated routine from my document scanner to an Acrobat-OCR and -optimization and finally to import the ocr-ed and optimized pdfs into Evernote.

All the best

joh

Hi.

try this


on adding folder items to this_folder after receiving added_items
	set trash_Folder to POSIX path of (path to trash folder from user domain)
	set toEraseFolder to POSIX path of (path to desktop folder) & "toerase/"
	
	repeat with i from 1 to number of items in added_items
		set new_item to item i of added_items
		tell application "System Events" to set fileName to name of new_item
		do shell script "mv " & quoted form of POSIX path of new_item & space & quoted form of trash_Folder
		do shell script "mv " & quoted form of (toEraseFolder & fileName) & space & quoted form of trash_Folder
	end repeat
end adding folder items to

That’s it!!!

Thank you so so so much!!!

Actually from right next door (Konstanz) :slight_smile:

If you want to really delete the files, not just move them to the trash, you may use :

on adding folder items to this_folder after receiving added_items
	set toEraseFolder to POSIX path of (path to desktop folder) & "toerase/"
	
	repeat with i from 1 to number of items in added_items
		set new_item to item i of added_items
		tell application "System Events" to set fileName to name of new_item
		do shell script "rm " & quoted form of POSIX path of new_item & space & quoted form of (toEraseFolder & fileName)
	end repeat
end adding folder items to

It delete the two files in a single call.

Of course, with such code there is no way to move back the files from the trash.

Yvan KOENIG (VALLAURIS, France) vendredi 12 décembre 2014 10:49:10

Thank you for the modification. I’ve put it in, everything working very smoothly.

All the best

joh