This is an old revision of the document!
Zotero API documentation
Setting up a simplified development environment
The two easiest ways to set up a development environment for Zotero is either with an interactive javascript environment, or using The Firefox extension Plain old Webserver which runs a web server inside of Firefox. Plain old Webserver can use server side Javascript to dynamically create pages, and as such all of the Zotero internals are accessible to code running within this web server.
There are a number of suitable interactive javascript environments for interacting with Firefox's internals (FireBug is not suitable). The suitable extensions that I have been able to find are:
- A Firefox plugin to provide MozRepl access directly inside firefox .
- The ExecuteJS extension.
My feeling is that the easiest way to get some kind of working application up is to use the Plain old Webserver and server side javascript. This also makes it relatively easy to deploy code to other users without going to the trouble of creating a whole new firefox extension.
Setting up a Firefox development environment is beyond the scope of this document.
Perl programmers should be aware of the MozRepl CPAN module, and the closely related MozRepl::RemoteObject which allows you to access Javascript objects from inside perl programs.
A problem with MozREPL: With a longer script I've had trouble with MozRepl timing out on me for no apparent reason. The solution to this is to either send the code to the terminal application in smaller sized chunks, or to keep pasting the code in until it works. Unfortunately in some cases missing semicolons and curly brackets can cause silent failure in the REPL as well which means that debugging longer bits of code can be interesting. Avoid this by avoiding excessively long blocks of procedural code - factor everything into smaller functions wherever possible.
Zotero API Howtos
The Zotero API is under-documented, and at present requires a lot of looking around in the source code. The most useful parts of the source code are in chrome/content/zotero/xpcom and xpcom/data, and the chrome/content/zotero (particularly overlay.js and fileInterface.js)
Create new Zotero object
This is the first thing that you need to do when interacting with zotero's internals. The code to do so is:
var Zotero = Components.classes[“@zotero.org/Zotero;1”] .getService(Components.interfaces.nsISupports).wrappedJSObject;
Setup a Zotero search
If you are focused on data access, then the first thing you will want to do will be to retrieve items from your zotero. Creating an in-memory search is a good start.
var search = new z.Search();
Search for items containing a specific tag
Starting with the code from “Setup a Zotero search” we then use the following code to retrieve items with a particular tag:
search.addCondition('tag', 'is', 'tag name here');
Search for a Zotero subcollection
TODO
Select a Zotero saved search
TODO
Search term operators
TODO
Combining search terms
TODO
Complete list of search operators
TODO (should be pretty easy)
Complete list of search fields
TODO (with description of what the more obscure fields mean - e.g. abstractNote for abstract, and how do we search the fulltext archive?)
Executing the search
Once the search conditions have been set up, then it's time to execute the results:
var results = search.search();
This returns the item ids in the search as an array [I could be wrong … ]. The next thing to do is to get the Zotero items for the array of IDs:
var items = z.Items.get(results);
=head5 Getting a bibliography for an array of items:
Here we use zotero's quickcopy functions to get a bibliography in the style specified in Zotero's preferences.
First we start with a list of as in the previous entry.
var qc = z.QuickCopy;
var biblio = qc.getContentFromItems(new Array(item),
z.Prefs.get(“export.quickCopy.setting”));
var biblio_html_format = cite.html;
var biblio_txt = cite.text;
TODO: get citations. change the style. get stuff in other formats, especially RTF
Get information about an item.
TODO: need to list all the possible fields here, and what kind of entry they belong to.
To get an item's abstract, we get the 'extra' field from the Zotero item:
var abstract = item.getField('extra');
=head5 Get fulltext for an item.
TODO
=head5 Get stored attachements for an item
TODO
Get child notes for an item
To get the child notes for an item, we use the following code:
var notes = item.getNotes();
This returns an array of notes. Each note is in HTML format. To get each note in turn we just iterate through the array:
for (var j=0;j<notes.length;j++) {
var note = z.Items.get(notes[j]);
var note_html = note.getNote;
}
Get an item's related items
This technique works for anything that can have related items attached within the zotero database. This includes items and notes.
var related_items = item.relatedItemsBidirectional
This returns a list of items just like in the search examples.
Generic XUL Javascript to provide support functions
Writing out a file:
This function will write out a file to a specified filename. If the file already exists it will be silently overwritten. [TODO: obviously this needs to be cleaned up, talk about append mode as well, and failure if file already exists …]
Note that Plain Old Webserver contains quite a few simplified support functions for reading, writing and deleting files, including new bits of javascript, and generally making things nice and easy.
function writeFile(filename, data) {
var file = Components.classes[“@mozilla.org/file/local;1”].createInstance(Components.interfaces.nsILocalFile);
var foStream = Components.classes[“@mozilla.org/network/file-output-stream;1”].createInstance(Components.interfaces.nsIFileOutputStream);
file.initWithPath(filename);
foStream.init(file, 0x02 , 00666, 0);
foStream.write(data, data.length);
foStream.close();
};
writeFile(“/tmp/boo.txt”, “boo\n”);
TODO
many other things :)