Is there a JavaScript function for Convert Linked Files to Stored Files?

59 views
Skip to first unread message

Minyi Han

unread,
Dec 28, 2021, 7:38:34 PM12/28/21
to zotero-dev
Is there a JavaScript function for Convert Linked Files to Stored Files with the default option Delete original file after storing in Zotero? I would like to update this plugin  redleafnew/delitemwithatt: Remove attachment(s) when delete the item(s) or collection in Zotero and JurisM. (github.com), and convert linked attachment files to stored ones before the items were deleted,  with the aim of the linked files also could be restored.

Minyi Han

unread,
Dec 28, 2021, 9:42:03 PM12/28/21
to zotero-dev
I found this function   Zotero.Attachments.convertLinkedFileToStoredFile(item),  when it runs,  it will convert the linked file to stored one, however the original attachment is still in the linked files folder. Which parameter should be used to delete the original one, only keep the stored one in the storage folder?  (item, true),  (item, 0) for the parameter were tried, but failed.     When ZoteroPane.convertLinkedFilesToStoredFiles() is used, an dialog is prompted.

Many thanks.

The code snippet used:

  if (item.isAttachment()) {
                var ifLinksAtt = (item.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE); //检测是否为链接模式
                var file = await item.getFilePathAsync();
                if (file && ifLinksAtt) { // 如果文件存在(文件可能已经被删除)且为链接模式删除文件
                 
                 Zotero.Attachments.convertLinkedFileToStoredFile(item);
               
                    }  
                item.deleted = true;
                await item.saveTx();
               
                }

Dan Stillman

unread,
Dec 29, 2021, 4:45:18 AM12/29/21
to zoter...@googlegroups.com
On 12/28/21 9:42 PM, Minyi Han wrote:
> I found this function
>  Zotero.Attachments.convertLinkedFileToStoredFile(item), when it runs,
>  it will convert the linked file to stored one, however the original
> attachment is still in the linked files folder. Which parameter should
> be used to delete the original one, only keep the stored one in the
> storage folder? (item, true), (item, 0) for the parameter were tried,
> but failed.

Remember that Zotero is open source — you can just look at the source
code for questions like this. You certainly shouldn't just make up
parameters like you're doing.

It's { move: true } for the second parameter:

https://github.com/zotero/zotero/blob/ad02b701983b180a979bfd955d050339add50a06/chrome/content/zotero/xpcom/attachments.js#L2747

Minyi Han

unread,
Dec 29, 2021, 5:14:41 AM12/29/21
to zotero-dev
Many thanks,

However, the converting will fail when the attachment (PDF) is opened, using the function or in zotero UI.

Minyi Han

unread,
Dec 29, 2021, 7:50:18 AM12/29/21
to zotero-dev
Is there a function to prompt to close the file if the file is opened.

Dan Stillman

unread,
Dec 29, 2021, 1:51:08 PM12/29/21
to zoter...@googlegroups.com
You mean close the external program that you have the PDF open in? No, that's not a thing.

Minyi Han

unread,
Dec 29, 2021, 7:07:53 PM12/29/21
to zotero-dev
No exactly. 

Just prompt the user like 'The attachment has been opened, please close it before converting'  when the attachment is used by another program.

Or is there a function to check if the attachment has been opened?

Reply all
Reply to author
Forward
0 new messages