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
Next revisionBoth sides next revision
dev:client_coding:javascript_api [2020/01/18 19:35] – [Batch Editing] dstillmandev:client_coding:javascript_api [2020/10/17 20:58] – [Batch Editing] dstillman
Line 11: Line 11:
  
   - 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/Ctrl-R.+  - In the window that opens, enter some 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. E.g. returning the currently selected item(s) 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)
Line 20: Line 20:
 </code> </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+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> <code javascript>
Line 387: Line 387:
 https://api.zotero.org/itemFields?pprint=1. https://api.zotero.org/itemFields?pprint=1.
  
-==== Example: Delete Tags By Name ====+==== Example: Creator Name Changes ====
  
-<html><p id="zotero-5-update-warning" style="colorred; font-weight: bold">This example has not been updated for Zotero 5 and should not currently be used.</p></html>+Edit the first four lines as necessary:
  
-<code javascript>var tags ["foo""bar""baz"]+<code javascript>var oldName = "Robert L. Smith"
-var ids []+var newFirstName = "Robert"
-var allTags = Zotero.Tags.search(); +var newLastName = "Smith"; 
-tags tags.map(tag => tag.toLowerCase()); +var newFieldMode 0// 0: two-field, 1: one-field (with empty first name) 
-for (var id in allTags) { +  
-    if (tags.indexOf(allTags[id].name.toLowerCase()) !-1) { +var new Zotero.Search(); 
-      ids.push(id);+s.libraryID Zotero.Libraries.userLibraryID; 
 +s.addCondition('creator', 'is', oldName); 
 +var ids await s.search()
 +if (!ids.length
 +    return "No items found"
 +
 +await Zotero.DB.executeTransaction(async function () { 
 +    for (let id of ids) { 
 +        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.firstName = newFirstName; 
 +        creator.lastName = newLastName; 
 +        creator.fieldMode = newFieldMode; 
 +        } 
 +        newCreators.push(creator); 
 +        } 
 +        item.setCreators(newCreators); 
 +        await item.save();
     }     }
 +});
 +return ids.length + " item(s) updated";</code>
 +
 +==== Example: Convert manual tag in selected library to automatic tag ====
 +
 +Replace "Foo" in the first line with the tag to change:
 +
 +<code javascript>var tag = "Foo";
 +var s = new Zotero.Search();
 +s.libraryID = ZoteroPane.getSelectedLibraryID();
 +s.addCondition('tag', 'is', tag);
 +var ids = await s.search();
 +if (!ids.length) {
 +    return "No items found";
 } }
-Zotero.Tags.erase(ids); +for (let id of ids) { 
-</code>+    let item = Zotero.Items.get(id); 
 +    item.addTag(tag, 1); 
 +    await item.saveTx(); 
 +
 +return ids.length + " tag(s) updated";</code>
  
 ==== Example: Delete Tags By Part of Name ==== ==== Example: Delete Tags By Part of Name ====
dev/client_coding/javascript_api.txt · Last modified: 2022/07/02 18:22 by dstillman