Skip to content

Instantly share code, notes, and snippets.

@zhangtemplar
Created June 18, 2023 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangtemplar/1059855d12c7d892cab43855eb31705c to your computer and use it in GitHub Desktop.
Save zhangtemplar/1059855d12c7d892cab43855eb31705c to your computer and use it in GitHub Desktop.
Automatically link arxiv pdf in zotero
var arxiv_url_pattern = "arxiv.org/abs/";
const LINK_MODE_LINKED_URL = 3;
var s = new Zotero.Search();
s.libraryID = ZoteroPane.getSelectedLibraryID();
s.addCondition('url', 'contains', arxiv_url_pattern);
var ids = await s.search();
if (!ids.length) {
return "No items found";
} else {
Zotero.warn("Find " + ids.length + " items");
}
await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
let item = await Zotero.Items.getAsync(id);
Zotero.warn("start: " + item.getField("title"));
try {
if (item.getAttachments().length > 0) {
Zotero.warn("already have attachment, skip");
continue;
}
} catch (error) {
// attachment types.
Zotero.warn("invalid item type, skip " + error);
continue;
}
let url = item.getField("url");
let file_url = url.replace("arxiv.org/abs/", "arxiv.org/pdf/") + ".pdf";
await Zotero.Attachments.addPDFFromURLs(item, [file_url]);
await item.save();
Zotero.warn("done: " + item.getField("title"));
}
});
return ids.length + " item(s) updated";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment