Skip to content

Instantly share code, notes, and snippets.

@frafra
Created August 21, 2023 12:22
Show Gist options
  • Save frafra/4f24879549e1cdba69de26922d987342 to your computer and use it in GitHub Desktop.
Save frafra/4f24879549e1cdba69de26922d987342 to your computer and use it in GitHub Desktop.
zotero cookbook
// Add incremental Call Number
let items = Zotero.getActiveZoteroPane().getSelectedItems();
let callNumberLength = items.length.toString().length;
for (i=0; i<items.length; i++) {
let item = items[i];
if (!items[i].isRegularItem()) continue;
item.setField("callNumber", (i+1).toString().padStart(callNumberLength, "0"));
await item.saveTx();
}
// Rename all PDF attachments
let items = Zotero.getActiveZoteroPane().getSelectedItems();
for (i=0; i<items.length; i++) {
let item = items[i];
if (!item.isRegularItem()) continue;
let attachmentIDs = item.getAttachments();
for (j=0; j<attachmentIDs.length; j++) {
let attachmentID = attachmentIDs[j];
if (!attachmentID) continue;
let attachment = Zotero.Items.get(attachmentID);
if (!attachment.isPDFAttachment()) continue;
// Custom filename
let callNumber = item.getField("callNumber");
let title = item.getField("title");
if (title.length > 50) title = title.substring(0, 50) + "...";
let year = item.getField("year");
let newFilename = callNumber + "_" + title + "_" + year + ".pdf";
// Save changes
attachment.renameAttachmentFile(newFilename);
await attachment.saveTx();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment