JS Unfulfilled Best Attachment 7.0.6

edited September 18, 2024
Debug ID D1065973352
Report ID 1313967717

Attempted to run script from Attachment Title vs Filename:

var items = ZoteroPane.getSelectedItems();
for (let item of items) {
if (!item.isRegularItem()) continue;
let attachment = await item.getBestAttachment();
if (!attachment) continue;
let title = attachment.getField('title');
if (title.endsWith('.pdf')) {
attachment.setField('title', 'PDF');
await attachment.saveTx();
}
}
Result:

===>undefined<=== (undefined)
This worked in the past (7.0.0) but now isn't. I disabled all plugins to troubleshoot this.
Here is the output of what I have selected under non-asynchronous mode:

JS ran:

var items = ZoteroPane.getSelectedItems();
// for (let item of items) {
// if (!item.isRegularItem()) continue;
// let attachment = await item.getBestAttachment();
// if (!attachment) continue;
// let title = attachment.getField('title');
// if (title.endsWith('.pdf')) {
// attachment.setField('title', 'PDF');
// await attachment.saveTx();
// }
// }
items
Result:

[
"0": {
"key": "RBLUHDZ3",
"version": 8669,
"itemType": "presentation",
"title": "Containment/Surveillance",
"place": "Sandia National Laboratories",
"creators": [
{
"firstName": "H. A.",
"lastName": "Smartt",
"creatorType": "presenter"
}
],
"tags": [
{
"tag": "/unread",
"type": 1
}
],
"collections": [],
"relations": {},
"dateAdded": "2024-09-18T20:43:58Z",
"dateModified": "2024-09-18T20:44:44Z"
}
"1": {
"key": "TDDR6TKK",
"version": 8664,
"itemType": "attachment",
"title": "2022-Containtment-Surveillance",
"parentItem": "RBLUHDZ3",
"linkMode": "imported_file",
"contentType": "application/pdf",
"charset": "",
"filename": "2022-Containtment-Surveillance.pdf",
"tags": [],
"relations": {},
"dateAdded": "2024-09-18T18:07:51Z",
"dateModified": "2024-09-18T20:43:58Z"
}
"2": {
"key": "TFVF3GSJ",
"version": 7557,
"itemType": "presentation",
"title": "Niobium STJ Material Analysis",
"date": "June 29. 2022",
"accessDate": "2022-06-29",
"place": "Göttingen",
"creators": [],
"tags": [
{
"tag": "/unread",
"type": 1
}
],
"collections": [
"FZC39X4Y"
],
"relations": {},
"dateAdded": "2023-03-25T20:32:46Z",
"dateModified": "2023-04-03T04:26:34Z"
}
"3": {
"key": "GPWNHJ3R",
"version": 8670,
"itemType": "attachment",
"title": "Nb STJ",
"parentItem": "TFVF3GSJ",
"linkMode": "linked_file",
"contentType": "application/pdf",
"charset": "",
"path": "attachments:presentation/_/2022-niobium_stj_material_anal.pdf",
"tags": [],
"relations": {},
"dateAdded": "2024-09-18T20:13:59Z",
"dateModified": "2024-09-18T20:53:41Z"
}
]
  • edited September 18, 2024
    (Please try to use more descriptive thread titles. "JS" isn't "broken".)

    Those attachments' titles don't end in ".pdf", so they're not changed. The code is only for changing titles that had been previously been changed to match the filename. If you want it to do something different, you'd have to edit the code.
  • Updated post title.

    Fair point about the code filtering by attachment title. On debugging further, it appears that the problem might be that the best attachment is never given.
     
    var items = ZoteroPane.getSelectedItems();

    for (let item of items) {
    item.getBestAttachment();
    }
    gives


    {
    "isFulfilled": false,
    "isRejected": false
    }


    on an Item with either a single PDF attachment or a linked PDF attachment.
  • No, that's just an async function.
  • Debug ID: D241486413
    Report ID: 1124671906
    Zotero 7.0.6 OSX Sonoma 14.6.1 (23G93)

    OK here is another trace of my errors while running the following:


    var items = ZoteroPane.getSelectedItems();
    for (let item of items) {
    if (!item.isRegularItem()) continue;
    let attachment = await item.getBestAttachment();
    if (!attachment) continue;
    let ctype = attachment.getField('contentType');
    if (ctype.endsWith('/pdf')) {
    attachment.setField('title', 'PDF');
    await attachment.saveTx();
    }
    }


    AFAICT this should use the contentType field ("application/pdf" for PDFs) to rename matching attachment titles to PDF. However, anything run as async functions return ===>undefined<=== (undefined)...
  • edited September 18, 2024
    No, 'contentType' isn't an item field. You can't just make things up and ask why it's not working — you'd have to look at existing Zotero code and see exactly how this all works, or explain exactly what you're trying to do and see if a community member would write some code for you. But I'm afraid we can't help with this.
    However, anything run as async functions return ===>undefined<=== (undefined)...
    Async mode runs the code as an async function and shows the return value.
  • edited September 18, 2024
    (Also, though, attachment titles for PDFs aren’t always just “PDF” and won’t be going forward, so you’d be overwriting actual data here and creating inconsistencies with future titles. You can do that if you want to, but the code in the documentation specifically addressed titles that matched filenames for a reason.)
  • https://s3.amazonaws.com/zotero.org/images/forums/u5805913/uuxcrr2bekzqp3s2zdr4.png

    Lesson learned: the item/attachment objects are very obscure, so while getField works for normal item fields it does not work for properties of the attachment "object". Rather one must use .attachment<PROP> properties to parse them.

    For posterity, here is how one might change all item titles to "PDF" based on mime type (contentType):

    var items = ZoteroPane.getSelectedItems();
    for (let item of items) {
    if (!item.isRegularItem()) continue;
    let attachment = await item.getBestAttachment();
    if (!attachment) continue;
    let ctype = attachment.attachmentContentType;
    if (ctype.endsWith('/pdf')) {
    attachment.setField('title', 'PDF');
    await attachment.saveTx();
    }
    // v = []
    // for (let i in Object(item)){
    // v.push(i)
    // }
    //. return v
    }
    (you can see how I found the .attachmentContentType property via the commented portion, useful debug hack)

    Note as dstillman mentions, this should be used only if - like me - your attachment titles currently have no meaning (in my case, I used plugins like Attanger/ZotFile to rename files + attachment titles and now want to keep with Zotero's best practices which could change in the future).
Sign In or Register to comment.