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/01/22 04:23] – [The Zotero Object] dstillman | dev:client_coding:javascript_api [2019/06/11 03:44] – [Running Ad Hoc JavaScript in Zotero] dstillman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | |||
- | |||
====== Zotero JavaScript API ====== | ====== Zotero JavaScript API ====== | ||
Line 11: | Line 8: | ||
===== Running Ad Hoc JavaScript in Zotero ===== | ===== Running Ad Hoc JavaScript in Zotero ===== | ||
- | Zotero | + | Zotero |
- In the Advanced pane of the Zotero preferences, | - In the Advanced pane of the Zotero preferences, | ||
Line 20: | Line 17: | ||
In synchronous mode, the value of the final line will appear in the right-hand pane. | In synchronous mode, the value of the final line will appear in the right-hand pane. | ||
+ | ===== Zotero Code Architecture ===== | ||
- | (Before Zotero 5.0.61 is released, you'll need to install the [[: | + | ==== Window Scope vs. Non-Window Scope === |
- | ===== Window Scope vs. Non-Window Scope ==== | + | |
Zotero code exists in either window scope and non-window scope. | Zotero code exists in either window scope and non-window scope. | ||
Line 44: | Line 41: | ||
</ | </ | ||
(The Zotero pane will always be available unless the main window is closed, as is possible on macOS.) | (The Zotero pane will always be available unless the main window is closed, as is possible on macOS.) | ||
- | ===== Notification System ===== | ||
- | Zotero has a built-in notification system that allows other privileged code to be notified when a change is made via the data layer---for example, when an item is added to the library. Within Zotero itself, this is used mostly to update the UI when items change, but external | + | ==== Notification System ==== |
+ | |||
+ | Zotero has a built-in notification system that allows other privileged code to be notified when a change is made via the data layer — for example, when an item is added to the library. Within Zotero itself, this is used mostly to update the UI when items change, but extensions can use the system to perform additional operations when specific events occur — say, to sync item metadata with a web-based | ||
Available events: | Available events: | ||
Line 63: | Line 61: | ||
//The Zotero JavaScript API is under-documented, | //The Zotero JavaScript API is under-documented, | ||
- | |||
- | Once you have access to the core Zotero object, you can use the objects and methods provided by the Zotero JavaScript API. | ||
- | |||
- | Zotero uses a combination of instantiatable objects (e.g. '' | ||
==== Adding items and modifying data ==== | ==== Adding items and modifying data ==== | ||
- | A typical operation might include a call to '' | + | A typical operation might include a call to '' |
<code javascript> | <code javascript> | ||
Line 344: | Line 338: | ||
The JavaScript API can provide a powerful way to script changes to your Zotero library. The common case of search-and-replace is accomplished easily using a basic script. | The JavaScript API can provide a powerful way to script changes to your Zotero library. The common case of search-and-replace is accomplished easily using a basic script. | ||
- | First, install the [[https:// | + | Before proceeding, back up your [[:zotero_data|Zotero data directory]] and temporarily disable auto-sync in the Sync pane of the Zotero preferences. |
- | + | ||
- | In Execute JS, switch the target window to an open browser window, paste the relevant code into the " | + | |
==== Example: Item Field Changes ==== | ==== Example: Item Field Changes ==== | ||
Line 357: | Line 349: | ||
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.addCondition(fieldName, | s.addCondition(fieldName, | ||
- | var ids = s.search(); | + | var ids = await s.search(); |
- | if (ids) { | + | if (!ids.length) { |
- | for(var i in ids) { | + | |
- | var item = Zotero.Items.get(ids[i]); | + | |
- | var mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, | + | |
- | item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue); | + | |
- | item.save(); | + | |
- | } | + | |
- | alert(ids.length + " items updated"); | + | |
} | } | ||
- | else { | + | await Zotero.DB.executeTransaction(async function () { |
- | alert("No items found"); | + | |
- | }</ | + | let item = await Zotero.Items.getAsync(id); |
+ | let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, | ||
+ | item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue); | ||
+ | await item.save(); | ||
+ | | ||
+ | }); | ||
+ | return ids.length + " item(s) updated"; | ||
+ | |||
+ | The list of field names to use can be retrieved via the web API: | ||
- | The list of field names to use can be retrieved via the server API: https:// | + | https:// |
==== Example: Delete Tags By Name ==== | ==== Example: Delete Tags By Name ==== | ||
+ | |||
+ | < | ||
<code javascript> | <code javascript> | ||
Line 390: | Line 387: | ||
==== Example: Delete Tags By Part of Name ==== | ==== Example: Delete Tags By Part of Name ==== | ||
+ | |||
+ | < | ||
<code javascript> | <code javascript> |