JS Unfulfilled Best Attachment 7.0.6
Debug ID D1065973352
Report ID 1313967717
Attempted to run script from Attachment Title vs Filename:
Here is the output of what I have selected under non-asynchronous mode:
JS ran:
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"
}
]
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.
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. gives
{
"isFulfilled": false,
"isRejected": false
}
on an Item with either a single PDF attachment or a linked PDF attachment.
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)...
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): (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).