You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

how to delete files in macOS automator

I am developing an automation (automator 2.10) in macOS Catalina (10.15.7) to input a downloaded file (.csv) into a Numbers (11.1) spreadsheet, select and copy all the transactions, and then paste them into a new spreadsheet using a template. There are several operations that need doing after this e.g. formatting the date column into date format and sorting into ascending sequence so that it's easier to check against the paper statement.


One of the things I have found vital to do is to always bring the spreadsheet window into focus first when starting to "record" keyboard and mouse selections. When doing this on the csv spreadsheet it actually refers to it with the dowloaded file name. The filename is unique as it includes the date and time downloaded so (I'm assuming) the automation won't work on next month's file.


So, I thought, once the downloaded file has been selected in the automation (it prompts the user to select it from the Downloads folder), copy it to a working folder and then rename it to the same name every time. But the rename won't work if it already has a file of the same name in the folder, so it needs deleting first.


I am just trying to delete a specific file from a specific folder but I can't work out what actions I need to do this. I've practically sorted out everything else.


Can anyone help please?


iMac 27″, macOS 10.15

Posted on Nov 1, 2024 11:12 AM

Reply
2 replies

Nov 1, 2024 11:33 AM in response to PaulRSte

Good luck with this. Automator is great with single-threaded, single-path solutions, but is overly cumbersome when it comes to any kind of logic processing (such as determining which document is front most).


To address your question directly:


> I am just trying to delete a specific file from a specific folder but I can't work out what actions I need to do this. I've practically sorted out everything else.


Since you know the path to the file, you can combine a 'Get Specified Finder Items' to locate the earlier file, then add a 'Move Finder Items to Trash' to discard it.


However, my thought is you're doing this the wrong way. Any script that demands a fixed filename in a specific location is bound to break at some point (as well as being inflexible to change).


While it might take a little more work (and learning curve), this task sounds far better suited to AppleScript than Automator. The big advantage is that AppleScript is far more flexible and actually understand things like variables, so you can target items indirectly (e.g. the front document in any application, regardless of what it's called, and even without that application needing to be frontmost).


For comparison, here's an AppleScript that will delete a specific file from your Documents folder


tell application "Finder"
	delete file "Old File.txt" of (path to documents folder)
end tell


To be fair, you can take small-steps using the Run AppleScript action within your Automator workflow before migrating to Script Editor itself.

how to delete files in macOS automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.