From 8b446b7fc58c7ff8a2dfa0067b5bc2fe9d73cd3b Mon Sep 17 00:00:00 2001 From: Abe Jellinek <1770299+AbeJellinek@users.noreply.github.com> Date: Tue, 27 Jan 2026 11:44:50 -0500 Subject: [PATCH] Set auto attachment title when not renaming (#5748) --- chrome/content/zotero/renameFiles.mjs | 4 ++-- chrome/content/zotero/xpcom/data/item.js | 5 ++--- chrome/content/zotero/xpcom/recognizeDocument.js | 2 +- chrome/content/zotero/zoteroPane.js | 2 +- test/tests/attachmentsTest.js | 14 ++++++-------- test/tests/recognizeDocumentTest.js | 6 +++--- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/chrome/content/zotero/renameFiles.mjs b/chrome/content/zotero/renameFiles.mjs index bbaddaf118..1a15cd1fb5 100644 --- a/chrome/content/zotero/renameFiles.mjs +++ b/chrome/content/zotero/renameFiles.mjs @@ -146,7 +146,7 @@ export async function renameFilesFromParent({ userLibrary = true, groupLibrary = // update the title if it matches the old filename const newTitleLC = attachmentItem.getField('title').toLowerCase(); if (newTitleLC === oldBaseName.toLowerCase() || newTitleLC === oldFileName.toLowerCase()) { - attachmentItem.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true }); + attachmentItem.setAutoAttachmentTitle(); } await attachmentItem.saveTx(); @@ -196,7 +196,7 @@ export async function renameFileFromParent(attachmentItem) { const newTitleLC = attachmentItem.getField('title').toLowerCase(); if (newTitleLC === oldBaseName.toLowerCase() || newTitleLC === oldName.toLowerCase()) { - attachmentItem.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true }); + attachmentItem.setAutoAttachmentTitle(); requiresSave = true; } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index ef306a7e31..85196ec06b 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -4014,7 +4014,7 @@ Zotero.Item.prototype._getDefaultTitleForAttachmentContentType = function () { }; -Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs, forceFirstOfType } = {}) { +Zotero.Item.prototype.setAutoAttachmentTitle = function ({ forceFirstOfType } = {}) { if (!this.isAttachment()) { throw new Error("setAutoAttachmentTitle() can only be called on attachment items"); } @@ -4030,8 +4030,7 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs .every(item => item === this) ) : forceFirstOfType; - let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this); - if (isFirstOfType && isBeingRenamed) { + if (isFirstOfType) { let defaultTitle = this._getDefaultTitleForAttachmentContentType(); if (defaultTitle !== null) { this.setField('title', defaultTitle); diff --git a/chrome/content/zotero/xpcom/recognizeDocument.js b/chrome/content/zotero/xpcom/recognizeDocument.js index 5d2476940e..52cbc4a084 100644 --- a/chrome/content/zotero/xpcom/recognizeDocument.js +++ b/chrome/content/zotero/xpcom/recognizeDocument.js @@ -305,7 +305,7 @@ Zotero.RecognizeDocument = new function () { if (result !== true) { throw new Error("Error renaming " + path); } - attachment.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true }); + attachment.setAutoAttachmentTitle(); await attachment.saveTx(); } diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 54735027ee..ccba67849b 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -5783,7 +5783,7 @@ var ZoteroPane = new function () { if (result !== true) { throw new Error('Error renaming ' + path); } - item.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true }); + item.setAutoAttachmentTitle(); await item.saveTx(); } } diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index 1dec7580e2..5fc9592967 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -103,7 +103,7 @@ describe("Zotero.Attachments", function () { await attachment.eraseTx(); }); - it("should set a child item's title to the filename, minus its extension", async function () { + it("should set a child item's title based on its type", async function () { let file = getTestDataDirectory(); file.append('test.pdf'); let parent = await createDataObject('item'); @@ -144,7 +144,7 @@ describe("Zotero.Attachments", function () { await attachment.eraseTx(); }); - it("should set a child item's title to the filename, minus its extension", async function () { + it("should set a child item's title based on its type", async function () { let file = getTestDataDirectory(); file.append('test.pdf'); let parent = await createDataObject('item'); @@ -152,7 +152,7 @@ describe("Zotero.Attachments", function () { file: file, parentItemID: parent.id, }); - assert.equal(attachment.getField('title'), 'test'); + assert.equal(attachment.getField('title'), Zotero.getString('file-type-pdf')); await parent.eraseTx(); }); }) @@ -2309,17 +2309,15 @@ describe("Zotero.Attachments", function () { var attachment = await importFileAttachment('test.png', { parentItemID: item.id, - // Use default setAutoAttachmentTitle() behavior -- the file isn't going to be - // renamed because autoRenameFiles.fileTypes doesn't match image/, so the title - // becomes the filename minus extension, i.e., "test" - title: null + // Pretend this was, e.g., a secondary attachment before, and it got titled + // after the file basename + title: 'test' }); assert.equal(attachment.getField('title'), 'test'); await renameFileFromParent(attachment); assert.equal(attachment.attachmentFilename, 'Title.png'); // After a manual rename, the title becomes the default for this type assert.equal(attachment.getField('title'), Zotero.getString('file-type-image')); - }); it("should change attachment title if file basename matches the title", async function () { diff --git a/test/tests/recognizeDocumentTest.js b/test/tests/recognizeDocumentTest.js index e6383ad8fb..a799f4fd64 100644 --- a/test/tests/recognizeDocumentTest.js +++ b/test/tests/recognizeDocumentTest.js @@ -108,7 +108,7 @@ describe("Document Recognition", function () { assert.lengthOf(addedIDs, 2); var item = Zotero.Items.get(addedIDs[0]); assert.equal(item.getField("title"), "Scaling study of an improved fermion action on quenched lattices"); - assert.lengthOf(modifiedIDs, 1); + assert.lengthOf(modifiedIDs, 2); await waitForProgressWindow(); @@ -139,7 +139,7 @@ describe("Document Recognition", function () { // Item and note assert.lengthOf(addedIDs, 2); var item = Zotero.Items.get(addedIDs[0]); - assert.lengthOf(modifiedIDs, 1); + assert.lengthOf(modifiedIDs, 2); await waitForProgressWindow(); @@ -167,7 +167,7 @@ describe("Document Recognition", function () { // Item and note assert.lengthOf(addedIDs, 2); var item = Zotero.Items.get(addedIDs[0]); - assert.lengthOf(modifiedIDs, 1); + assert.lengthOf(modifiedIDs, 2); await waitForProgressWindow();