Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
dev:client_coding:javascript_api [2020/10/17 20:58] – [Batch Editing] dstillmandev:client_coding:javascript_api [2022/07/02 18:22] (current) – [Executing the search] dstillman
Line 36: Line 36:
 Non-window scope applies to lower-level code that doesn't have access to the DOM. This includes the core ''Zotero'' object, which contains all other non-window code, including the data layer used for retrieving and modifying library data. In Zotero, non-window code is contained within the ''xpcom'' subdirectory. Non-window scope applies to lower-level code that doesn't have access to the DOM. This includes the core ''Zotero'' object, which contains all other non-window code, including the data layer used for retrieving and modifying library data. In Zotero, non-window code is contained within the ''xpcom'' subdirectory.
  
-Overlays and windows in Zotero can import the core ''Zotero'' object via the include.js script. Zotero methods can then be called from anywhere within the window's scope simply by calling, for example, ''var item = Zotero.Items.get(1);''.+Windows in Zotero can import the core ''Zotero'' object via the include.js script. Zotero methods can then be called from anywhere within the window's scope simply by calling, for example, ''var item = Zotero.Items.get(1);''.
  
-To access Zotero functionality from your own extension, you will need access to the core ''Zotero'' object. If your extension operates within the main browser overlay, you already have access to the ''Zotero'' object and don’t need to take further steps. Otherwise, you can import the Zotero object into other windows by including the script %%chrome://zotero/content/include.js%% within an HTML or XUL file:+To access Zotero functionality from your own extension, you will need access to the core ''Zotero'' object. If your extension operates within the main Zotero window, you already have access to the ''Zotero'' object and don’t need to take further steps. Otherwise, you can import the Zotero object into other windows by including the script %%chrome://zotero/content/include.js%% within an HTML or XUL file:
  
 <code html> <code html>
Line 87: Line 87:
     ]     ]
 ); );
-var itemID = await item.save();+var itemID = await item.saveTx();
 return itemID; return itemID;
 </code> </code>
Line 216: Line 216:
 s.addCondition('collection', 'is', collectionKey); // e.g., 'C72FDAP2' s.addCondition('collection', 'is', collectionKey); // e.g., 'C72FDAP2'
 s.addCondition('savedSearch', 'is', savedSearchKey); s.addCondition('savedSearch', 'is', savedSearchKey);
 +</code>
 +
 +=== Search by creator === 
 +
 +<code javascript>
 +var name = 'smith';
 +s.addCondition('creator', 'contains', name);
 </code> </code>
  
Line 236: Line 243:
 results: results:
  
-<code javascript>var results = await s.search();</code>+<code javascript>var itemIDs = await s.search();</code>
  
 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>var items = await Zotero.Items.getAsync(results);</code>+<code javascript>var items = await Zotero.Items.getAsync(itemIDs);</code>
  
 ==== Managing citations and bibliographies ==== ==== Managing citations and bibliographies ====
Line 340: Line 347:
 return fulltext; return fulltext;
 </code> </code>
 +
 +==== File I/O ====
 +
 +=== Getting the contents of a file ===
 +
 +<code javascript>
 +var path = '/Users/user/Desktop/data.json';
 +var data = await Zotero.File.getContentsAsync(path);
 +</code>
 +
 +=== Saving data to a file ===
 +
 +<code javascript>
 +var path = '/Users/user/Desktop/file.txt';
 +var data = "This is some text.";
 +await Zotero.File.putContentsAsync(path, data);
 +</code>
 +
  
 ==== To Do === ==== To Do ===
Line 356: Line 381:
  
 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.
 +
 +All examples operate on the currently selected library.
  
 ==== Example: Item Field Changes ==== ==== Example: Item Field Changes ====
Line 367: Line 394:
 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, 'is', oldValue); s.addCondition(fieldName, 'is', oldValue);
 var ids = await s.search(); var ids = await s.search();
Line 397: Line 424:
    
 var s = new Zotero.Search(); var s = new Zotero.Search();
-s.libraryID = Zotero.Libraries.userLibraryID;+s.libraryID = ZoteroPane.getSelectedLibraryID();
 s.addCondition('creator', 'is', oldName); s.addCondition('creator', 'is', oldName);
 var ids = await s.search(); var ids = await s.search();
Line 422: Line 449:
 return ids.length + " item(s) updated";</code> return ids.length + " item(s) updated";</code>
  
-==== Example: Convert manual tag in selected library to automatic tag ====+==== Example: Convert Manual Tag to Automatic ==== 
 + 
 +Note that this will change all instances of the tag in the library.
  
 Replace "Foo" in the first line with the tag to change: Replace "Foo" in the first line with the tag to change:
Line 434: Line 463:
     return "No items found";     return "No items found";
 } }
-for (let id of ids) { +await Zotero.DB.executeTransaction(async function () { 
-    let item = Zotero.Items.get(id); +    for (let id of ids) { 
-    item.addTag(tag, 1); +        let item = Zotero.Items.get(id); 
-    await item.saveTx(); +        item.addTag(tag, 1); 
-}+        await item.save(
 +            skipDateModifiedUpdate: true 
 +        }); 
 +    } 
 +});
 return ids.length + " tag(s) updated";</code> return ids.length + " tag(s) updated";</code>
  
dev/client_coding/javascript_api.1602982731.txt.gz · Last modified: 2020/10/17 20:58 by dstillman