Skip to content

Instantly share code, notes, and snippets.

@the-solipsist
Created September 10, 2023 20:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-solipsist/1f7deba5ccc3e36e6d54096748538f35 to your computer and use it in GitHub Desktop.
Save the-solipsist/1f7deba5ccc3e36e6d54096748538f35 to your computer and use it in GitHub Desktop.
Zotero search and replace javascript
// Adapted from
// https://forums.zotero.org/discussion/78501/possible-to-search-replace-a-character-in-all-titles
var s = new Zotero.Search();
s.libraryID = Zotero.Libraries.userLibraryID;
//s.addCondition('itemType', 'is', 'attachment');
// Add as many conditions as you like
s.addCondition('title', 'contains', 'Text to be replaced');
// get a list of valid fieldnames from https://api.zotero.org/itemFields?pprint=1
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
var titles = [];
for (let id of ids) {
let item = Zotero.Items.get(id);
let title = item.getField('title');
if (title.includes("Text to be replaced")) {
let newtitle = title.replace("Text to be replaced", "New text");
titles.push(newtitle);
//item.setField('title', newtitle); // uncomment this during final run.
//await item.saveTx(); // uncomment this during final run
}
}
return titles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment