How to replace a PDF attachment file.

64 views
Skip to first unread message

linxz...@gmail.com

unread,
Aug 10, 2020, 2:57:31 AM8/10/20
to zotero-dev
I am doing an add-on to help CNKI user to retrieve metadata and add TOC to PDF.  I want to replace the old pdf attachment with a TOC added pdf.  Some old pdf attachment items have tags, related items, or notes. So I hope I can add these child items to the new PDF attachment item.  I have tried to do these, but I found some problems.

1. Simply replace the attachment file with a new PDF file.
```javascript
var item = ZoteroPane.getSelectedItems()[0];
OS.File.copy(newFile, item.getFilePath());
```
But the new file will not sync to another device. Maybe someone could me how to  trigger file sync?

2. Import new PDF by Zotero.Attachments
```javascript
var newItem = await Zotero.Attachments.importFromFile({
            file,
            libraryID,
            fileBaseName,
            parentItemID,
        });
newItem.tags = oldItem.tags;
var relatedItems = oldItem. relatedItems;  // return a key array [ "EHAMIZAW"]
```
I don't know how to get attachment  child notes and add the child notes to new attachment. Also `relatedItems` are identified by keys,  I hope someone could tell me how to get Items by keys.

I think the first code look like quite simple. Is it a good to replace PDF attachment like that?

I hope I could make myself clear.


Dan Stillman

unread,
Aug 13, 2020, 6:30:04 PM8/13/20
to zoter...@googlegroups.com
On 8/10/20 2:57 AM, linxz...@gmail.com wrote:
I am doing an add-on to help CNKI user to retrieve metadata and add TOC to PDF.  I want to replace the old pdf attachment with a TOC added pdf.  Some old pdf attachment items have tags, related items, or notes. So I hope I can add these child items to the new PDF attachment item.  I have tried to do these, but I found some problems.

1. Simply replace the attachment file with a new PDF file.
```javascript
var item = ZoteroPane.getSelectedItems()[0];
OS.File.copy(newFile, item.getFilePath());
```
But the new file will not sync to another device. Maybe someone could me how to  trigger file sync?

This is fine, and is equivalent to making a change (e.g., an annotation) to the PDF manually in a PDF reader and saving it back to the file. You just need to make sure that the modification time of the file has been changed, which is what Zotero uses to determine whether to sync it.

This won't start a file sync right away, since it's essentially just modifying the file outside of Zotero, but the file will be uploaded in an upcoming file sync as with any other external file change. If you want to make sure it happens sooner, you can run `Zotero.Notifier.trigger('open', 'file', item.id);`, which is what gets called when an attachment is opened to indicate that the file should be checked for potential changes at the next sync.

linxz...@gmail.com

unread,
Aug 14, 2020, 2:28:38 AM8/14/20
to zotero-dev
Thank you for your detailed explanation.  I have tried to import the new attachment file in a second way. Add related items, tags, and note by the code I found in the Zotfile add-on. Next time, I will try to replace the attachment file. Your explanation is really helpful☺.   

And I have another question: Is it possible to access Zotero preferences in the web translator code?  I found something about [ Hidden Preferences ](https://www.zotero.org/support/preferences/hidden_preferences), but when I use `Zotero.Prefs`  to the web translator, it will throw an error :`TypeError: Zotero.Prefs is undefined`. 

Dan Stillman

unread,
Aug 14, 2020, 10:12:23 AM8/14/20
to zoter...@googlegroups.com
On 8/14/20 2:28 AM, linxz...@gmail.com wrote:
> And I have another question: Is it possible to access Zotero
> preferences in the web translator code?  I found something about [
> Hidden Preferences
> ](https://www.zotero.org/support/preferences/hidden_preferences), but
> when I use `Zotero.Prefs`  to the web translator, it will throw an
> error :`TypeError: Zotero.Prefs is undefined`.

General preferences can't be used, because translators run in a sandbox.

But see "Translator Preferences" at the bottom of that page and the
Z.getHiddenPref() function:

https://www.zotero.org/support/preferences/hidden_preferences#translator_preferences1


linxz...@gmail.com

unread,
Aug 16, 2020, 11:16:58 AM8/16/20
to zotero-dev
Thank you for your  hint, Dan Stillman
Reply all
Reply to author
Forward
0 new messages