Problem with QuickCopy.getContentFromItems()

63 views
Skip to first unread message

Harold Peach

unread,
Mar 18, 2025, 2:28:59 PMMar 18
to zotero-dev
I'm trying to write a simple script that will append the citation of the parent item to an item note, but I'm getting an error message that I cannot figure out. Any help would be appreciated.

The following code works:

```  Javascript
const items = Zotero.Items.get(itemID);
const format = 'bibliography=http://www.zotero.org/styles/chicago-fullnote-bibliography';
const biblio = Zotero.QuickCopy.getContentFromItems(new Array(items), format, null, true);
return biblio.html;
```

But when I place the same code (shown below) in a function and call it, the code fails at ```QuickCopy.getContentFromItems()``` with the error message "Invalid mode 'undefined' in Zotero.QuickCopy.getContentFromItems()"

``` Javascript
// Returns the citation for the parent item.
function getParentCitation(itemID) {
    console.log('Getting Parent Citation\nParent Item ID: ' + itemID);
    // itemIDs for standalone notes and item notes can be passed to this function.
    // Only item notes have a parent ID, so the itemID passed to this function
    // for a standalone note will be null. This if/then/else statement checks
    // for standalone notes and returns a blank citation in those cases.
    if (!itemID) {
        console.log('Standalone note. No parent.');
        var citation = ' ';
    } else {
        console.log('Item note. Retrieving parent information.');
        const parentItem = Zotero.Items.get(itemID);
        console.log('Parent item: ', parentItem);
        const format = 'https://www.zotero.org/styles/chicago-fullnote-bibliography'; // Chicago Manual of Style, 17th edition (full note)
        console.log('Citation Format: ', format);
        const biblio = Zotero.QuickCopy.getContentFromItems(new Array(parentItem), format, null, true); // Return citation for the parent item in the format specified.
        var citation = biblio.html; // Copy the HTML-formatted version of the citation.
    }
    return citation;
}

// Returns the value of the specified field from the parent item.
function getFieldValueByItemID(itemID, fieldName) {
    if (itemID) {
        const parentItem = Zotero.Items.get(itemID);
        fieldValue = parentItem.getField(fieldName);
    } else {
        fieldValue = '';
    }
    return fieldValue;
}

// Lists all item notes in the selected items and appends the parent reference information to the note.
// TO WORK YOU MUST EXPAND THE ITEMS AND THEN SELECT THE CHILD ITEMS AS WELL.
// Based on code by dstillman
const zoteroPane = Zotero.getActiveZoteroPane();
var noteHTML = await zoteroPane.getSelectedItems()
    .filter(item => item.isNote())
    .map(item => item.getNote() + '<p>Source: ' + getParentCitation(item.parentID) +
        ' <a href=zotero://select/library/items/' + getFieldValueByItemID(item.parentID,'key') +
        '>zotero://select/library/items/' + getFieldValueByItemID(item.parentID,'key') + '</a></p>');
return noteHTML.join('<hr>');
```

Abe Jellinek

unread,
Mar 18, 2025, 3:14:16 PMMar 18
to zoter...@googlegroups.com
Don’t use `new Array()` - it doesn’t do what you expect it to do. Instead of `Zotero.QuickCopy.getContentFromItems(new Array(parentItem), …)`, try `Zotero.QuickCopy.getContentFromItems([parentItem], …)`.

On Mar 18, 2025, at 2:28 PM, Harold Peach <harold...@gmail.com> wrote:

I'm trying to write a simple script that will append the citation of the parent item to an item note, but I'm getting an error message that I cannot figure out. Any help would be appreciated.
--
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 view this discussion visit https://groups.google.com/d/msgid/zotero-dev/731e07e5-fa3b-4187-9fbf-edce9f6db55cn%40googlegroups.com.

Abe Jellinek

unread,
Mar 18, 2025, 3:20:27 PMMar 18
to zoter...@googlegroups.com
Oh, sorry, it’s just that you’re passing an invalid format string. You’re prefixing it with `bibliography=` in the first code block and not prefixing it with anything in the second. The `bibliography=` is required.

Harold Peach

unread,
Mar 18, 2025, 5:38:21 PMMar 18
to zotero-dev
Good catch. I added 'bibliography=' to format, but unfortunately it did not fix the overall problem. Adding it to format just changed the error I'm getting from Zotero.QuickCopy.getContentFromItems(). I tried the following three combinations:

parentItem results in "TypeError: items.filter is not a function"
[parentItem] and new Array(parentItem) both result in "TypeError: Zotero.Styles.get(...).getCiteProc is not a function"

All resulted in the errors shown.


Harold Peach

unread,
Mar 19, 2025, 12:18:05 AMMar 19
to zotero-dev
It turned out to be a scope issue. Below is the working code.


// Returns the value of the specified field from the parent item.
function getParentCitation(itemID) {
    console.log('Getting Parent Citation\nParent Item ID: ' + itemID);
    // itemIDs for standalone notes and item notes can be passed to this function.
    // Only item notes have a parent ID, so the itemID passed to this function
    // for a standalone note will be null. This if/then/else statement checks
    // for standalone notes and returns a blank citation in those cases.
    let citation = 'No citation for this item.';
    const format = 'bibliography=http://www.zotero.org/styles/chicago-note-bibliography';  // Chicago Manual of Style, 17th edition (full note)

    console.log('Citation Format: ', format);
    if (itemID > 0) {

        console.log('Item note. Retrieving parent information.');
        const parentItem = Zotero.Items.get(itemID);
        console.log('Parent item: ', parentItem);
        const biblio = Zotero.QuickCopy.getContentFromItems(new Array(parentItem), format);  // Return citation for the parent item in the format specified.
        console.log('QCBib: ', biblio);
        citation = biblio.text;
    } else {
        citation = '&nbsp;';
    }
    console.log('Citation: ', citation);

    return citation;
}

// Returns the value of the specified field from the parent item.
function getFieldValueByItemID(itemID, fieldName) {
    console.log('Getting Parent Field\nParent Item ID: ' + itemID);

    // itemIDs for standalone notes and item notes can be passed to this function.
    // Only item notes have a parent ID, so the itemID passed to this function
    // for a standalone note will be null. This if/then/else statement checks
    // for standalone notes and returns a blank citation in those cases.
    let fieldValue = 'No value for this field.';
    if (itemID > 0) {

        const parentItem = Zotero.Items.get(itemID);
        console.log('Parent item: ', parentItem);
        fieldValue = parentItem.getField(fieldName);
    } else {
        fieldValue = '&nbsp;';
    }
    console.log('Field Value: ', fieldValue);

    return fieldValue;
}

// Lists all item notes in the selected items and appends the parent reference information to the note.
// TO WORK YOU MUST EXPAND THE ITEMS AND THEN SELECT THE CHILD ITEMS AS WELL.
// Also works with saved search.
// Based on code by dan stillman
Reply all
Reply to author
Forward
0 new messages