Skip to content

Accessing attributes of Zotero Items

The following Chickenfoot script demonstrates how to access attributes related to Zotero items. (I've run this script on both Zotero 1.0.7 and the latest named release of  Zotero 1.5 Sync Preview (1.5-sync3.5))):

// zotero_item_show_pieces.js
// code to extract elements of the first selected item
// Dec 16, 2008

var Zotero = chromeWindow.Zotero;
var ZoteroPane = chromeWindow.ZoteroPane;
var items = ZoteroPane.getSelectedItems();
var item = items[0];

// get item ID

output("item ID: " + item.id);

// get named type

var iType = item.getType();
output("type: " + Zotero.ItemTypes.getName(iType));

// figure out whether this is a top level item, note, etc.
// if not, is it a  note or attachment
output("isRegularItem: " + item.isRegularItem());
output("isAttachment: " + item.isAttachment());
output("isNote: " + item.isNote()); // or just check type == note

// get all possible fields for the type and their value
var fields = Zotero.ItemFields.getItemTypeFields(iType);
for (var i=0; i<fields.length; i++) {
  fieldName = Zotero.ItemFields.getName(fields[i]);
  fieldVal = item.getField(fieldName);
  fieldLocalName = Zotero.ItemFields.getLocalizedString(iType,fields[i])
  output(fieldLocalName + ": " + fieldVal);
}
//fields;

// get creators

var creators = item.getCreators();

for (var i=0; i<creators.length; i++) {
  // Zoteo 1.5 +
  if (creators[i].ref) {
    output (Zotero.CreatorTypes.getName(creators[i].creatorTypeID) + ": " +creators[i].ref.lastName + ", " + creators[i].ref.firstName);  }
  // Zotero 1.0.x
  else {
    output (Zotero.CreatorTypes.getName(creators[i].creatorTypeID) + ": " +creators[i].lastName + ", " + creators[i].firstName);
  }
}

// date added and date modified
output("Date Added: " + item.getField('dateAdded'));
output("Date Modified: " + item.getField('dateModified'));

// Notes -- return ids for the notes

var notes = item.getNotes();
output("notes: " + notes);

// Attachments -- there are more details to take care of
// https://www.zotero.org/trac/browser/extension/tags/1.5-sync3.5/chrome/content/zotero/xpcom/data/item.js#L3469

var attachments = item.getAttachments();
output("attachments: " + attachments);

// Tags -- Z 1.5.x has more complex structures than Z 1.0.7

var tags = item.getTags();
output("tags:");
for (var i=0; i<tags.length; i++) {
  if (tags[i].name) {
    output("|" + tags[i].name + "|");
  }
  else {
  output("|" + tags[i]['tag'] + "|");
  }
}

// Related

// https://www.zotero.org/trac/browser/extension/tags/1.0.7/chrome/content/zotero/xpcom/data_access.js#L2180

if (item.getSeeAlso) {
  var related = item.getSeeAlso();
} else if (item.relatedItems) {
  var related = item.relatedItems;
}

output("related: " + related);

// Collections -- get which collections the item belongs in.
output("collections: " + item.getCollections());

A few notes:

  • Some of the conditionals in the script reflect differences in how Zotero 1.0.x and the upcoming Zotero 1.5+ Zotero object model work.
  • For Zotero 1.5+,  you can learn a lot about how to access attributes related to a Zotero item by studying Zotero.Item.serialize (v 1.5-sync3.5)

Post a Comment

You must be logged in to post a comment.