Created
November 9, 2023 17:29
-
-
Save zuphilip/1c9d0b91c796e3a5ccbd19f1d3355ad4 to your computer and use it in GitHub Desktop.
Zotero Script for moving custom field notes (e.g. imported from Citavi) to some field
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
// Move value from #CostumField notes with fixed label | |
// to a different field. Set these two fields according to your needs. | |
var label = "Signatur"; | |
var field = "callNumber"; | |
// List of all possible fields: | |
// https://api.zotero.org/itemFields?pprint=1 | |
var check = true; | |
var overwrite = false; | |
var items = Zotero.getActiveZoteroPane().getSelectedItems(); | |
function html2text(html) { | |
var tempDivElement = document.createElement("div"); | |
tempDivElement.innerHTML = html; | |
return tempDivElement.textContent || tempDivElement.innerText || ""; | |
} | |
for (let item of items) { | |
noteIDs = item.getNotes(); | |
for (let nid of noteIDs) { | |
let note = Zotero.Items.get(nid); | |
let skip = true; | |
for (let tag of note.getTags()) { | |
if (tag.tag == "#CostumField") { | |
skip = false; | |
break; | |
} | |
} | |
if (skip && check) { | |
continue; | |
} | |
var noteHTML = note.getNote(); | |
var noteText = html2text(noteHTML); | |
Zotero.log(noteText); | |
var parts = noteText.split(":"); | |
if (parts.length >= 2 && parts[0] == label) { | |
parts.shift(); | |
value = parts.join(":"); | |
var content = item.getField(field); | |
if (!content || overwrite) { | |
item.setField(field, value); | |
item.save(); | |
} | |
} | |
} | |
} | |
items; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a preview of its use: