batch move PDF files out of reference items to the top level

edited April 10, 2024
I imported many PDF files from another program. The meta information was not extracted from these PDF files probably by the other program, and now what I have in Zotero is like this:

<<pdf_file_1.pdf>>
---pdf_file_1
<<pdf_file_2.pdf>>
---pdf_file_2
<<pdf_file_3.pdf>>
---pdf_file_3

Basically the only meta information is the title of the PDF files.

I want to move these PDF files out of the reference items to the top level, so that the meta information can be extracted. Manually dragging individual files is tedious. I wonder if there are any automatic method to achieve this.

Thanks.
  • You can select all the parent items and run this from Tools → Developer → Run JavaScript:

    await Zotero.DB.executeTransaction(async function () {
    var items = ZoteroPane.getSelectedItems();
    for (let item of items) {
    if (!item.isRegularItem()) continue;
    let attachment = await item.getBestAttachment();
    attachment.parentID = null;
    await attachment.save();
    item.deleted = true;
    await item.save();
    }
    });


    This will turn all the primary child attachments of the selected items into top-level items and move the selected parent items to the trash. It won't handle child notes or other child attachments.

    You should temporarily disable auto-syncing and make a backup of zotero.sqlite in your data directory before running this, and make sure it did what you want before re-enabling syncing.
  • edited April 11, 2024
    (An alternative approach without code would be to just construct an advanced search that matches only the child items (e.g., "Attachment File Type" is "PDF"), do a Select All in that window, press Enter to select all those attachments in the main window, and then drag them out of their parents together. You'd then have to delete the parent items. But this might be more complicated if you have combinations of standalone and child attachments in your library, or some attachments that are already correct.)
  • Hi, dstillman,

    Thanks for your reply. Your JavaScript solution works great.

    Another question: when I dragged a PDF file to an item from the center panel, the PDF file appears inside that item as a child item, but a copy of the PDF file still remains in the center panel. If I delete the PDF file in the center panel, the PDF inside the item (the child item) is also deleted. It seems that they are linked.

    How to move a PDF file in the center panel into an item?
Sign In or Register to comment.