Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set BibDesk URLs to ShortDOI shortcuts
(*
Set BibDesk URLs to shortDOI shortcuts
Kurt Raschke
kurt@kurtraschke.com
Licensed under the BSD license
*)
on run
tell application "BibDesk"
tell document 1
if (name of every static group) contains "Bad DOI" then
set theGroup to static group "Bad DOI"
else
set theGroup to make new static group with properties {name:"Bad DOI"}
end if
end tell
if the length of ((theGroup's publications) as list) is greater than 0 then
tell theGroup to remove publications of theGroup
end if
set theItems to publications of document 1 whose value of field "DOI" is not "" and ¬
value of field "URL" does not start with "http://doi.org/"
repeat with thePub in theItems
set theDOI to the value of field "DOI" of thePub
set theShortDOI to getShortDOI of me for theDOI
if theShortDOI is not missing value then
set theShortcut to (text 4 through end) of theShortDOI
set newURL to "http://doi.org/" & theShortcut
else
set newURL to ""
add thePub to theGroup
end if
if the value of field "URL" of thePub is not equal to newURL then
set the value of field "URL" of thePub to newURL
end if
end repeat
end tell
end run
to getShortDOI for theDOI
set theRequest to "http://shortdoi.org/" & urlEncode(theDOI) & "?format=xml"
set theCommand to "curl " & (quoted form of theRequest)
set theResult to do shell script theCommand
tell application "System Events"
set xmlNode to make new XML data with properties {text:theResult}
try
return value of first XML element in (first XML element in xmlNode) whose name is "ShortDOI"
on error
return missing value
end try
end tell
end getShortDOI
to urlEncode(theURL)
set badChars to {36, 38, 43, 44, 58, 59, 61, 63, 64, 32, 34, 60, 62, 35, 37, ¬
123, 125, 124, 92, 94, 126, 91, 93, 96}
set encodedURL to ""
repeat with theCharacter in characters of theURL
set charCode to ASCII number of theCharacter
if charCode is in badChars or (charCode is greater than or equal to 0 and ¬
charCode is less than or equal to 31) or ¬
(charCode is greater than or equal to 127 and ¬
charCode is less than or equal to 255) then
set encodedURL to encodedURL & "%" & getHex(charCode)
else
set encodedURL to encodedURL & theCharacter
end if
end repeat
return encodedURL
end urlEncode
to getHex(theNumber)
set hexCharacters to "0123456789ABCDEF"
set remainder to theNumber mod 16 as integer
set outCharacters to character (remainder + 1) of hexCharacters
if (remainder - theNumber) as integer is not equal to 0 then
set outCharacters to getHex((theNumber - remainder) / 16) & outCharacters
end if
return outCharacters
end getHex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.