Find and Replace Scripts (batch processing workaround)

edited December 18, 2019
I know this is something that has come up time and time again, but I was wondering if there was some easy way to create a script to change a particular field for all files in a folder. And then give a pointer as to how to modify this script (for non-programmers like me) to change a different field to something else (and where to find the correct names for the fields).

For example: change all "item types" to "newspaper"
and then: change all "publication" to "New York Times"

On these forums, I found the following script which is probably close to what I'm looking for.

var fieldName = "journalAbbreviation";
var oldValue = "Urban Sdd";
var newValue = "Urban Studies";

var fieldID = Zotero.ItemFields.getID(fieldName);
var s = new Zotero.Search();
s.libraryID = Zotero.Libraries.userLibraryID;
s.addCondition(fieldName, 'is', oldValue);
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
let item = await Zotero.Items.getAsync(id);
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
await item.save();
}
});
return ids.length + " item(s) updated";

  • For changing field entries, I would recommend using the Zutilo add-on, see here.

    For changing item types, you could try this script. Note that when changing the itemType of an item A from type1 to type2, there can be fields that are non-empty in A (type1) but not defined for type2. I think you will currently get a prompt asking to discard that field entry.
  • edited December 18, 2019
    The script works to convert to books. I also tried 'thesis' and that worked too, but 'newspaper article' did not. So I guess the names you see in the pop-down menu are slightly different than the codes.

    I installed Zutilo and will check that out too!
  • How are you downloading Zutlio, and how are you trying to install it? What exact steps are you following? It definitely works in general.

    The correct approach is to download the latest zutilo.xpi file from here: https://github.com/willsALMANJ/Zutilo/releases

    Then, open Zotero (not Firefox), click Tools → Add-ons, then drag the downloaded zutilo.xpi file onto the window that pops up.
  • @bwiernik -- sorry I had already figured it out. I was stuck with the Firefox problem and edited my original post removing that part. You probably started typing this answer before I edited my post. Since then, I've been enjoying this nice piece of software!
  • I'm still wondering where I can find a list of item types for the script....
  • In the Zutilo context menu, you can "copy item fields", then paste to a text editor. The itemType for the given item will be in these data.
  • edited January 12, 2020
    @eversdavid

    I found this as the official list of item types (note that they are case sensitive) : https://api.zotero.org/itemTypes



    It doesn't have all the itemtypes in JurisM though
Sign In or Register to comment.