Find and Replace Scripts (batch processing workaround)
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 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";
This is an old discussion that has not been active in a long time. Before commenting here, you should strongly consider starting a new discussion instead. If you think the content of this discussion is still relevant, you can link to it from your new discussion.
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.
I installed Zutilo and will check that out too!
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.
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