what javascript would I run to obtain the key for every note in my library?
Hi,
I've been working from the javascript API documentation and I haven't been able to find / understand how to get the key for every note in my library?
The docs detail how to get child notes for an item if I already have the parent key, but not how to check if a parent key has a note attached to it.
Apologies if this is a basic javascript question, I'm just starting to figure this stuff out. Happy to experiment with suggestions and report back when successful if you have time to reply with some ideas.
ultimately I'd like to automatically search every note in my library for instances of "[text]" containing specific keywords and print them as a new .tex file with some basic document template materials.
Thanks in advance,
Ezra
I've been working from the javascript API documentation and I haven't been able to find / understand how to get the key for every note in my library?
The docs detail how to get child notes for an item if I already have the parent key, but not how to check if a parent key has a note attached to it.
Apologies if this is a basic javascript question, I'm just starting to figure this stuff out. Happy to experiment with suggestions and report back when successful if you have time to reply with some ideas.
ultimately I'd like to automatically search every note in my library for instances of "[text]" containing specific keywords and print them as a new .tex file with some basic document template materials.
Thanks in advance,
Ezra
var s = new Zotero.Search();
s.libraryID = Zotero.Libraries.userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('recursive', 'true');
s.addCondition('noChildren', 'true');
s.addCondition('includeParentsAndChildren', 'true');
var tagName = 'Finished';
s.addCondition('tag', 'is', tagName);
var results = await s.search();
var items = await Zotero.Items.getAsync(results);
----
all it returns is "===>undefined<=== (undefined)" even thought there are plenty of items with the "Finished" tag in my library.
Assuming you're running it in the javascript runner, this should work:
return (await Zotero.Items.getAll(Zotero.Libraries.userLibraryID)).filter(item => item.isNote()).map(item => item.key)
undefined
, and that's what you're seeing.Ezra
Now I just have to search the content of the notes for those keywords and parse out the quotes they were imported within.
I tried to adapt the example in the docs to load all the note contents into a string and print that:
var r = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID).filter(item => item.isNote()).map(item => item.key)
//return r
for (let id of r) {
let note = Zotero.Items.get(id);
let noteHTML = note.getNote();
}
return noteHTML
but the only output is: TypeError: note.getNote is not a function
var noteHTML = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID)
.filter(item => item.isNote())
.map(item => item.getNote());
return noteHTML.join('\n\n------------------------\n\n');
Unfortunately, even just trying to add a search parameter, eg:
var noteHTML = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID)
.filter(item => item.isNote());
s.addCondition(fieldName, 'has', "extracted");
.map(item => item.getNote());
return noteHTML.join('\n\n------------------------\n\n');
returns:
SyntaxError: expected expression, got '.'
Is there a specific part of the source code I should be looking at to find the syntax for this kind of search? I've been looking at /chrome/content/zotero/xpcom/data/search.js but I can't find any instance of ".filter(item => [...]" there.
.map
calls map on nothing. map must be called on an array.But in a more general sense you're mixing two approaches here. One just calls up all items in the library and uses regular javascript code to filter them.
s.addCondition
(withs
undefined BTW) uses the Zotero search facilities.TBH it'd be easier for us to write the full code if you explain what you want rather than guide you through the Zotero internals. But even if you want to explore Zotero to get a better understanding it'd be best to have a clear idea on what you want to achieve first.
Another aside: writing a
.tex
file is in itself not entirely trivial given that you are looking to write the note text (which is HTML), and you might be better off writing as HTML and let pandoc convert it to tex.BibTeX.js
translator), something like this should do the job (untested though):{
"translatorID": "a10da689-df6d-4951-8f6d-add746611577",
"label": "Notes",
"creator": "",
"target": ".html",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 2,
"lastUpdated": "2021-04-27 18:28:36"
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function doExport() {
let item
while (item = Zotero.nextItem()) {
if (!item.notes) continue
for (note of item.notes.filter(note => note.note.includes(Zotero.getHiddenPref('note-search.term')))) {
Zotero.write(`<h1>${escapeHtml(item.citationKey)}</h1>\n`)
Zotero.write(note.note)
}
}
}
I do have BBT installed.
I'm trying to code an annotation scanner that will compile all the quotes and comments in a note that are flagged by a # and will produce a .tex file that contains the corresponding quotes, proper \cite commands, and comments, ordered by year of publication.
eg, if I have: in one note, for the parent item with the bibkey "Dewar 2009," and, in another note for the parent item with the bibkey "Gell 1998" I would like the desired code to, when running for "#reverseeng", return something like:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{biblatex}
\addbibresource{library.bib}
\begin{document}
``Style, I argue, is 'relations between relations' of forms.'' \cite[237]{gell1998}
good definition of style
``In other words, the artists attempted a kind of ``reverse engineering"'' of traditional conceptions of community formation.'' \cite[127]{Dewar2009}
a reverse engineering of community through the development of capital's folk music
\printbibliography
\end{document}]
I did manage to get the runner to print to print the output of dstillman's code to a txt file that already exists, with:
var noteHTML = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID)
.filter(item => item.isNote())
.map(item => item.getNote());
var data = noteHTML.join('\n\n------------------------\n\n');
var path = 'targetfilepath';
await Zotero.File.putContentsAsync(path, data);
Hopefully this clarifies my objectives. I'm sure there's more than one approach for this, and I'm still trying to figure out the cleanest way to have it retrieve the bibkey along with the desired quotes. I'll work with the code you just provided (I think you're right that a translator for html to pandoc for tex is probably a good approach, thanks for pointing that out) and see if I have follow up questions.
I can also start a new thread on the forum for this since the original question was not indicative of the overall objective.
Retrieving the citation key in a translator is trivial, see the translator I posted above. Retrieving the citation key from inside Zotero isn't hard, but it's more work, and I'd prefer to use https://github.com/retorquere/zotero-better-bibtex/discussions for discussing that, since these forums are not meant for dev questions, but mostly because discussing technical things is easier when markdown can be used.
There is a fairly significant chance that the project you're planning is more ambitious than you had in mind. The notes are HTML (rich text), and html to tex conversion is not trivial, believe me.
I've also created a thread over in the bbt discussions so we can use md, with a follow up question regarding what would make the html to tex question easier
https://github.com/retorquere/zotero-better-bibtex/discussions/1798