Created
June 18, 2023 22:13
-
-
Save zhangtemplar/1059855d12c7d892cab43855eb31705c to your computer and use it in GitHub Desktop.
Automatically link arxiv pdf in zotero
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
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