Getting the Child Item of an Item in an Export Translator?

45 views
Skip to first unread message

Jonathan Jeffrey

unread,
Jul 7, 2022, 3:33:05 AM7/7/22
to zotero-dev
Hi, I am trying to write an export translator such that I can use the Quick Copy feature to generate a "Open PDF" url for use in a note-taking app. My current draft is like:

function doExport() {
    var item;
    while (item = Zotero.nextItem()) {
        Zotero.write("zotero://open-pdf/library/items/");
        Zotero.write(item.key);
        Zotero.write("?page=0");
    }
}

The issue is that the generated link is of the *parent* item of the PDF (whereas the open-pdf API wants the PDF item key), and I can neither Quick Copy directly from the nested PDF nor figure out a way to access the child of the item from the translator.

However, I imagine the latter might be possible: what is the easiest way to do it?

On a side note: is there any way to call window.prompt in the export translator,? I would like be able to e.g. quickly put in the desired page number of the PDF.

Dan Stillman

unread,
Jul 7, 2022, 3:52:33 AM7/7/22
to zoter...@googlegroups.com
On 7/7/22 1:52 AM, Jonathan Jeffrey wrote:
Hi, I am trying to write an export translator such that I can use the Quick Copy feature to generate a "Open PDF" url for use in a note-taking app.

There's an `attachments` property with an array of child attachments, and the item key is included. Just add Zotero.debug(item) to see what's available.


The issue is that the generated link is of the *parent* item of the PDF (whereas the open-pdf API wants the PDF item key)

We'll be updating zotero://open-pdf soon to accept a parent item key, so that it functions that same as double-clicking on the parent. You could also just generate a zotero://select link for the attachment item with Zutilo and change zotero://select to zotero://open-pdf, or submit a patch to Zutilo to add support for copying zotero://open-pdf links.

But now that these links are being used more widely, we might consider adding a built-in way to copy these links.


On a side note: is there any way to call window.prompt in the export translator,?

No. Translators run in a sandbox and aren't part of the main Zotero environment.

Jonathan Jeffrey

unread,
Jul 7, 2022, 8:35:59 AM7/7/22
to zotero-dev
Dan, thanks for your guidance. I ended up looping through the attachment property and selecting the first PDF found.

This seems to work well enough for me (I don't store more than one PDF per entry, and haven't tested whether it corresponds with the Open PDF button / double-clicking parent if you do). Do you know how doubling clicking the parent / pressing Open PDF works, and whether this aligns?

function doExport() {
    var item;
    while ((item = Zotero.nextItem())) {
        // Zotero.debug(item);
        for (let key in item.attachments) {
            let attachment = item.attachments[key];
            // Zotero.debug(attachment);

            if (attachment.contentType === "application/pdf") {
                Zotero.write("zotero://open-pdf/library/items/");
                Zotero.write(attachment.key);
                Zotero.write("?page=0");
                break;
            }
        }
    }
}
Reply all
Reply to author
Forward
0 new messages