Replace Single Character in Names through JavaScript Batch Editing

I'm looking for assistance replacing a single character in the creator/author names field. I've looked at some examples using other fields but cannot make it work with the name field, especially single field mode names (e.g. "XYZ Institute"). Maybe because it's an array?
  • Can you be more specific about what you're trying to change (current value -> desired new value), what you've tried, and what isn't working?
  • Hi! I tried adapting the name change example in the coding documentation at https://www.zotero.org/support/dev/client_coding/javascript_api#examplecreator_name_changes. But the first & last name was throwing me off.

    What I have is one collection with corporate names that imported with a "$" instead of a comma. I want to keep the one-field fieldMode while replacing the $ with a comma.
    For example, "U.S. EPA$ Office of Research" becomes "U.S. EPA, Office of Research" without changing the fieldMode into firstName "Office of Research" and lastName "U.S. EPA."

    Does that make sense?
  • Where'd these items come from? That looks like a MARC artifact. If items with this issue are coming from a library catalog, we'd want to fix that.

    In any case, Tools -> Run JavaScript, run this:

    for (let item of ZoteroPane.getSelectedItems()) {
    item.setCreators(item.getCreators().map(c => {
    if (c.fieldMode === 1) {
    c.lastName = c.lastName.replace('$ ', ', ');
    }
    return c;
    }));
    await item.saveTx();
    }
  • I appreciate your assistance. When I tried to run the suggested code I received this:
    https://s3.amazonaws.com/zotero.org/images/forums/u2397286/a9ea1y06q90d6tvbv4dd.jpg

    I suspect I'm missing something very basic?
  • That means it worked. It doesn’t return anything. Select all the items you want to fix and run it again!
Sign In or Register to comment.