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/10/20 06:32] – [Managing citations and bibliographies] zuphilip | 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 ===== | ||
Line 11: | Line 9: | ||
- 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 18: | ||
</ | </ | ||
- | 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 36: | 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 87: | Line 85: | ||
] | ] | ||
); | ); | ||
- | var itemID = await item.save(); | + | var itemID = await item.saveTx(); |
return itemID; | return itemID; | ||
</ | </ | ||
Line 216: | Line 214: | ||
s.addCondition(' | s.addCondition(' | ||
s.addCondition(' | s.addCondition(' | ||
+ | </ | ||
+ | |||
+ | === Search by creator === | ||
+ | |||
+ | <code javascript> | ||
+ | var name = ' | ||
+ | s.addCondition(' | ||
</ | </ | ||
Line 236: | 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 ==== | ||
Line 249: | Line 254: | ||
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 = Zotero.Prefs.get(" | var format = Zotero.Prefs.get(" | ||
if (format.split(" | if (format.split(" | ||
- | return | + | alert("No bibliography style is choosen in the settings for QuickCopy." |
} | } | ||
- | var biblio = qc.getContentFromItems([item], format); | + | var biblio = qc.getContentFromItems(items, format); |
var biblio_html_format = biblio.html; | var biblio_html_format = biblio.html; | ||
var biblio_txt = biblio.text; | var biblio_txt = biblio.text; | ||
</ | </ | ||
+ | |||
+ | If you instead want to have the citation string then simply replace the 7th | ||
+ | line with '' | ||
+ | |||
=== Get a list of available styles === | === Get a list of available styles === | ||
Line 275: | Line 284: | ||
</ | </ | ||
- | TODO: get citations. | + | TODO: change the style. |
especially RTF | especially RTF | ||
Line 336: | 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 349: | 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 363: | 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 383: | 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 ==== |