Retrieving attachments filenames

36 views
Skip to first unread message

Shashi Prabhakar

unread,
Dec 17, 2014, 2:10:26 AM12/17/14
to zoter...@googlegroups.com
Dear Group members,

I am trying to extract information from the zotero item using this piece of code and its working fine except the last two lines

    var selected_items = ZoteroPane.getSelectedItems();
    var item = selected_items[0];
    var type = item.getType();
    var title = item.getField('title');
    var creators = item.getCreators();
    author = "";
    for (var i=0; i<creators.length; i++) {
        if (creators[i].ref) {
            author = author + creators[i].ref.firstName + " " + creators[i].ref.lastName + "\n";
        }
    }
    var journal        = item.getField('publicationTitle');
    var volume         = item.getField('volume');
    var issue          = item.getField('issue');
    var pages          = item.getField('pages');
    var date           = item.getField('date', true).substr(0, 4);
    var doi            = item.getField('DOI');
    var url            = item.getField('url');
    var dateadded      = item.getField('dateAdded');
    var datemodified   = item.getField('dateModified');

    var attachment     = item.getAttachments();
    var file           = attachment.filename;


However, I am not able to get the number and paths of attachments associated with that particular item from the last two lines of the code. I request for an help.

Thanks in advance.
With best regards,
shashi

Aurimas Vinckevicius

unread,
Dec 17, 2014, 2:29:24 AM12/17/14
to zoter...@googlegroups.com
Zotero.Item.getAttachments returns an array of attachment IDs, so the size of that array would give you the number of attachments that you have for the item (including attached links). You would then have to retrieve the attachment items via Zotero.Items.get and use the result to retrieve filenames while making sure that the attachment is not a link attachment (since there is no file name for a link attachment)

--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+...@googlegroups.com.
To post to this group, send email to zoter...@googlegroups.com.
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.

Shashi Prabhakar

unread,
Dec 17, 2014, 6:00:59 AM12/17/14
to zoter...@googlegroups.com
Thank for the quick reply.

I am able to get the attachment ids using this statement

    var attachment = item.getAttachments();

When I display the attachment variable, it shows 157. However, I am unable to get the filenames from this statement

    var filename   = Zotero.Items.get(attachment).attachementPath;

when I alert the full object attachment, it displays the following content

[{"_disabled":false,"_id":157,"_libraryID":null,"_key":"7TD9VBTB","_itemTypeID":14,"_dateAdded":"2013-03-02 02:48:34","_dateModified":"2013-04-09 07:38:50","_firstCreator":"","_numNotes":0,"_numNotesTrashed":null,"_numNotesEmbedded":null,"_numNotesEmbeddedIncludingTrashed":null,"_numAttachments":0,"_creators":[],"_itemData":{"1":null,"27":null,"110":"Davis et al. - 2012 - Abruptly autofocusing vortex beams.pdf"},"_sourceItem":156,"_primaryDataLoaded":true,"_creatorsLoaded":false,"_itemDataLoaded":false,"_relatedItemsLoaded":false,"_changed":{},"_changedPrimaryData":false,"_changedItemData":false,"_changedCreators":false,"_changedDeleted":false,"_changedNote":false,"_changedSource":false,"_changedAttachmentData":false,"_previousData":{},"_bestAttachmentState":null,"_fileExists":true,"_deleted":null,"_hasNote":null,"_noteTitle":null,"_noteText":null,"_noteAccessTime":null,"_cachedAttachments":null,"_cachedNotes":null,"_attachmentLinkMode":0,"_attachmentMIMEType":"application/pdf","_attachmentPath":"storage:Davis et al. - 2012 - Abruptly autofocusing vortex beams.pdf","_relatedItems":false,"_sourceItemID":156}]

As i can understand, "_key":"7TD9VBTB" is the directory in storage folder and "_attachmentPath":"storage:Davis et al. - 2012 - Abruptly autofocusing vortex beams.pdf" is the filename.

I wish to know, I can extract these two values to form the complete file path string.

shashi
Reply all
Reply to author
Forward
0 new messages