Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
dev:client_coding:javascript_api [2020/04/16 16:03] – [Running Ad Hoc JavaScript in Zotero] dstillman | dev:client_coding:javascript_api [2020/10/17 21:03] – [Batch Editing] dstillman | ||
---|---|---|---|
Line 356: | Line 356: | ||
Before proceeding, back up your [[: | Before proceeding, back up your [[: | ||
+ | |||
+ | All examples operate on the currently selected library. | ||
==== Example: Item Field Changes ==== | ==== Example: Item Field Changes ==== | ||
Line 367: | Line 369: | ||
var fieldID = Zotero.ItemFields.getID(fieldName); | var fieldID = Zotero.ItemFields.getID(fieldName); | ||
var s = new Zotero.Search(); | var s = new Zotero.Search(); | ||
- | s.libraryID = Zotero.Libraries.userLibraryID; | + | s.libraryID = ZoteroPane.getSelectedLibraryID(); |
s.addCondition(fieldName, | s.addCondition(fieldName, | ||
var ids = await s.search(); | var ids = await s.search(); | ||
Line 397: | Line 399: | ||
var s = new Zotero.Search(); | var s = new Zotero.Search(); | ||
- | s.libraryID = Zotero.Libraries.userLibraryID; | + | s.libraryID = ZoteroPane.getSelectedLibraryID(); |
s.addCondition(' | s.addCondition(' | ||
var ids = await s.search(); | var ids = await s.search(); | ||
Line 422: | Line 424: | ||
return ids.length + " item(s) updated";</ | return ids.length + " item(s) updated";</ | ||
- | ==== Example: | + | ==== Example: |
- | < | + | Replace |
- | <code javascript> | + | <code javascript> |
- | var ids = []; | + | var s = new Zotero.Search(); |
- | var allTags | + | s.libraryID |
- | tags = tags.map(tag => tag.toLowerCase()); | + | s.addCondition('tag', ' |
- | for (var id in allTags) { | + | var ids = await s.search(); |
- | | + | if (!ids.length) { |
- | ids.push(id); | + | return "No items found"; |
- | } | + | } |
+ | for (let id of ids) { | ||
+ | | ||
+ | item.addTag(tag, 1); | ||
+ | await item.saveTx(); | ||
} | } | ||
- | Zotero.Tags.erase(ids); | + | return ids.length + " tag(s) updated" |
- | </ | + | |
==== Example: Delete Tags By Part of Name ==== | ==== Example: Delete Tags By Part of Name ==== |