Zettelkasten Forum


Keyboard Maestro macro for inserting citekeys from a .bib file

edited March 2019 in The Archive

KM wizards: are there any among you who might be able to create a macro for the quick insertion of citekeys from a .bib file?

I imagine something along the lines of the macros for inserting note links and tags, but where the shortcut "[#" would call up a list of citekeys.

Thanks in the meantime, and happy zettel'n. :smile:

Post edited by ctietze on

Comments

  • Hi @argonsnorts ,

    I just posted my macro here.

    It requires "The Silver Searcher", which on a Mac is easy to install via Homebrew.

    I use "Ctrl+Option+Cmd+Z" for all my Zettelkasten macros, but of course you could change the trigger to "[#".

  • @cdguit fantastic, thank you!

    And it's easy enough to have the citekeys rendered in multimarkdown format---ie [#AuthorYEAR]---by changing the "replace with" field from @$1 to [#$1] :+1:

  • Good work, folks :) I removed the "REQUEST" tag from the title because a KM macro is not a feature request for The Archive itself.

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

  • @argonsnorts You're welcome, I'm glad it works for you.

  • @cdguit and any other wizards out there:

    Is there a way to modify the macro so that the drop-down menu lists both the citekey and the title of the work, but then only inserts the citekey?

  • @argonsnorts

    How could I resist when you call me a wizard?
    No, seriously: This is one of the macros I wanted to have myself for quite some time, and since I had some time today and I knew it could be helpful to someone else, I tried to come up with a macro. For now, I will post it only in this thread, since we might change it. It feels a bit hacky and there are most definitely more elegant ways to do this.

    The macro needs pandoc-citeproc and jq, both can be installed via homebrew.
    It consists of four steps:

    1. pandoc-citeproc is used to convert the .bib file to JSON, which is parsed by jq to extract the author, the title and the citekey.
    2. The user is presented a list to choose one of those bib entries.
    3. The entry is formatted using a regex-based search and replace under the assumption that citekeys do not contain spaces.
    4. The entry is written at the current cursor position.

    I use "@citekey" for my references and Marked.app and pandoc to view the notes (which I should maybe explain in a new thread).
    For multimarkdown citekeys, change the regex in step 3 to ".@([^ ]+).", without the surrounding quotes. This means that the regex still "looks for" the @-sign, but does not capture it in the $1-variable. Finally, change the text in step 4 to "[#%Variable%selectedCitekey%]".

  • Thanks for putting this together!

    I had to tweak things slightly in order to get a properly formatted multimarkdown citekey. For whatever reason, the suggested change to the regex in step 3 didn't work for me. So I changed the @ in both the shell script and the regex to #, and then changed the text in step 4 to [%Variable%selectedCitekey%]. That seemed to do it.

    Also, there I'm getting a ~2.5 second delay between the triggering of the macro and the appearance of the dialogue box (presumably while the .bib file is being converted to JSON?)---but perhaps that is unavoidable? In any case, I appreciate the wizardry. (And wow, I'm glad someone understands regular expressions, because I certainly do not...)

  • You're welcome! Thank you for asking, I'm glad I finally took the tine to build what I wanted to use myself.
    Your solution to multimarkdown citekeys is cleaner, I'm glad it works.

    How many entries are in your .bib file? I just started setting up my Zettelkasten bibliography, it is very small (11 entries) and the dialogue box shows up instantaneously.
    If larger bibliographies are a problem, I would suggest to split the macro in two: One that executes the shell script and saves the results to listOfBibEntries. This macro could be set up to run periodically, for example every hour. The second macro would consist of steps 2-4.
    If you add a new entry to the .bib file and want to use it right away, you would have to run the first macro manually.

    I wanted to use regular expressions for a long time, and only a few months ago had a few reasons to. It is not that hard, and I can highly recommend https://regexr.com to learn and play around.

  • Voila! It now works instantly. Excellent suggestion. :+1:

    And I've got 950 bibliography entries, so it's no surprise that it took a while to convert each time.

  • Alright, sounds good :+1:
    I will split my macro as well.

    Let's try it out for some time, and if we don't find any problems, I will update the macro in the main thread.

  • @cdguit Great macro. A great contribution.

    Like you, I'm just starting to build my bib file. I like the idea of creating the variable with one macro that runs in the background and a separate one that calls the variable. I use the "This Application" trigger and set it to BibDesk is active run every 10 mins. I don't usually have BibDesk open and active unless I'm adding to it and then I want it to update the variable with the newest entry.

    I've been using https://regex101.com/ for help creating regular expressions. https://regexr.com looks similar.

    Will Simpson
    My zettelkasten is for my ideas, not the ideas of others. I will try to remember this. I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon, I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • Hello @Will ,
    thank you very much.

    Your trigger suggestion intrigues me. What exactly is happening there?
    I assume that as long as BibDesk is active, the macro will run every 10 minutes?
    What would happen if I activate BibDesk, add a new entry, and close BibDesk, all of that within 5 minutes?

  • @cdguit I revised my trigger to "This Application" "BibDesk" "Deactivates". Less friction and it runs when the database is saved and I switch windows to The Archive to place a citation. (Bibdesk deactivates) The listOfBibEntries variable is updated at this time. This seems more economical for CPU cycles.

    I also created a quick macro to get back from a citation in a note to the record in BibDesk.

    Will Simpson
    My zettelkasten is for my ideas, not the ideas of others. I will try to remember this. I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon, I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • @Will
    Thanks, both of that are very good ideas!

  • @cdguit, @Will:
    This is awesome, another great KM macro that makes working with The Archive easier!
    When I use your second macro, @cdguit, the titles that show in the search-prompt are changed a little bit compared to how they are entered into the .bib-file: Of the words in the title starting with a capital letter, only the first word still has a capital letter in the search-prompt, all the others now start with lower cases. Any idea what causes that and how it can be changed?

  • Oh, and does anyone know how the script

    export PATH=/usr/local/bin:$PATH;
    pandoc-citeproc --bib2json "path/to/bibliography.bib" | jq -r ' .[] | "\(.author[0].family), \(.title), @\(.id)"'
    

    needs to be changed in order to display the year of the resource as well? Just adding \(.year) into the string "\(.author[0].family), \(.title), @\(.id)" unfortunately didn't work...

  • @Vinho
    I'm not much of a scripter but a few minutes poking around jq man pages and looking at the script output lead to ... in order to get the year to add insert \(.issued."date-parts"[0]).
    I see lots of problems here though. the year comes across with the format [2019]. It would take additional steps to remove the [ ]'s. Also some of my "issued."date-parts"[0]" have years and months which looks a bit odd. [2019,3].

    Will Simpson
    My zettelkasten is for my ideas, not the ideas of others. I will try to remember this. I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon, I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • Thanks, @Will, that works for me! I don't mind having the year in brackets.

  • @Vinho
    I notice the same: When I run the pandoc-citeproc | jq command in the terminal, the capitalization of the original bib file will remain unchanged. When running this command in Keyboard Maestro, the original capitalization is lost. I don't know why this is happening.

    But since this list is only a convenient way to choose the correct citekey and the original bib file remains unchanged, this is acceptable to me.

  • @cdguit: The reason for this being important to me is mainly that I would like to have a second macro that adds the complete bibliography data for an entry with a specific citekey (for the resources section at the end of a Zettel). I could use a modified version of your macro for that if the outputs were nice. But I guess there are other and maybe better ways to create citations in MMD formatting quickly?

  • @Vinho
    Okay, I see what you want to do. I have no experience with MMD references at all, so I'm afraid I can't be of any help.

    Personally, I use BibTex references and just described my setup here.

  • @cdguit @Vinho
    Has either of you found the solution to the changing of capitalization between the bib file produced by BibDesk and Keyboard Maestro? I see different behavior from @cdguit in that the terminal output of
    pandoc-citeproc --bib2json "/Users/will/Dropbox/Apps/BibDesk/zettelkasten.bib" | jq
    displays sentence cased titles rather than title cased titles. Looking in the source .bib file the cases are correct. It seems to me pandoc-citeproc is the culprit not Keyboard Maestro.

    Will Simpson
    My zettelkasten is for my ideas, not the ideas of others. I will try to remember this. I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon, I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • @Will: I haven't explored this further, but my terminal output of the above mentioned command also displays sentence cased titles like in your case. I also assume that pandoc-citeproc is the culprit.

  • @Vinho thanks for checking. I even tested 'jq', replacing it with 'cat' and the same capitalization basterdization occurs.

    Will Simpson
    My zettelkasten is for my ideas, not the ideas of others. I will try to remember this. I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon, I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • Hey guys,
    sorry for letting you wait so long for my answer! I have been quite busy the last weeks.
    I didn‘t look further in the capitalization problem, since I only use Keyboard Maestro to conveniently insert the citekey.
    When things calm down a bit, I might look into the problem again.

Sign In or Register to comment.