Zettelkasten Forum


Keyboard Maestro macro for inserting citekeys from a .bib file

edited March 3 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:

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 • http://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
    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
    kestrelcreek.com

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

Sign In or Register to comment.