Sunday, April 18, 2010

Create BibTeX Entry from Safari Document with BibDesk

I often have to reference webpages from my papers. I use bibtex and in order to reference a webpage I define a new bibtex entry of type webpage. Since I do not want to type in these entries manually, and thanks to BibDesk, a small applescript does the trick for me:
-- (C) 2010 Jens von Pilgrim
tell application "Safari"
 set theTitle to name of front document
 set theURL to URL of front document
end tell

set theDate to (year of (current date)) & "-"
set theMonth to (month of (current date)) * 1
if theMonth < 10 then
 set theDate to theDate & "0"
end if
set theDate to theDate & theMonth & "-"
set theDay to day of (current date)
if theDay < 10 then
 set theDate to theDate & "0"
end if
set theDate to (theDate & theDay) as text

display dialog "Key for webpage " & theTitle & ": " default answer ""
set dlgResult to result

if (button returned of dlgResult = "OK") then
 set theKey to text returned of dlgResult
 
 tell application "BibDesk"
  if (count of documents) = 0 then make new document
  
  tell document 1
   set newPub to make new publication at end of publications
   set type of newPub to "webpage"
   set cite key of newPub to theKey
   set the value of field "Key" of newPub to theKey
   set the value of field "Type" of newPub to "URL"
   set the value of field "Url" of newPub to theURL
   set the value of field "Lastchecked" of newPub to theDate
   set the value of field "Title" of newPub to "{" & theTitle & ", Project Website}"
   
   show newPub
  end tell
  
 end tell
end if
This script grabs the URL from the currently active Safari document and creates a new bibtex entry. Some field are automatically filled, including "Type" which is required by some styles I use.The script asks for the key of the new entry, as this cannot be determined automatically (at least, I don't want the script to make a false guess). I added the script to the BibDesk script folder (at ~/Library/Application Support/BibDesk/Scripts), so it is accessible from within BibDesk. Example: The script produces for http://bibdesk.sourceforge.net/ the following entry:
@webpage{BibDesk,
 Date-Added = {2010-04-18 13:40:38 +0200},
 Date-Modified = {2010-04-18 13:40:38 +0200},
 Key = {BibDesk},
 Lastchecked = {2010-04-18},
 Title = {{BibDesk, Project Website}},
 Type = {URL},
 Url = {http://bibdesk.sourceforge.net/}}

2 comments:

Stephan said...

Great script! Exactly what I was looking for! Thanks.

krieg said...

The script works well.
I want just to add the fact that the extension of the file must be scpt.

Thanks for this script