How to import some hundreds notes in Zotero
Hi,
how can I import some hundreds of sorted notes in Zotero?
I have a Zotero collection, provided with DOI’s, but without notes. From a xls-file with my old bibliography, I exported a txt-file with two columns, tabs-separated. The first column contains DOI’s, the second column has the corresponding notes. The DOI’s in Zotero collection and in the txt-file are practically the same. I use Zotero 7.0.9, Windows 10.
Here a sample of the txt-entries:
https://doi.org/10.1013/1.12345 y132 UHV-Koffer und Probehalter
https://doi.org/10.1023/1.78912 a134 N2 + O2 Plasma
https://doi.org/10.1002/35-667/14/10/402 y135 XYZ-Tisch mit Keramik
From the xls-file, I could export the list in another format, if it would match better.
how can I import some hundreds of sorted notes in Zotero?
I have a Zotero collection, provided with DOI’s, but without notes. From a xls-file with my old bibliography, I exported a txt-file with two columns, tabs-separated. The first column contains DOI’s, the second column has the corresponding notes. The DOI’s in Zotero collection and in the txt-file are practically the same. I use Zotero 7.0.9, Windows 10.
Here a sample of the txt-entries:
https://doi.org/10.1013/1.12345 y132 UHV-Koffer und Probehalter
https://doi.org/10.1023/1.78912 a134 N2 + O2 Plasma
https://doi.org/10.1002/35-667/14/10/402 y135 XYZ-Tisch mit Keramik
From the xls-file, I could export the list in another format, if it would match better.
Something like:
1. Load the file:
var contents = Zotero.File.getContents(path);
2. Parse the columns into an array of DOI/note pairs.
3. Iterate over the array and use Zotero.Search to find the item matching each DOI (stripping the prefix).
4. Add a child note to each parent item:
var note = new Zotero.Item("note");
note.parentID = item.id;
note.setNote(text);
yield note.saveTx();
I have zero experience with Java, only made some macros in Visual Basic and a little practice in HTML.
I would try your advice with AI and post the results if they work.
the error message "Uncaught (in promise) TypeError: Zotero.search is not a function" .
Here the script:
async function importNotes() {
try {
// Path to the TXT file containing DOIs and notes
const filePath = "D:\\XXXX\\import\\2411-DOI-test1.txt";
// Load the contents of the TXT file
const contents = Zotero.File.getContents(filePath);
// Split the contents into lines and create an array of objects with DOI and note
const lines = contents.split('\n');
const doiNotes = lines.map(line => {
const [doi, note] = line.split('\t');
return { doi, note };
});
// Iterate over each DOI-note pair
doiNotes.forEach(async ({ doi, note }) => {
// Search for the Zotero item based on the DOI (without prefix)
const searchResults = await Zotero.search('item.DOI:"' + doi.replace('https://doi.org/', '') + '"');
// If an item is found, add a note to it
if (searchResults.length > 0) {
const item = searchResults[0];
const newNote = new Zotero.Item("note");
newNote.parentID = item.id;
newNote.setNote(note);
await newNote.saveTx();
console.log(`Note added to item with DOI: ${doi}`);
} else {
console.log(`No item found for DOI: ${doi}`);
}
});
} catch (error) {
console.error("An error occurred:", error);
// You can add more actions here, e.g., display an error message
}
}
importNotes();
I made the script, and it does work! My 500 notes were imported in 30 sec.
It is not really smart nor correct. For example, it generates hundreds of wrong console messages. It has two nested loops instead of Zotero.Search. Still did not understand how the search works.
async function importNotesInSelectedCollection() {
try {
//before start the script, the items must be selected with mouse or ctrl+A
const selectedItems = ZoteroPane.getSelectedItems();
// Path to the TXT file
const filePath = "D:\\XXXX\\2411-DOI-test1.txt";
// Load the contents of the TXT file
const contents = Zotero.File.getContents(filePath);
// Split the contents into lines and create an array of objects with DOI and note
const lines = contents.split('\n');
const doiNotes = lines.map(line => {
const [tdoi, tnote] = line.split('\t');
return { tdoi, tnote };
});
// Iterate over each DOI-note pair
for (const { tdoi, tnote } of doiNotes) {
for (let i = 0; i < selectedItems.length; i++) {
let itemm = selectedItems[i];
let itemmDOI = itemm.getField('DOI');
// Compare the DOI from the TXT file with the DOI of each selected item.
if (itemm.isRegularItem() && itemmDOI == tdoi) {
let nnote = new Zotero.Item('note');
nnote.parentID = itemm.id;
nnote.setNote(tnote);
await nnote.saveTx();
console.log(`Note added to item with DOI ${tdoi}.`);
} else {
console.log(`No item found for DOI ${tdoi}.`);
}
}
}
} catch (error) {
console.error("An error occurred:", error);
}
}
importNotesInSelectedCollection();