Created
August 21, 2023 12:22
-
-
Save frafra/4f24879549e1cdba69de26922d987342 to your computer and use it in GitHub Desktop.
zotero cookbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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