Andre Allen
Andre Allen

Reputation: 61

Delete Files Immediately, using AppleScript

I am a novice when it comes to using applescripts. I am wanting to be able to delete the contents of a folder immediately after some previous scripts have been run. I do not know if this is possible, but if it is, I would greatly appreciate the help in providing this solution.

Upvotes: 2

Views: 1672

Answers (1)

CJK
CJK

Reputation: 6092

tell application id ("com.apple.SystemEvents") ¬
        to tell the folder ("~/path/to/folder") ¬
        to delete (files whose visible = true)

Warning: There is no confirmation before or after this command has completed its action. The files will be deleted immediately and will not be recoverable.

files specifically excludes folders. Substitute with the term folders to delete only folders. Substitute with the term items to delete files and folders contained in the folder at the specified path.

~/ is recognised short-form denoting the user's home folder, so ~/path/to/folder is equivalent to /Users/<you>/path/to/folder.

The whose visible filter is spares hidden files that one might wish to keep if they are likely to be important; otherwise, simply amend the final line to delete the files to delete both hidden and non-hidden files. delete the items will scrub everything inside that folder.


Special cases:

If the target is a symbolic link to a folder, target the folder (path of item "~/path/to/folder").

If the target is a Finder alias that points to a folder, retrieve the original path of the folder: tell app id "com.apple.finder" to tell the alias file ("/Users/<you>/path/to/folder"1 as POSIX file) to set pathToFolder to its original item as text. The variable pathToFolder can be used in place of the quoted string path in the System Events block that otherwise remains the same.
1The shorthand ~/ cannot be used in a_ Finder block, so the fully-formed, absolute path will need to be used.

Upvotes: 4

Related Questions