← til

Writing Bear templates with Automator and Ruby

September 20, 2020

Bear is one of my favorite apps. It's simple, has a fantastic minimalist design, and uses Markdown. I'm happy to support its development since it's one of the rare apps that delight me. But, I've also noticed that some features are missing.

I like to stick to the same format when creating some types of notes. For Zettelkasten notes, I use one format and add a meta info in the header. I also journal almost every day, and I've started to use one format for all my journal entries.

That's why I would love to see support for templates added to Bear. Actually, I'm impatient with this, so I've tried to find a workaround, and lo and behold, there is one.

Using a template for journal entries

The trick is to use Automator that executes a Ruby script and map it to a keyboard shortcut.

So, in Automator, create a "Quick Action". Drag "Run Shell Script", select "Workflow receives no input", and enable "Output replaces selected text".

Since I'm writing a template for my journal, I want it to automatically:

  • set the title to the current date in the format I use
  • write the day of the week and current time
  • write the current location and weather
  • write the placeholder for mood
  • tag it properly (I use #journal/year/month format)
  • write the placeholder for any thoughts I have that day
  • write the placeholder for morning journal entry
  • write the placeholder for evening journal entry

Here's the script for this that I've put in Automator:

ruby <<END
  require "date"
  require "open3"

  # I use my small NPM package for getting the location and weather
  weather_and_location = Open3.popen3("npx wethr") do |_, stdout|

  puts <<~TEMPLATE
    #{Time.now.strftime("%A")} at #{Time.now.strftime("%I:%M %p")}

    ## Thoughts


    ## I am grateful for

    ## What would make today great?


    ## What awesome things have happened today?

    ## How could I have made today even better?


I've saved it and went to
System Preferences > Keyboard > Shortcuts > Services and assigned a new keyboard shortcut to it. I've mapped it to ⌃⇧⌘A, which made this possible in Bear:

Fixing date formats

Templates are not the only thing where Automator is useful.

I wanted to update all of the dates in Bear, to be consistent with the date formatting. It's much easier to find specific dates like this. Luckily, Ruby has excellent support for dates and Date.parse handled anything I threw at it:

ruby -e '
require "date"
puts Date.parse(gets)'

I mapped this to ⌃⇧⌘D and went ahead and highlighted all the dates.

Tagging people

Nested tags are useful to improve organization, but also to more easily find things. I like the idea of tagging people under #@, so David would be tagged under #@/david. This makes it easier to find all the mentions of him by just clicking on his tag.

Since I haven't used this system before, I wanted to convert some names to this format. All it took was another simple Ruby script:

ruby -e '
link = gets.strip
bare = link.scan(/([[:alnum:]]+)/).flatten.join("-")
print "#@/#{bare.downcase}"'

I mapped this to ⌃⇧⌘X and then converted all names to use the nested tag.

@shime_sh Hrvoje Šimić
0 kudos