Delete Script Deletes Wrong Files

Hey Guys

i m creating a script that does a cleanup of specific folders.

I have created a temp folder with temp files (safe way to test).

When i run the script below though, it deletes all the files on the desktop, and doesn’t touch the given folder.

Any ideas?

set FolderToBeProcessed to ("$HOME/Desktop/New Folder/")
tell application "Finder"
	delete (files)
end tell


Lots of ideas! I guess you still can drag out the files from your trash to your desktop.

Poor poor Finder are trying to do as good as it can while someone are talking “pidgin” AppleScript to it.

First of all: You are using a POSIX path, Finder are accustomed to HFS paths ala: Macintosh Hd:Users:You:Desktop:Test

(And I wouldn’t pull that stunt with the environment variables further than any that are defined during login ” unless you now how to write them into your own plist file in the folder ~/.MacOsX.)

  • You can’t use that in POSIX path expression. the command path to desktop as text gives you the correct hfs path to the desktop folder, which you can elaborate on by concatenating the correct substring representing some sub folder. Spaces in filenames are allowed and the folder separator is “:”

Second: You give finder nothing to work with, and luckily for you it treats the desktop and not the root folder. -Which I would have believed it would have done!
The only sensible thing you are feeding finder with is delete and god knows what error the expression b[/b] accomplishes. -I think the delete command gets an empty list and interprets that to mean all.

Here is a annotated rework: You should really take a gander at the Finder dictionary in the Script Editor.

” set FolderToBeProcessed to ("$HOME/Desktop/Test/") 
-- The $HOME Variable doesn't work very well.
set FolderToBeProcessed to (path to desktop as text) & "New folder"
-- this works better.
tell application "Finder"
	delete (every file of folder FolderToBeProcessed)
	-- I add the POSIX path to the FolderToBeProcessed to have it translated to its HFS equivalent.
	-- I then use the correct syntax to make Finder delete every file of that folder.
	-- every file and so on returns a list with file references, which are handed over to the delete command
	-- which gladly commences.
	-- your previous expression I belive : returned an empty list, which finder intrerpreted as every thing in the working folder
end tell

AppleScript is a lot of fun, and there are many good tutorials here. Seek and ye shall find.

Best Regards