Skip to content

Instantly share code, notes, and snippets.

@alvin-milton
Last active September 10, 2020 22:43
Show Gist options
  • Save alvin-milton/f2d7e8caa842ee1bf4932c8cfa01805f to your computer and use it in GitHub Desktop.
Save alvin-milton/f2d7e8caa842ee1bf4932c8cfa01805f to your computer and use it in GitHub Desktop.
Using Hammerspoon to empty the Trash

Fun w Hammerspoon

How many times do you empty your trash?

If you are like me, sometimes a lot, sometimes never.

It really depends on how long you tend to keep certain files in your system.

I wanted to automate the emptying of the trash.

I don't want to use the mouse each time because keyboard shortcuts tend to be quicker.

So ultimately, I want to be able to use the keyboard and "map" to a command that does this.

Sounds simple right?

First step was to work out in terminal how to do this.

Found out you'd type a command like this to get it done:

rm -rf ~/.Trash/*

I definitely don't want to type this every time in terminal.

Thats multiple keystrokes - the antithesis of what I want to achieve, so I created an alias.

In ~/.bash_profile this line exists:

alias trash="rm -rf ~/.Trash/*

This is closer, it maps the word trash to the command for emptying the trash.

While aliases are cool, this still requires me to be in the terminal.

& I'm not always in the terminal.

Enter Hammerspoon. Feel free to check that out.

In my Hammerspoon config, lives a little bit of Lua which kinda looks like JS code that looks like this:

hs.hotkey.bind(hyper, 'E', function()
  hs.execute('trash', true)
end)

hyper is a variable I set higher in the config that simulates pressing the keys CMD + SHIFT + ALT + SHIFT together.

Do enough research on Hammerspoon, and you'd end up understanding why this type of thing is helpful.

Hint, it has something to do with keyboard automation.

I use the Caps Lock key to simulate pressing all those buttons.

After reloading the config, I can now press CAPS LOCK + E to execute the terminal command trash but without being in the terminal & this will empty my trash.

Mission achieved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment