-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.js
52 lines (49 loc) · 2.21 KB
/
set.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// see https://www.zotero.org/support/dev/client_coding/javascript_api#batch_editing
// From Dan Stillman at https://forums.zotero.org/discussion/80506/
// first make a backup of zotero.sqlite in your Zotero data directory with Zotero closed and then go to Tools → Developer → Run JavaScript
// combined with code from https://forums.zotero.org/discussion/7707/find-and-replace-on-multiple-items/p2
// by tanaree https://forums.zotero.org/profile/1648161/tanaree April 17, 2014
// For creators, sample JSON is [{"fieldMode":0,"firstName":"Israel","lastName":"Knohl","creatorTypeID":1}]
// so the regExFil should include the colon and double quotes like /"firstName":"Ken(neth)?"/gm
// and the replaceText should be in single quotes and double quotes like '"firstName":"Ken M."'
var totalChanges = 0;
var f,c="";
fieldName=prompt("field name (or blank to cancel):");
if (fieldName.length>0) {
var changes = 0;
var oldValue = "";
var fieldID = Zotero.ItemFields.getID(fieldName);
var items = Zotero.getActiveZoteroPane().getSelectedItems();
await Zotero.DB.executeTransaction(async function () {
switch (fieldName) {
case "creator":
oldValue = JSON.stringify(items[0].getCreators());
break;
default:
oldValue = items[0].getField(fieldName);
}
newValue=prompt("set "+fieldName+" to:",oldValue);
for (let item of items) {
switch (fieldName) {
case "creator":
oldValue = JSON.stringify(item.getCreators());
break;
default:
oldValue = item.getField(fieldName);
}
if (newValue != oldValue) {
changes++;
switch (fieldName) {
case "creator":
item.setCreators(JSON.parse(newValue));
break;
default:
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
}
await item.save();
}
}
});
}
return changes + " item(s) modified";