Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:client_coding:javascript_api [2019/06/11 03:44] – [Running Ad Hoc JavaScript in Zotero] dstillman | dev:client_coding:javascript_api [2024/08/21 03:55] (current) – [Zotero JavaScript API] dstillman | ||
---|---|---|---|
Line 3: | Line 3: | ||
Whereas Zotero' | Whereas Zotero' | ||
- | Note that the (mostly user-contributed) | + | Note that this documentation of the JavaScript API is not comprehensive. If you use the JavaScript API in ways beyond what's described here, please consider expanding this wiki page or suggesting changes in the forums. |
- | + | ||
===== Running Ad Hoc JavaScript in Zotero ===== | ===== Running Ad Hoc JavaScript in Zotero ===== | ||
Zotero includes an option to run arbitrary privileged JavaScript: | Zotero includes an option to run arbitrary privileged JavaScript: | ||
- | | + | - In the Tools → Developer |
- | | + | - In the window that opens, enter some JavaScript in the Code textbox and click Run or press Cmd-R/ |
- | - In the window that opens, enter JavaScript in the Code textbox and click Run or press Cmd-R/ | + | |
- | To run asynchronous code, check the "Run as async function" | + | When running **asynchronous** code containing '' |
- | In synchronous mode, the value of the final line will appear in the right-hand pane. | + | <code javascript> |
+ | var items = Zotero.getActiveZoteroPane().getSelectedItems(); | ||
+ | return items; | ||
+ | </ | ||
+ | |||
+ | 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 mode with | ||
+ | |||
+ | <code javascript> | ||
+ | var items = Zotero.getActiveZoteroPane().getSelectedItems(); | ||
+ | items; | ||
+ | </ | ||
===== Zotero Code Architecture ===== | ===== Zotero Code Architecture ===== | ||
Line 27: | Line 34: | ||
Non-window scope applies to lower-level code that doesn' | Non-window scope applies to lower-level code that doesn' | ||
- | Overlays and windows | + | Windows |
- | To access Zotero functionality from your own extension, you will need access to the core '' | + | To access Zotero functionality from your own extension, you will need access to the core '' |
<code html> | <code html> | ||
Line 78: | Line 85: | ||
] | ] | ||
); | ); | ||
- | var itemID = await item.save(); | + | var itemID = await item.saveTx(); |
return itemID; | return itemID; | ||
</ | </ | ||
Line 207: | Line 214: | ||
s.addCondition(' | s.addCondition(' | ||
s.addCondition(' | s.addCondition(' | ||
+ | </ | ||
+ | |||
+ | === Search by creator === | ||
+ | |||
+ | <code javascript> | ||
+ | var name = ' | ||
+ | s.addCondition(' | ||
</ | </ | ||
Line 227: | Line 241: | ||
results: | results: | ||
- | <code javascript> | + | <code javascript> |
This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | ||
- | <code javascript> | + | <code javascript> |
- | ===== Managing citations and bibliographies | + | ==== Managing citations and bibliographies ==== |
TODO: this is pretty sparse. | TODO: this is pretty sparse. | ||
- | ==== Getting a bibliography for an array of items: | + | === Getting a bibliography for an array of items: === |
Here we use Zotero' | Here we use Zotero' | ||
- | specified in Zotero' | + | specified in Zotero' |
+ | bibliography from all currently selected items. | ||
- | First we start with a list of as in the previous entry. | + | <code javascript> |
- | + | var qc = Zotero.QuickCopy; | |
- | <code javascript> | + | var format |
- | var biblio | + | if (format.split(" |
- | var biblio_html_format = cite.html; | + | |
- | var biblio_txt | + | } |
+ | var biblio = qc.getContentFromItems(items, | ||
+ | var biblio_html_format = biblio.html; | ||
+ | var biblio_txt = biblio.text; | ||
</ | </ | ||
- | ==== Get a list of available styles | + | If you instead want to have the citation string then simply replace the 7th |
+ | line with '' | ||
+ | |||
+ | |||
+ | === Get a list of available styles === | ||
<code javascript> | <code javascript> | ||
Line 262: | Line 284: | ||
</ | </ | ||
- | TODO: get citations. | + | TODO: change the style. |
especially RTF | especially RTF | ||
Line 323: | Line 345: | ||
return fulltext; | return fulltext; | ||
</ | </ | ||
+ | |||
+ | ==== File I/O ==== | ||
+ | |||
+ | === Getting the contents of a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = await Zotero.File.getContentsAsync(path); | ||
+ | </ | ||
+ | |||
+ | === Saving data to a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = "This is some text."; | ||
+ | await Zotero.File.putContentsAsync(path, | ||
+ | </ | ||
+ | |||
==== To Do === | ==== To Do === | ||
Line 336: | Line 376: | ||
===== Batch Editing ===== | ===== Batch Editing ===== | ||
- | The JavaScript API can provide | + | The JavaScript API provides |
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 350: | Line 392: | ||
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 370: | Line 412: | ||
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 to Automatic ==== | ||
+ | |||
+ | Note that this will change all instances of the tag in the library. | ||
+ | |||
+ | 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); | + | await Zotero.DB.executeTransaction(async function () { |
- | </ | + | for (let id of ids) { |
+ | let item = Zotero.Items.get(id); | ||
+ | | ||
+ | await item.save({ | ||
+ | skipDateModifiedUpdate: | ||
+ | }); | ||
+ | } | ||
+ | }); | ||
+ | return ids.length + " tag(s) updated"; | ||
==== Example: Delete Tags By Part of Name ==== | ==== Example: Delete Tags By Part of Name ==== |