Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
dev:client_coding:javascript_api [2019/08/05 05:54]
dstillman [Running Ad Hoc JavaScript in Zotero]
dev:client_coding:javascript_api [2020/01/18 19:35] (current)
dstillman [Batch Editing]
Line 13: Line 13:
   - In the window that opens, enter JavaScript in the Code textbox and click Run or press Cmd-R/​Ctrl-R.   - In the window that opens, enter JavaScript in the Code textbox and click Run or press Cmd-R/​Ctrl-R.
  
-When running asynchronous code containing ''​await'',​ the entered code is wrapped in an [[https://​developer.mozilla.org/​en-US/​docs/​Web/​JavaScript/​Reference/​Statements/​async_function|async function]], allowing you to wait for the resolution of promises returned by functions. Most Zotero functions that access the database, disk, or network are asynchronous. In this mode, the value of a ''​return''​ statement will be displayed in the right-hand pane upon successful completion.+When running ​**asynchronous** code containing ''​await'',​ the entered code is wrapped in an [[https://​developer.mozilla.org/​en-US/​docs/​Web/​JavaScript/​Reference/​Statements/​async_function|async function]], allowing you to wait for the resolution of promises returned by functions. Most Zotero functions that access the database, disk, or network are asynchronous. In this mode, the value of a ''​return''​ statement will be displayed in the right-hand pane upon successful completion. ​E.g. returning the currently selected item(s)
  
-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; 
 +</​code>​ 
 + 
 +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 mode with 
 + 
 +<code javascript>​ 
 +var items = Zotero.getActiveZoteroPane().getSelectedItems();​ 
 +items; 
 +</​code>​
 ===== Zotero Code Architecture ===== ===== Zotero Code Architecture =====
  
Line 232: Line 242:
 <code javascript>​var items = await Zotero.Items.getAsync(results);</​code>​ <code javascript>​var items = await Zotero.Items.getAsync(results);</​code>​
  
-===== Managing citations and bibliographies ​=====+==== Managing citations and bibliographies ====
  
 TODO:  this is pretty sparse. ​ the rtfscan code is a good place to look for some guidance. TODO:  this is pretty sparse. ​ the rtfscan code is a good place to look for some guidance.
  
-==== Getting a bibliography for an array of items: ​====+=== Getting a bibliography for an array of items: ===
  
 Here we use Zotero'​s Quick Copy functions to get a bibliography in the style Here we use Zotero'​s Quick Copy functions to get a bibliography in the style
-specified in Zotero'​s preferences.+specified in Zotero'​s preferences. We will in the following example create this 
 +bibliography from all currently selected items.
  
-First we start with a list of as in the previous entry. +<code javascript>​var items = Zotero.getActiveZoteroPane().getSelectedItems();​ 
- +var qc = Zotero.QuickCopy;​ 
-<code javascript>​var qc = Zotero.QuickCopy;​ +var format ​= Zotero.Prefs.get("​export.quickCopy.setting"​)
-var biblio ​qc.getContentFromItems([item], ​Zotero.Prefs.get("​export.quickCopy.setting"​));​ +if (format.split("​="​)[0] !== "​bibliography"​) { 
-var biblio_html_format = cite.html; +   ​alert("​No bibliography style is choosen in the settings for QuickCopy."​);​ 
-var biblio_txt ​ cite.text; ​+
 +var biblio = qc.getContentFromItems(items,​ format); 
 +var biblio_html_format = biblio.html; 
 +var biblio_txt = biblio.text;
 </​code>​ </​code>​
  
-==== Get a list of available styles ​====+If you instead want to have the citation string then simply replace the 7th 
 +line with ''​var biblio ​qc.getContentFromItems(items,​ format, null, true);''​. 
 + 
 + 
 +=== Get a list of available styles ===
  
 <code javascript> ​ <code javascript> ​
Line 261: Line 279:
 </​code>​ </​code>​
  
-TODO:  get citations. ​ ​change the style. ​ get stuff in other formats,+TODO:  change the style. ​ get stuff in other formats,
 especially RTF especially RTF
  
Line 335: Line 353:
 =====  Batch Editing ===== =====  Batch Editing =====
  
-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 provides ​a powerful way to script changes to your Zotero library. The common case of search-and-replace is accomplished easily using a basic script ​in the [[#​running_ad_hoc_javascript_in_zotero|JavaScript runner]].
  
 Before proceeding, back up your [[:​zotero_data|Zotero data directory]] and temporarily disable auto-sync in the Sync pane of the Zotero preferences. Before proceeding, back up your [[:​zotero_data|Zotero data directory]] and temporarily disable auto-sync in the Sync pane of the Zotero preferences.
dev/client_coding/javascript_api.1564998895.txt.gz · Last modified: 2019/08/05 05:54 by dstillman