Created
January 7, 2016 14:01
-
-
Save konn/bc7b5c45f1bd1b8b1bca to your computer and use it in GitHub Desktop.
arXiv to BibDesk-BibLaTeX Apple Script
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
property arxivPrefixes : {"http://www.arxiv.org/", "http://arxiv.org/", "https://www.arxiv.org/", "https://arxiv.org/"} | |
on run | |
tell application "Safari" | |
set theURL to URL of the first document as string | |
end tell | |
set pathComps to split("/", nthElement(arxivPrefixes, 2, theURL)) | |
set c to count of pathComps | |
main(join("/", items 2 thru c of pathComps)) | |
end run | |
on parseDate(val) | |
set dateTxt to val as string | |
set oldDelim to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to {"T"} | |
set datePart to the first text item of dateTxt | |
set AppleScript's text item delimiters to oldDelim | |
return datePart | |
end parseDate | |
on main(ident) | |
tell application "System Events" | |
log ident | |
set tmpPath to path to temporary items from system domain | |
set tmpPathPOSIX to POSIX path of tmpPath | |
set rndName to do shell script "uuidgen" | |
set tmpFile to tmpPathPOSIX & "/" & rndName & ".xml" | |
set uri to ("http://export.arxiv.org/api/query?id_list=" & ident) | |
set src to do shell script ("curl -s '" & uri & "' > " & tmpFile) as text | |
try | |
set xmlDoc to XML file tmpFile | |
tell the first XML element of xmlDoc | |
set theEntry to XML element "entry" | |
tell theEntry | |
set theTitle to (the value of the XML element "title") as text | |
set updated to parseDate(the value of the XML element "updated") of me | |
set published to parseDate(the value of the XML element "published") of me | |
set abst to the value of the XML element "summary" as string | |
set cat to the value of the XML attribute "term" of the XML element "arxiv:primary_category" as string | |
set theJournal to "" | |
if the (count of (every XML element whose name is "arxiv:journal_ref" as list)) > 0 then | |
set theJournal to the value of the XML element "arxiv:journal_ref" | |
end if | |
set now to current date | |
set dois to (every XML element whose name is "arxiv:doi") as list | |
set theDoi to "" | |
if (count of dois) > 0 then | |
set theDoi to the value of the XML element "arxiv:doi" as string | |
end if | |
set visited to {the year of now, the month of now as number, the day of now} | |
set authorItems to every XML element whose name is "author" | |
set theAuthors to {} | |
repeat with anAuthor in authorItems | |
set theAuthors to theAuthors & {the value of the first XML element of anAuthor as Unicode text} | |
end repeat | |
set revision to my nthElement("v", 2, my nthElement("/", 2, ident)) as number | |
tell the first document of application "BibDesk" | |
set authorTxt to my join(" and ", theAuthors) | |
set props to {title:theTitle, abstract:abst} | |
set pub to make new publication with properties props at the beginning of publications | |
tell pub | |
if theJournal is not "" then | |
set type of pub to "article" | |
set the value of field "journaltitle" to theJournal | |
else | |
set type of pub to "online" | |
end if | |
set the value of field "date" to updated | |
set the publication year to my nthElement("-", 1, updated) | |
set the publication month to my nthElement("-", 2, updated) | |
set the value of field "day" to my nthElement("-", 3, updated) | |
set the value of field "author" to authorTxt as Unicode text | |
set the value of field "eprinttype" to "arxiv" | |
set the value of field "urldate" to my join("-", visited) | |
set the value of field "eprint" to ident | |
if theDoi is not "" then | |
set the value of field "doi" to theDoi | |
end if | |
if cat is not "" then | |
set the value of field "eprintclass" to cat | |
end if | |
set the value of field "version" to revision | |
set cite key to its generated cite key | |
show | |
end tell | |
end tell | |
end tell | |
end tell | |
on error e | |
log ("error! " & e) | |
end try | |
do shell script ("rm " & tmpFile) | |
tell application "BibDesk" to activate | |
end tell | |
end main | |
on join(s, targ) | |
set oldDelims to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to s | |
set answer to targ as string | |
set AppleScript's text item delimiters to oldDelims | |
return answer | |
end join | |
on split(s, targ) | |
set oldDelims to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to s | |
set theList to every text item of targ | |
set AppleScript's text item delimiters to oldDelims | |
return theList | |
end split | |
on nthElement(s, n, targ) | |
set theList to split(s, targ) | |
if (count of items of theList) ≥ n then | |
set targ to item n of theList | |
else | |
set targ to "" | |
end if | |
return targ | |
end nthElement |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment