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/03/30 00:34] – [Batch Editing] Add creator editing example dstillman | dev:client_coding:javascript_api [2020/10/17 21:03] – [Batch Editing] dstillman | ||
---|---|---|---|
Line 11: | Line 11: | ||
- In the Tools → Developer menu, select Run JavaScript. Opening the Error Console, which appears in the same menu, will also be helpful. | - In the Tools → Developer menu, select Run JavaScript. Opening the Error Console, which appears in the same menu, will also be helpful. | ||
- | - In the window that opens, enter JavaScript in the Code textbox and click Run or press Cmd-R/ | + | - In the window that opens, enter some JavaScript in the Code textbox and click Run or press Cmd-R/ |
When running **asynchronous** code containing '' | When running **asynchronous** code containing '' | ||
Line 20: | Line 20: | ||
</ | </ | ||
- | In **synchronous** mode, the value of the final line will appear in the right-hand pane. The same result as above could be achieved in synchonous | + | In **synchronous** mode, the value of the final line will appear in the right-hand pane. The same result as above could be achieved in synchronous |
<code javascript> | <code javascript> | ||
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 (tags.indexOf(allTags[id].name.toLowerCase()) | + | if (!ids.length) { |
- | | + | |
- | | + | |
} | } | ||
- | Zotero.Tags.erase(ids); | + | for (let id of ids) { |
- | </ | + | let item = Zotero.Items.get(id); |
+ | | ||
+ | await item.saveTx(); | ||
+ | } | ||
+ | return ids.length + " tag(s) updated"; | ||
==== Example: Delete Tags By Part of Name ==== | ==== Example: Delete Tags By Part of Name ==== |