Skip to content

Instantly share code, notes, and snippets.

@kuus
Last active December 5, 2015 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuus/21951bea92cdd69ee43a to your computer and use it in GitHub Desktop.
Save kuus/21951bea92cdd69ee43a to your computer and use it in GitHub Desktop.
/*!
* Zotero Utilities v0.0.1 (http://kuus.github.io/)
* Copyright 2015 kuus <kunderikuus@gmail.com> (http://kunderikuus.net)
* license MIT
*
* Freely adapted from the code by Philipp Zumstein @ Philipp Zumstein
* https://www.snip2code.com/Snippet/429895/Batch-edit-and-replace-for-Zotero
*
* Available on the global scope under the namespace `zu`
*/
(function (window, exports) {
/**
* Loop through each item in the currently selected collection
* @param {Function} callback
*/
function _eachItemInCurrentCollection (callback) {
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++) {
// console.log(items[i]);
callback(items[i]);
}
}
}
}
exports.eachItemInCurrentCollection = _eachItemInCurrentCollection;
/**
* Batch action: add tag to currently selected collection
* @param {string} tag
*/
exports.batchAddTag = function (tag) {
_eachItemInCurrentCollection(function (item) {
item.addTag(tag);
});
};
/**
* Batch: set item field
* @param {String} field i.e. `'publisher'`
* @param {String} newValue i.e. `'semiotext(e)'`
*/
exports.batchSetField = function (field, newValue) {
var fieldID = Zotero.ItemFields.getID(field);
_eachItemInCurrentCollection(function (item) {
if (fieldID && Zotero.ItemFields.isValidForType(fieldID, item.itemTypeID)) {
item.setField(field, newValue);
// TODO item type
// TODO creators (and other multiple fields)
item.save();
}
});
};
/**
* Batch: set item field
* @param {String} field i.e. `'publisher'`
* @param {String} newValue i.e. `'semiotext(e)'`
*/
exports.batchLogField = function (field, newValue) {
var fieldID = Zotero.ItemFields.getID(field);
_eachItemInCurrentCollection(function (item) {
if (fieldID && Zotero.ItemFields.isValidForType(fieldID, item.itemTypeID)) {
console.log(item.getField(field));
}
});
};
/**
* Batch: set item field
* @param {String} field i.e. `'publisher'`
* @param {String} oldValue i.e. `'sEm__iotext(e)'` or a regex `/^\D*g/`
* @param {String} newValue i.e. `'semiotext(e)'`
*/
exports.batchReplaceField = function (field, oldValue, newValue) {
var fieldID = Zotero.ItemFields.getID(field);
_eachItemInCurrentCollection(function (item) {
if (fieldID && Zotero.ItemFields.isValidForType(fieldID, item.itemTypeID)) {
var content = item.getField(field);
item.setField(field, content.toString().replace(oldValue, newValue));
// TODO item type
// TODO creators (and other multiple fields)
item.save();
}
});
};
/**
* Batch: clean year from auto set `0101`
*/
exports.batchCleanYear = function () {
exports.batchReplaceField('date', 'January 101', '');
};
window.zu = exports;
})(window, {});
@kuus
Copy link
Author

kuus commented Dec 5, 2015

Extra field supplementary fields

See:

{:DOI: }
{:original-date: }
{:original-publisher: }
{:original-publisher-place: }
{:original-title: }
{:reviewed-title: }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment