# Alfred Workflow for creating new Zettels including metadata

edited September 2019

I created a simple Alfred Workflow and thought I'd share it, in case it is useful to others. It:

1. Takes input from the Alfred launcher in the form 'z {filename}'
2. Creates a new file in ~/Dropbox/zettelkasten (easy to change in the workflow) named: '{yyyyMMddHHmm} {filename}.md'
3. Opens that file in The Archive (using the thearchive://match/ external link URL scheme)
4. Sends a post notification that the process was completed

The file that was created in step 2 has the following structure:

[[{yyyyMMddHHmm}]]
Title: {filename}
Tags:


I found myself creating new Zettels and having to copy the metadata across from the filename every time. This automates that step.

You can download it here: https://www.dropbox.com/s/zg9pi4irwa2rx9b/zettelkasten.alfredworkflow?dl=1

## Comments

• Thank you for sharing this. It is nice to see an Alfred workflow here alongside all the Keyboard Maestro workflows! It's not only helpful but also helps me understand a little more how to put together an Alfred workflow.

• I updated the workflow to include a Launcher keyword that will generate/refresh a document containing unique tags and their frequency, similar to the approaches discussed here.

However, because I only tag in the 'Tags:' metadata field (rather than in the body), tags are only taken from there.

This Workflow:

1. Is run from the Alfred launcher by typing 'ztags'
2. Pulls tags from the 'Tags:' metadata field1
3. Saves them to a new file: ~/Dropbox/zettelkasten/TAGS.md1
4. Opens that file in The Archive (using the thearchive://match/ external link URL scheme)
5. Sends a post notification that the process was completed

Alfred doesn't have a hook for watching window focus (like Keyboard Maestro). Ideally the tag document would be refreshed whenever it was accessed, but I couldn't find any viable way of doing that either. Typing ztags into the Launcher is the simplest option I could find.

The tag regex (1, 2) is fairly simple because there aren't any edge cases to my tags right now. The thread I mentioned above contains some more robust regex examples.

You can download v2 here: https://www.dropbox.com/s/x4gf49g90pbuxw0/zettelkasten_v2.alfredworkflow?dl=1

1. The script that is executed in steps 3 and 4:
cd ~/Dropbox/zettelkasten egrep -ohs '^Tags:.*\$' -- * | egrep -ohs '#[A-Za-z0-9_\-]+' | sort | uniq -c | sort -t# -k2 > TAGS.md ↩︎ ↩︎

• Thanks a lot for the script in (1).

Its lovely to see Alfred here. I only got Keyboard Maestro because people share quite useful Keyboard Maestro here, but still prefer Alfreds approach for most tasks.

• @sai I downloaded version 2 of your script and I can't get it to generate the TAGS.md file. I changed the path for where to generate the file to where my zettels are (I did the same for the initial workflow and that worked fine). I have searched for the new tags file and cannot find it anywhere.

• @DavidWJ can you turn on the debugger and paste any output?

Also, note that this only pulls tags that are specified on a line beginning with 'Tags:' (i.e. from the tags metadata section) - rather than the full body of the file.

To change the path for the tag generation workflow, you should just need to update the path in the script (highlighted in attached) from cd ~/Dropbox/zettelkasten to cd /path/to/your/zettelkasten.

• edited September 2019

I've added a third trigger to the workflow, inspired by this discussion.

It allows tags to be searched from Launcher. Pressing return on a tag will copy it to the clipboard and insert it into the frontmost application.

You can download v3 here: https://www.dropbox.com/s/lopwpux3lpydiyz/zettelkasten_v3.alfredworkflow?dl=1

Here's the bash script which does all the work:
https://gist.github.com/perchard/04458b46f63e5288de79b038fcbcecae

One great consequence of having this abstracted from a specific application (i.e., as an Alfred/KM workflow, rather than built in to the software) is that it's universal - it works anywhere. It's great to be able to have quick access to all of my tags in emacs too. The plain text approach provides interoperability that would be much more complicated (through APIs) or impossible otherwise.

• @sai I also keep my tags in a line beginning 'Tags:', and I changed the path to the zettels to reflect where I kept them....and therein also lay a bug from my side.

Thank you for making me aware of the debugger. The path to my zettles included a folder made up of two words. The space between them threw the script. I found out how to let the script know that there was a space in the path name, and now all is good.

Is there a way to sort the tags alphabetically or by number of tags, i.e. highest lowest?

• @DavidWJ the current implementation should sort alphabetically. This done by the following part of the script:

sort -t# -k2

To sort by tag frequency in descending order, change it to:

sort -t# -k1 -r

The -k parameter specifies which field to sort by (where field 1 is frequency, field 2 is tag). -r reverses the sort order (default is ascending). You can see the documentation for sort from your terminal by typing man sort (Ctrl-D and Ctrl-U to navigation down/up - man pages are displayed using vi).

• @sai Thank you again for your time on this. I see that it is sorting alphabetically. I was thrown as I have a couple of tags that start with capital letters. They are sorted into alphabetical order first, and then the tags starting with lower case letters. I didn't catch that.

I appreciate the explanation of how the script works. I'm learning something.

• @DavidWJ Ah, my tags are all lowercase so I didn't notice that. You can ignore case by passing -f:

sort -t# -k2 -f

I haven't verified the above, but the man page for sort indicates that this will perform case-independent sorting.

• @sai I just added -f and now case is ignored. Thank you.

• @sai I just found v3. Thank you for this addition.

• Thank you @sai this is fantastic - thought I'd add something else to the discussion, with Alfred it's super easy to dump elements of the clipboard into the Zettel as well, so my workflow is now copying the text I'm looking to preserve, then trigger this workflow and the latest snippet from my clipboard is added automatically. If for some reason that text isn't needed, it's easy enough to erase.

My modification to the script (including adding human-readable date like: Monday April 27, 2020 at 10:55:24 AM CDT):

    ---

ID: [[{var:ymd}]]
Title: {var:title}
Tags:
Date: {date:EEEE} {datetime:long}

---

{clipboard:0}

• @johnny_n If your IDs are date-time-based, you may eventually find out that the human-readable date becomes obsolete. My notes from 2008--2013 had ID and date, but I fare just as well with only the ID. (I guess I got better at reading IDs over the years.)

Author at Zettelkasten.de • https://christiantietze.de/

• @sai finally got around to trying this. I modified the output a bit to fit my needs, and it works a treat. Thanks so much!

• @ctietze said:
@johnny_n If your IDs are date-time-based, you may eventually find out that the human-readable date becomes obsolete. My notes from 2008--2013 had ID and date, but I fare just as well with only the ID. (I guess I got better at reading IDs over the years.)

I also simplified to ID-only. To increase readability, I "spend" one byte on a hyphen between date and time (e.g., 20200430-2332), which I think is totally worth it.

Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!