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 [2019/10/20 06:46] – [API Methods] zuphilip | dev:client_coding:javascript_api [2020/10/17 20:58] – [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 353: | Line 353: | ||
===== Batch Editing ===== | ===== Batch Editing ===== | ||
- | The JavaScript API can provide | + | The JavaScript API provides |
Before proceeding, back up your [[: | Before proceeding, back up your [[: | ||
Line 387: | Line 387: | ||
https:// | https:// | ||
- | ==== Example: | + | ==== Example: |
- | < | + | Edit the first four lines as necessary: |
- | <code javascript> | + | <code javascript> |
- | var ids = []; | + | var newFirstName = "Robert"; |
- | var allTags | + | var newLastName = "Smith"; |
- | tags = tags.map(tag => tag.toLowerCase()); | + | var newFieldMode |
- | for (var id in allTags) { | + | |
- | | + | var s = new Zotero.Search(); |
- | ids.push(id); | + | s.libraryID |
+ | s.addCondition(' | ||
+ | var ids = await s.search(); | ||
+ | if (!ids.length) { | ||
+ | return "No items found"; | ||
+ | } | ||
+ | await Zotero.DB.executeTransaction(async function () { | ||
+ | | ||
+ | let item = await Zotero.Items.getAsync(id); | ||
+ | let creators = item.getCreators(); | ||
+ | let newCreators = []; | ||
+ | for (let creator of creators) { | ||
+ | if (`${creator.firstName} ${creator.lastName}`.trim() == oldName) { | ||
+ | | ||
+ | creator.lastName = newLastName; | ||
+ | creator.fieldMode = newFieldMode; | ||
+ | } | ||
+ | newCreators.push(creator); | ||
+ | } | ||
+ | item.setCreators(newCreators); | ||
+ | await item.save(); | ||
} | } | ||
+ | }); | ||
+ | return ids.length + " item(s) updated";</ | ||
+ | |||
+ | ==== Example: Convert manual tag in selected library to automatic tag ==== | ||
+ | |||
+ | Replace " | ||
+ | |||
+ | <code javascript> | ||
+ | var s = new Zotero.Search(); | ||
+ | s.libraryID = ZoteroPane.getSelectedLibraryID(); | ||
+ | s.addCondition(' | ||
+ | var ids = await s.search(); | ||
+ | if (!ids.length) { | ||
+ | return "No items found"; | ||
} | } | ||
- | 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 ==== |