Last active
December 15, 2019 19:31
-
-
Save zuphilip/6357475314a600d2dd92fb12245d1d0e to your computer and use it in GitHub Desktop.
JS-Script for a report about the values of a simple field over all items of the currently selected collection in Zotero or your whole Zotero library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Change the constant 'field' to any simple Zotero field | |
(see https://api.zotero.org/itemFields?pprint=1 for a list) | |
and run the script as async funtion to see a report | |
of the currently selected collection or your complete | |
library. [License: CC0] | |
*/ | |
const field = "language"; | |
var collection = ZoteroPane.getSelectedCollection(); | |
var observables = []; | |
if (collection) { | |
var items = collection.getChildItems(); | |
observables = items.map(item => item.getField(field)); | |
} | |
else { | |
var s = new Zotero.Search(); | |
s.libraryID = ZoteroPane.getSelectedLibraryID(); | |
s.addCondition('noChildren', 'true') | |
var itemIDs = await s.search(); | |
for (let id of itemIDs) { | |
let item = Zotero.Items.get(id); | |
observables[id] = item.getField(field); | |
} | |
} | |
count = {} | |
for (let obs of observables) { | |
if (obs in count) { | |
count[obs]++; | |
} | |
else { | |
count[obs] = 1; | |
} | |
} | |
// sort the keys and create a new object in | |
// this order to be output | |
var sortedKeys = Object.keys(count).sort(function (a, b) { | |
return a.toLowerCase().localeCompare(b.toLowerCase()); | |
}); | |
var countSorted = {}; | |
for (let key of sortedKeys) { | |
countSorted[key] = count[key]; | |
} | |
return countSorted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment