Bulk editing of titles - is it possible?

edited June 24, 2020
I need to reformat a bibliography for a particularly persnickety journal.

I know that I can select an individual item in my Zotero library on my Mac, right click on the Title field and convert it to sentence case. But is there a way to do that for a whole group of items that I select?

I also know that I can add HTML tags to scientific names within an individual item in my library, so that they appear in italics. But is there a way to do a search and replace for a whole group of items, so as to add the tags without having to do each item individually?
  • For the species names, you can use Zotero's JavaScript API to search for the name, then replace it with the name+tags:
    https://www.zotero.org/support/dev/client_coding/javascript_api#batch_editing

    It might also be possible to use the JavaScript API to mass-convert titles to sentence case (you should always store titles in sentence case), but you will want to be careful there to check for proper casing of words afterward (e.g., the genus name).

    @dstillman Is the sentence-casing function available in the JavaScript API?
  • @bwiernik thanks for the link!
  • edited June 24, 2020
    @bwiernik the code for the sentence-casing is at https://github.com/zotero/zotero/blob/master/chrome/content/zotero/bindings/itembox.xml#L2075 . You can't call it directly (code living inside XBL is a real PITA to reach into) but it's just two lines.
  • (and for a specific use case, you could do much better than Zotero's default sentence case code, e.g. by adding some exceptions, e.g. for genus names.
  • edited February 25, 2022
    This is the script I've written to preserve words with uppercase letters other than the first.


    zoteroPane = Zotero.getActiveZoteroPane();
    items = zoteroPane.getSelectedItems();
    var result = "";
    for (item of items) {
    var title = item.getField('title');
    result += " " + title + "\n";
    var new_title = title.replace(/\b([A-Z][a-z0-9]+|A)\b/g, function (x) { return x.toLowerCase(); });
    new_title = new_title.replace(/(^|\?\s*)[a-z]/, function (x) { return x.toUpperCase(); });
    result += "-> " + new_title + "\n\n";
    // // Do it at your own risk
    // item.setField('title', new_title);
    // await item.saveTx();
    }
    return result;

  • edited October 5, 2020
    @zepinglee - thanks for the inspiration.

    My variation of your code simply converts titles of selected items to sentence case. Proper nouns then need to be manually re-capitalised. A marker is added to the extra field so that this code will not overwrite re-capitalised titles. On balance, this saves me time when lots of titles have initially been saved to Zotero in title case.


    // this code changes titles in selected items to sentence case
    // note that Proper nouns will need to be manually changed(back) to Upper case
    // paste and run this code from Zotero>Tools>Developer>Run Javascript
    zoteroPane = Zotero.getActiveZoteroPane();
    items = zoteroPane.getSelectedItems();
    var result = "";
    for (item of items)
    {
    var extra = item.getField('extra');
    if(extra.indexOf("sentenceCase:1") === -1)
    {
    var title = item.getField('title').trim();
    if(title)
    {
    result += " " + title + "\n";
    // make first char uppercase and remainder lowercase
    var new_title = title[0].toUpperCase() + title.substring(1).toLowerCase();
    result += "-> " + new_title + "\n\n";
    // add marker in extra to stop title being overwritten by this code after manually (re-)capitalising proper nouns
    var new_extra = extra + "\nsentenceCase:1";
    // uncomment next line to write changes to title and extra fields
    //item.setField('title', new_title); await item.saveTx(); item.setField('extra', new_extra); await item.saveTx();
    }
    }
    }
    return result;

  • @zepinglee It works, thanks your code.
  • Update 2022:

    Using Better BibTeX plugin: Select the items, right click and choose "Better BibTeX -> BBT Sentence-case".
  • This seems not working under Zotero 6.0.6 in the mac os (v 12.3.1). The letter fter : remains lower. Could anybody help double check if this is the case? Thank you.
  • If your question is about BBT sentencecase, please open an issue on github.
  • edited May 3, 2022
    @frankcsiu Yes, I can reproduce this behavior and I suggest a further discussion at https://github.com/retorquere/zotero-better-bibtex/issues .
  • edited May 3, 2022
    Oh wait, I thought fter was something special, like a ligature, but you just meant "after :". In which case, correct, BBT sentencecase only applies lower-case to parts of the title in the assumption that the title is in Title Case, it doesn't ever upper-case anything.
Sign In or Register to comment.