-
-
Save bwiernik/21ede60a8f7819a53715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 = 'de'; | |
/* | |
// 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 fieldID = Zotero.ItemFields.getID(field); | |
var selectedCollection = ZoteroPane.getSelectedCollection(); | |
if (!selectedCollection) { | |
alert('Select a Collection first'); | |
} else { | |
var items = selectedCollection.getChildItems(); | |
if (confirm('Do you want to change these ' + items.length + ' items in collection ' + selectedCollection.name + '?')) { | |
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