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 [2019/10/20 06:46] – [API Methods] zuphilipdev:client_coding:javascript_api [2020/03/30 00:34] – [Batch Editing] Add creator editing example dstillman
Line 353: 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.
Line 386: Line 386:
  
 https://api.zotero.org/itemFields?pprint=1. https://api.zotero.org/itemFields?pprint=1.
 +
 +==== Example: Creator Name Changes ====
 +
 +Edit the first four lines as necessary:
 +
 +<code javascript>var oldName = "Robert L. Smith";
 +var newFirstName = "Robert";
 +var newLastName = "Smith";
 +var newFieldMode = 0; // 0: two-field, 1: one-field (with empty first name)
 + 
 +var s = new Zotero.Search();
 +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: Delete Tags By Name ==== ==== Example: Delete Tags By Name ====
dev/client_coding/javascript_api.txt · Last modified: 2022/07/02 18:22 by dstillman