citation-tools

Scripts to help with Zotero and CSL

https://github.com/pobrien333/citation-tools

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (2.2%) to scientific vocabulary
Last synced: 6 months ago · JSON representation ·

Repository

Scripts to help with Zotero and CSL

Basic Info
  • Host: GitHub
  • Owner: POBrien333
  • Language: JavaScript
  • Default Branch: main
  • Size: 13.7 KB
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created about 1 year ago · Last pushed 7 months ago
Metadata Files
Readme Citation

README.md

citation-tools

Scripts to help with Zotero and CSL.

  1. Citation Labeler that outputs citation labels into the extra field for Citation Styles: https://github.com/POBrien333/citation-tools/blob/main/citation-labeler.js
  2. tags-to-extra-field.js allows you to copy Zotero tags to the extra field as keywords: https://github.com/POBrien333/citation-tools/blob/main/tags-to-extra-field.js
  3. Grant Application citation style that is super short: https://github.com/POBrien333/citation-tools/blob/main/grant-applications-super-short.csl

Owner

  • Login: POBrien333
  • Kind: user

Citation (citation-labeler.js)

// This script takes the first author letters and the last 2 digits from the year and outputs it into the extra field as the citation-label to use with CSL citation styles in Zotero.
(async () => {
    var items = Zotero.getActiveZoteroPane().getSelectedItems();
    if (!items.length) {
        window.alert("No items selected.");
        return;
    }

    var existingLabels = 0;
    var skippedItems = 0;
    var updatedItems = 0;


    items.forEach(item => {
        var extraField = item.getField('extra');
        if (extraField && extraField.includes('citation-label')) {
            existingLabels++;
        }
    });

    // Prompt for overwrite if necessary
    var overwrite = true;
    if (existingLabels > 0) {
        overwrite = window.confirm(
            `${existingLabels} items already have a citation-label. Do you want to overwrite them?`
        );
    }

    // Process items
    for (let item of items) {
        var extraField = item.getField('extra');
        if (extraField && extraField.includes('citation-label')) {
            if (!overwrite) continue; // Skip if overwrite not confirmed
        }

        var date = item.getField('date', false, true);
        var yearLastTwoDigits = '';
        if (date) {
            var parsedDate = Zotero.Date.strToDate(date);
            if (parsedDate.year) {
                yearLastTwoDigits = parsedDate.year.toString().slice(-2); // Extract last 2 digits of the year
            }
        }

        if (!yearLastTwoDigits) {
            skippedItems++;
            continue; // Skip items without a valid year
        }

        var authors = item.getCreators();
        if (!authors || authors.length === 0) {
            skippedItems++;
            continue; // Skip items without authors
        }

        var lastName = authors[0].lastName.toUpperCase();
        var citationLabel = `${lastName.slice(0, 3)}${yearLastTwoDigits}`;

        // Prepend to the Extra field
        extraField = (extraField || '').replace(/^/, `citation-label: ${citationLabel}\n`);
        item.setField('extra', extraField);
        updatedItems++;
    }

    // Save changes
    await Zotero.DB.executeTransaction(async () => {
        for (let item of items) {
            await item.saveTx();
        }
    });

    // Summary alert
    window.alert(
        `${updatedItems} items were given a citation label.\n` +
        `${skippedItems} items were skipped.\n` +
        `${existingLabels} existing citation-labels were ${overwrite ? 'overwritten' : 'not overwritten'}.`
    );
})();

GitHub Events

Total
  • Push event: 1
  • Public event: 1
Last Year
  • Push event: 1
  • Public event: 1