Action

Bookends Refereces v2

Posted by suavito, Last update about 2 years ago - Unlisted

Allows to pick Bookends citations like {Kaufman, 2021, #297372} from a prompt list. Optionally the page (range) can be added in the text field.
If a Bookends citation is found in the clipboard it is added to the beginning of the prompt list.
Output is something like {Kaufman, 2021, #297372@97} when a page number has been added.
Template Tags the user has to set:
- [[references]]: A string with the references, divided by |
- [[openingDelimiter]]: The opening delimiter of the Bookends citation fomat, for example {.
- [[closingDelimiter]]: The closiing delimiter of the Bookends citation fomat, for example }.
- [[before]] and [[after]]: Wrapper for the output. If not wanted, leave both empty.

Steps

  • defineTemplateTag

    name
    references
    template
    {Kronauer, 2004, #84357}|{Schulze, 2021, #186943}|{Walser, 2020, #265472}|{Bernhard, 2020, #130280}|{Houellebecq, 2022, #269883}|{Theweleit, 2020, #122493}|{Leo, 2014, #267000}
  • defineTemplateTag

    name
    openingDelimiter
    template
    {
  • defineTemplateTag

    name
    closingDelimiter
    template
    }
  • defineTemplateTag

    name
    before
    template
    
    ---
    
    
  • defineTemplateTag

    name
    after
    template
  • script

    // This script removes the delimiters of the references to make them look more convenient in the prompt list. Additionally, if a citation is found in the clipboard it gets added to the top of the prompt list.
    
    theTrimmedReferences = draft.getTemplateTag("references");
    
    theOpeningDelimiter = draft.getTemplateTag ("openingDelimiter");
    theClosingDelimiter = draft.getTemplateTag ("closingDelimiter");
    
    theClipboard = app.getClipboard();
    
    if ((theClipboard.indexOf(theOpeningDelimiter) == 0) && (theClipboard.indexOf(theClosingDelimiter) == (theClipboard.length - theClosingDelimiter.length)) && (theClipboard.indexOf(", #") != -1)) {
    
    theTrimmedReferences = "★ " + theClipboard + "|" + theTrimmedReferences;
    
    }
    
    theTrimmedReferences = theTrimmedReferences.replaceAll(theOpeningDelimiter, "");
    theTrimmedReferences = theTrimmedReferences.replaceAll(theClosingDelimiter, "");
    
    draft.setTemplateTag ("trimmedReferences", theTrimmedReferences);
    
  • prompt

    promptKey
    theReference
    promptTitle
    References
    promptMessage
    Message?
    promptButtons
    [[trimmedReferences]]
    includeTextField
    true
    textFieldDefault
    includeCancelButton
    true
  • script

    // Inserts the pick from the prompt list plus optional page (range), all wrapped in [[before]] and [[after]]
    
    var theReferences = draft.getTemplateTag("trimmedReferences");
    
    var theOpeningDelimiter = draft.getTemplateTag("openingDelimiter");
    var theClosingDelimiter = draft.getTemplateTag("closingDelimiter");
    
    // sets a variable to the text of the prompt selected
    var theReference = draft.getTemplateTag("theReference_button");
    
    if (theReference.indexOf("★ ") == 0) {
    theReference = theReference.substring(2);
    }
    
    // sets a variable to the text of the text field
    var page = draft.getTemplateTag("theReference_text");
    
    var theBefore = draft.getTemplateTag("before");
    var theAfter = draft.getTemplateTag("after");
    
    // Get the content of the current draft before we modify it
    var text = draft.content;
    
    // get cursor or selection
    var selRange = [draft.selectionStart, draft.selectionLength];
    
    // Insert choice at the cursor or replace selected text
    // draft.content = text.substring(0, selRange[0]) + theReference + text.substring(selRange[0] + selRange[1]);
    
    if ((page != undefined) && page != null && (page != "")) {
    draft.content = text.substring(0, selRange[0]) + theBefore + theOpeningDelimiter + theReference + text.substring(selRange[0] + selRange[1]) + "@" + page + theClosingDelimiter + theAfter;}
    
    else {
    draft.content = text.substring(0, selRange[0]) + theBefore + theOpeningDelimiter + theReference + text.substring(selRange[0] + selRange[1]) + theClosingDelimiter + theAfter;}
    
    
    // Save draft changes
    draft.update();

Options

  • After Success Default , Tags: exzerpt
    Notification Info
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.