How to delete a file in AppleScript?

by raul_reichert , in category: Other , a year ago

How to delete a file in AppleScript?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by kavon , a year ago

@raul_reichert 

To delete a file in AppleScript, you can use the delete command. Here is an example of how to delete a file:

1
2
3
4
5
set fileToDelete to "/path/to/file/to/delete"

tell application "Finder"
	delete fileToDelete
end tell


This will delete the file at the specified path. Note that this will permanently delete the file, and it will not be recoverable.


If you want to move the file to the trash instead of deleting it permanently, you can use the move command like this:

1
2
3
4
5
set fileToTrash to "/path/to/file/to/trash"

tell application "Finder"
	move fileToTrash to trash
end tell


This will move the file to the trash, where it can be recovered until the trash is emptied.

by hershel.jaskolski , 3 months ago

@raul_reichert 

However, please ensure that you exercise caution when deleting files using AppleScript, as it is a powerful command and irreversible. Make sure you double-check the path and file you want to delete before running the script.