Skip to content

Instantly share code, notes, and snippets.

@zuphilip
Last active October 2, 2024 01:17
Show Gist options
  • Save zuphilip/b942c8bf2f1126982517 to your computer and use it in GitHub Desktop.
Save zuphilip/b942c8bf2f1126982517 to your computer and use it in GitHub Desktop.
Batch edit and replace for Zotero
/*
How does this work?
0) make a backup copy
1) create a collection of all entries you want to batch edit
(maybe use a smart search or drag and drop, delete individual items)
2) check that you have exactly the items you want to change in your collection
3) choose one example here and adapt it to your needs
4) run the code
5) save the log if needed
*/
// Example 1: set a fixed value in a field
// (the field must be a simple field)
var action = 'set';
var field = 'language';
var value = 'en';
/*
// Example 2: set a fixed value as type
var action = 'set';
var field = 'type';
var value = 'book';
*/
/*
// Example 3: replace a value with another one
var action = 'replace';
var field = 'publisher';
var oldvalue = 'Wiley and Sons';
var newvalue = 'Wiley';
*/
/*
// Example 4: replace creator-lastName or creator-firstName
var action = 'replace';
var field = 'creator-lastName';
var oldvalue = 'Göthe';
var newvalue = 'Goethe';
*/
// Example 5: replace with regex
//var action = 'replace';
//var field = 'DOI';
//var oldvalue = /^\D*/g;
//var newvalue = '';
var items = [];
var fieldID = Zotero.ItemFields.getID(field);
var selectedCollection = ZoteroPane.getSelectedCollection();
var selectedSavedSearch = ZoteroPane.getSelectedSavedSearch();
if (!selectedCollection && !selectedSavedSearch) {
alert('Select a Collection or Saved Search first');
}
if (selectedCollection) {
items = selectedCollection.getChildItems();
if (!confirm('Do you want to change these ' + items.length + ' items in collection ' + selectedCollection.name + '?')) {
items = [];
}
}
if (selectedSavedSearch) {
var ids = selectedSavedSearch.search();
for (var k=0; k<ids.length; k++) {
items[k] = Zotero.Items.get(ids[k]);
}
if (!confirm('Do you want to change these ' + items.length + ' items in collection ' + selectedSavedSearch.name + '?')) {
items = [];
}
}
for (var i=0; i<items.length; i++) {
//set the type
if (field == 'type' && action == 'set') {
var typeID = Zotero.ItemTypes.getID(value);
console.log(items[i].itemID + ' : type : ' + items[i].itemTypeID + ' -> ' + typeID + ' (' + value + ')');
items[i].setType(typeID);
items[i].save();
continue;
}
if ((field == 'creator-lastName' || field == 'creator-firstName') && action == 'replace') {
var subfield = field.split('-')[1];
var creators = items[i].getCreators();
for (var j=0; j<creators.length; j++) {
var content = creators[j].ref[subfield];
var updatedValue = content.toString().replace(oldvalue, newvalue);
console.log(items[i].itemID + ' : ' + field + ' : ' + content + ' -> ' + updatedValue);
creators[j].ref[subfield] = updatedValue;
}
items[i].save();
// TODO update view seems only to work incomplete
continue;
}
//set single fields
if ( fieldID && Zotero.ItemFields.isValidForType(fieldID, items[i].itemTypeID) ) {
var content = items[i].getField(field);
var updatedValue;
if (action == 'set') {
updatedValue = value;
}
if (action == 'replace') {
updatedValue = content.toString().replace(oldvalue, newvalue);
}
console.log(items[i].itemID + ' : ' + field + ' : ' + content + ' -> ' + updatedValue);
items[i].setField(field, updatedValue);
items[i].save();
continue;
}
console.log(items[i].itemID + ' : type ' + items[i].itemTypeID + ' : field ' + field + ' untouched' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment