WSSCode Blog

Scripting with Babashka on Alfred

December 04, 2021

Today I realized I can easily use Babashka scripts to automate things on my machine with Alfred, and it’s amazing!

If you don’t know Alfred, it’s a great utility tool for the Mac.

Some features I love about Alfred:

  • Blazing fast application launcher
  • Clipboard history (with search support)
  • Internet searches
  • Workflows

The Workflows feature is a very interesting one, it allows you to make tools using a drag-and-drop graph connection to make stuff happens, they look like this:

Workflow example

The workflow you see above allows me to generate valid brazilian tax ids. The CPF is a tax id we use for general people. CNPJ is how we call the tax id for companies.

In today demo we are going to re-create this workflow.

Making the visual part

First go the workflows tab and create a blank workflow.

From there, use the following video as a reference to create the nodes in our workflow:

Workflow example

The workflow structure above will respond to the keyword cpf when typed on Alfred. Then it will run the script. The clipboard task can inject the copied content at the user current text input.

Babashka coding

To make the CPF/CNPJ generation I’m going to use the cadastro-de-pessoa library.

To do this let’s create a bb.edn file:

{:deps {cadastro-de-pessoa {:mvn/version "0.4.0"}}}

The important part is where we are placing this file, we want it to be in the same folder with the rest of the workflow data files.

To locate this directory, right click in the workflow and pick the Open in Finder option at the menu. Put bb.edn at that folder.

Workflow example

Now, edit the Run Script task and use the following content:

/usr/local/bin/bb -m cadastro-de-pessoa.cpf/gen
important

It’s important to use the full path for bb, I installed from Homebrew which places it at /usr/local/bin/bb, if you are not sure where your bb is, run which bb in your terminal and use that full path.

Note that using the -m flag we can tell Babashka to run any function in any namespace at our classpath! This means we can run the function straight from the package, no extra sources needed.

If you wanted to add your own functions, you can add on the same plugin folder, consider that a Babashka project, and them use -m to invoke any function you defined there.

And that’s it, you should be able to use the keyword cpf now on Alfred to generate a CPF and paste at the current cursor position.

As an exercise try to implement the CNPJ generation, and also include snippet triggers to generate values without leaving the current editor (not even to open Alfred).

Click here to download the complete workflow I made here.


Follow closer

If you like to know in more details about my projects check my open Roam database where you can see development details almost daily.

Support my work

I'm currently an independent developer and I spent quite a lot of my personal time doing open-source work. If my work is valuable for you or your company, please consider supporting my work though Patreon, this way you can help me have more available time to keep doing this work. Thanks!

Current supporters

And here I like to give a thanks to my current supporters:

Albrecht Schmidt
Alister Lee
Austin Finlinson
Daemian Mack
Jochen Bedersdorfer
Kendall Buchanan
Mark Wardle
Michael Glaesemann
Oleg, Iar, Anton
West