How to get the zotero keys of the pdf attachments under selected items in batch?

How to get the zotero keys of the pdf attachments under selected items in batch? Is it possible to use Zotero JavaScript API functions?
  • Probably the simplest way to get a list of full attachment paths of selected items (including the 'key' folder for locally stored PDFs) is via the Zutilo add-on (right click 'Copy attachment paths' to clipboard).

    The following javascript gets a list of all PDF attachment paths in your library:
    var filepathnames = await Zotero.DB.columnQueryAsync('SELECT path AS filepathnames FROM itemAttachments JOIN items USING (itemID) WHERE libraryID=1 AND path IS NOT NULL AND path LIKE ? ORDER BY path','%.pdf');
    return filepathnames.join('\n');

    Some hints for doing that just for selected items here:
    https://forums.zotero.org/discussion/88173/how-to-get-the-attachment-path-when-a-item-is-select
  • @tim820, thanks. Your script works well.

    But I also want the "key" of the attachment at the same time. Can you help me with the `Zotero.DB.columnQueryAsync` function expression?
  • edited November 16, 2022
    Ah yes I forgot that the above code retains "storage:" in place of the local 'key' folder name for the paths of PDFs that are stored locally (because that's how they are stored in Zotero's sqlite). Try this to get all local-only PDF files with the full path 'key' folder name included ...

    var sql = "SELECT (? || '\\storage\\' || key || REPLACE(path,'storage:','\\')) AS filepathnames FROM itemAttachments JOIN items USING (itemID) WHERE libraryID=1 AND path IS NOT NULL AND path LIKE ? ORDER BY filepathnames";
    var filepathnames = await Zotero.DB.columnQueryAsync(
    sql,
    [
    Zotero.DataDirectory.dir,
    'storage:%.pdf'
    ]
    );
    return filepathnames.join('\n');

    Getting the list limited to selected items only - as suggested in the linked thread above - is still beyond me I'm afraid. But the other people in that thread can presumably assist.

  • Thanks again. But I'm afraid this won't work with zotfile installed.
  • edited November 16, 2022
    You didn't say you were using Zotfile/linked PDFs. In that case if you want a list of linked PDF paths, the alternative code below would give you that.

    But then a key is not relevant to that linked PDF path, because that folder path - under Zotfile's Custom Location - no longer includes a folder named with the key. So the fact that you said want the key suggests that you may really ONLY want the key of the PDF or that of the parent item, not the linked PDF path ? In which case the itemAttachments table in sqlite shows both the (PDF) itemID and the parentID (easily seen with say the free DB Browser), as well as the full linked PDF path. The items table then shows the key associated with each of those two itemID's. I think I'm right in saying there will be no (key) folder associated with the parentID unless it has other files in addition to its linked-location PDF. OTOH the linked PDF will have a key folder under Zotero\storage that contains full-text indexing files.

    var filepathnames = await Zotero.DB.columnQueryAsync('SELECT path AS filepathnames FROM itemAttachments JOIN items USING (itemID) WHERE libraryID=1 AND path IS NOT NULL AND path NOT LIKE ? AND path LIKE ? ORDER BY path',['storage:%','G:\\ZoteroAttachments\\%']);
    return filepathnames.join('\n');

    Note: replace the hard-coded G:\\ZoteroAttachments\\ with your Zotfile Custom Location (there may be a simple way to get Zotfile's Custom Location in code - if anyone knows what that is please let me know).

Sign In or Register to comment.