December 2008

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)

Chickenfoot
Zotero

Comments (0)

Permalink

Adding all tabs to Zotero Version 2 — scraping translatable sites

In a previous post adding all Firefox tabs to Zotero using Chickenfoot, I showed how to write a Chickenfoot script to loop through all Firefox  tabs and add each of them as an item into Zotero.  A limitation of the script was that it used only ZoteroPane.addItemFromPage for every tab, even if a given tab had a translator that could be used to save the item. As explained in Adding items to Zotero with Chickenfoot, you can use Zotero_Browser.scrapeThisPage to invoke the appropriate translator for tab. The reason I didn't use Zotero_Browser.scrapeThisPage in my Chickenfoot script to add items is that I didn't know how to write a function to determine suitable translator exists.

Now, I think I've come up with a way of determining whether a translator exists — though I'm not highly confident that the solution is fullproof.    I'll share my  Chickenfoot script here, explain the logic behind it, and write about its possible limitations.  First the script:

// add_each_tab_to_Zotero_2.js
// R. Yee

var Zotero = chromeWindow.Zotero;
var ZoteroPane = chromeWindow.ZoteroPane;
var Zotero_Browser = chromeWindow.Zotero_Browser;
var tabBrowser = getTabBrowser(chromeWindow);

// getIcon returns a link to the translator icon for the current
// tab and false if there is no suitable
// translator and 'chrome://zotero/skin/treesource-collection.png'
// if there are multiple savable elements on a page

function getIcon() {

  var browser = Zotero_Browser.tabbrowser.selectedBrowser;
  var tab0 = new Zotero_Browser.Tab(browser);

  // need to figure out whether doc is HTMLDocument
  // doc instanceof HTMLDocument doesn't seem to work here

  var doc = browser.contentWindow.document;

  // in emulation of
  // https://www.zotero.org/trac/browser/extension/tags/1.0.7/chrome/content/zotero/browser.js#L311

  var rootDoc = doc;
  if (rootDoc.defaultView) {
    while(rootDoc.defaultView.frameElement) {
      rootDoc = rootDoc.defaultView.frameElement.ownerDocument;
    }
  }

  // detect possible translators and return the corresponding icon
  tab0.detectTranslators(rootDoc,doc);
  return tab0.getCaptureIcon();

} // getIcon()

// create a new collection with current date
var new_Collection = Zotero.Collections.add("_Saved " + (new Date()).toLocaleString());

// output # tabs
output(tabBrowser.browsers.length);

// loop through tabs, selecting each one in turn
for (var i=0; i < tabBrowser.browsers.length; i++) {
  tabBrowser.mTabContainer.advanceSelectedTab(1, true);
  output(tabBrowser.selectedBrowser.contentWindow.location);
  var icon = getIcon();
  // if icon is not false and not representing multiple items -- scrape page
  if (icon && icon != 'chrome://zotero/skin/treesource-collection.png') {
    Zotero_Browser.scrapeThisPage(new_Collection.id);
  // otherwise add item as a generic web page
  } else {
   ZoteroPane.addItemFromPage(new_Collection.id);
  }
}

A few points about the script:

When I ran the script with 3 tabs, it seemed to work fine. When I had 20+ tabs, all the tabs were saved — but only the first one ended up in the right collection. (I don't know why….) Also, the code is not terribly elegant — for example, it depends on creating a new Zotero_Browser.Tab for each tab; I figure there should be some way to read off whether an icon exists in the interface already without having to recalculate possible translators.

Chickenfoot
Zotero

Comments (4)

Permalink

Getting primary and secondary item types in Zotero

A Chickenfoot script for calculating the primary and secondary Zotero items (which draws upon the code in overlay.js->onLoad():


// based on https://www.zotero.org/trac/browser/extension/tags/1.0.7/chrome/content/zotero/overlay.js#L153

var Zotero = chromeWindow.Zotero;

var itemTypes = Zotero.ItemTypes.getPrimaryTypes();

for(var i = 0; i<itemTypes.length; i++) {
  output (itemTypes[i]['name'] + ":" + itemTypes[i]['id']);
}

var itemTypes = Zotero.ItemTypes.getSecondaryTypes();

for(var i = 0; i<itemTypes.length; i++) {
  output (itemTypes[i]['name'] + ":" + itemTypes[i]['id']);
}

The output of the script is (for Zotero 1.0.7):

book:2
bookSection:3
document:34
journalArticle:4
magazineArticle:5
newspaperArticle:6
artwork:12
audioRecording:26
bill:16
blogPost:23
case:17
computerProgram:32
conferencePaper:33
dictionaryEntry:36
email:21
encyclopediaArticle:35
film:11
forumPost:25
hearing:18
instantMessage:24
interview:10
letter:8
manuscript:9
map:22
patent:19
podcast:31
presentation:27
radioBroadcast:30
report:15
statute:20
thesis:7
tvBroadcast:29
videoRecording:28

When you hit "New Item" button in Zotero, you are given the option of creating a new item of one of a given type:

primary and secondary Zotero item types

Furthermore, you can use Zotero.ItemTypes to map between item types and IDs:

Zotero.ItemTypes.getID('book'); yields 2

and

Zotero.ItemTypes.getName(2); returns book

Chickenfoot
Zotero

Comments (1)

Permalink