How Do I Delete All Files in Downloads Folder with a Hot Key?

Hi all,

I'm new to this, I would like to set up a keyboard shortcut to delete everything in the downloads folder, I tried "delete file" and the downloads folder but it failed.

Can anyone recommend a macro?

@jasonbb, I hope you don't mind that I have revised your topic title to better reflect the question you have asked.

FROM:
New user help with macro

TO:
How Do I Delete All Files in Downloads Folder with a Hot Key?

Providing a succinct, yet descriptive title about your problem/issue will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.


Now, to answer your question:

You can try a For Each action with a Collection of items in a folder (where folder is ~/Downloads), and then use the File Actions action (with Delete) acting on the For Each variable.

Let us know if you need more help.

1 Like

Remember to ensure you have good backups before automating a task that goes around deleting files and folders.

I would also recommend you use the Trash a File action rather than the Delete a File action (and in any event, the Downloads folder tends to be full of packages which are folders, so you would need to use the very dangerous Recursively Delete a File action).

Hey Jason,

This is easily and safely done with AppleScript.

All items are moved to the Trash, so you can recover if you make a mistake.

-Chris


Move Items in Downloads Folder to the Trash.kmmacros (5.2 KB)

2 Likes

That's great, thanks very much for that Chris!

1 Like

Can someone help me by adding a duration to this? For example, if a file is less than 30 days old is DOES NOT get deleted. If it is older than 30 days it is moved to the trash.

Here's one way you could do this just with native KM actions. The calculation used in the If Then Else action checks each file to see if it's older than 30 days by seeing if its modification date is less than the current date minus the number of seconds in 30 days (3600 is the number of seconds in an hour, 24 is of course the number of hours in a day, and 30 is the number of days you specified, so 3600*24*30):

Trash All Files in Downloads Folder Over 30 Days Old.kmmacros (4.1 KB)
image

I've disabled the Trash File action by default, so running the macro in this state will first show you a list of every file that will be deleted. If you're satisfied that the list of files shown is what you want to delete, then you can enable the Trash File action and run the macro again to actually delete them.

Perfect! Thank you!

1 Like

I would like to ask you to teach me what each variable means? I'll post a detailed image of what I mean tomorrow (I want to learn so that I can make my own Macros more efficiently) in the future. I tried doing this with automator, it didn't work.

You're welcome. I'm not sure I'll be able to answer your questions to the degree they require, but if you take the time to post a detailed image explaining what you're having trouble understanding, I'll see what I can do as time permits.

Please explain what each token means in the image? The content in the red boxes.

token%20explanation
Even the one up top that says LocalFile, I forgot to mark it.

Hey Abdallah,

Gabe has shown you how to do this with Keyboard Maestro's native actions; here's how to do it with AppleScript.

-Chris


Trash Files in the Downloads Folder More Than 30 Days Old v1.00.kmmacros (5.1 KB)

1 Like

To Those Who Don't Often Write Scripts:

I know Chris knows this, but for others where are not that familiar with AppleScript, you might (actually should) be concerned about the last command in Chris's script, delete:

tell application "Finder"
  delete ¬
    (files of downloadsFolder ¬
      whose creation date is less than dateMinusThirty as alias list)
end tell

As I'm sure you guessed, the Finder delete command does in fact mean "move to trash".
This is unlike the KM Actions Trash and Delete, which mean exactly what they say. :wink:

However, I will use this opportunity to make a point about using someone else's script:

==Always read the script and make sure you have a reasonable idea of what it does. If there are any doubts, then ask the author before you run the script.==

The command "delete" should have been a red flag to those of you who don't use, or are new to AppleScript.

Those are all variables. Each one is named for what it contains when the macro is run; a (path to a) file, the file's (modification) date, and the macro's results (in this case, a list of the files it finds that meet the deletion criteria) respectively. The Local part essentially means that the variables are newly created whenever the macro is run, and erased when the macro has completed. Variables can be set and referred to either as-is or by the %Variable%[VARIABLE_NAME]% syntax depending on the type of field you are working in at a given moment, but they both refer to the same variable.

If you aren't sure what variables are or how to use them in KM, here are a couple of links for further reading:

Wikipedia Entry on Variables
KM Wiki Entry on Variables

1 Like