Created
September 10, 2023 20:41
-
-
Save the-solipsist/1f7deba5ccc3e36e6d54096748538f35 to your computer and use it in GitHub Desktop.
Zotero search and replace javascript
This file contains hidden or 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
// 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