Search code examples
applescriptbibtexfdf

BibDesk script to link Skim annotations to citation entry


Some Background: I've used Skim with BibDesk for a while now for reading and annotating scientific journal articles. Recently, I purchased an android tablet and would like to use it to read and annotate .pdfs as well. I use the reference library Eratosthenes with ezPDF Reader and sync all files through Dropbox. The issue I'm having is that Skim stores annotations as extended attribute files by default, which are not accessible to other devices through Dropbox. I've worked around this problem by saving the annotations as .fdf files and then linking the .fdf files to the citation entry in BibDesk. ezPDF reader can import .fdf files as annotations, and if the annotation file is linked to the BibDesk entry, both the .pdf and .fdf can be easily downloaded to the tablet without needing to sync the entire Dropbox folder full of hundreds of references.

I'd like to write an applescript that does this automatically, but am very new to applescript and am having a hard time getting started. I've written the following script to be executed when "command-s" is pressed while in skim:

tell application "Skim"
    set docPath to path of front document
    set notesPath to text 1 thru -5 of docPath & ".fdf"
    save front document
    save front document in notesPath as "Notes as FDF"
end tell

This essentially saves the current document while simultaneously exporting an .fdf file. Within the same script, I would like to link the .fdf file to the appropriate citation entry in BibDesk. This would involve:

  • determining the citation entry name associated with the .pdf (maybe search through entries to locate one linked with the front document)
  • check to see if the .fdf file is already linked to it
  • if not, attach .fdf file

I haven't been able to find someone who's done something similar, and really can't get past the first step. I tried writing something basic (assume citation entry is highlighted, assume .fdf file is not linked), which produces no results:

tell application "BibDesk"
   set thePub to selection
   tell thePub
        set theFieldName to "Local-URL-2"
    set value of field theFieldName to fdfPath
   end tell
end tell

Is anyone familiar with Bibdesk applescripts able to help me with the second part of this code?

Thank you very much in advance.


Solution

  • The following code answers my initial question satisfactorily. The first section reiterates the saving and exporting commands. The second section locates the citation entry containing the linked .pdf file (front document in Skim). The third section attaches (links) the .fdf file to the citation entry (thanks to CRGreen for some help here).

    --save and export .fdf file
    tell application "Skim"
        set docPath to path of front document
        set fdfPath to text 1 thru -5 of docPath & ".fdf"
        save front document
        save front document in fdfPath as "Notes as FDF"
    end tell
    
    --search for relevant citation entry
    tell document 1 of application "BibDesk"
        --sort all publications in library by Cite Key
        set thePubs to (sort (get publications) by "Cite Key")
        -check each publication individually (surely this is not the most efficient way to do this)
        repeat with aPub in thePubs
            --check to see if the .pdf is in the citation entry
            tell aPub
                if linked files contains (POSIX file docPath) then
                    set thePub to aPub
                    --once the citation is found, exit loop
                    exit repeat
                end if
            end tell
        end repeat
    
        --link the .fdf file to the citation entry (if it isn't already)
        tell thePub
            --if the fdf file exists in the linked file, do nothing
            if linked files does not contain (POSIX file fdfPath) then
                add (POSIX file fdfPath) to end of linked files
            end if
        end tell
    end tell
    

    I'm assuming that there is a better way to search for the citation entry with the associated .pdf file (maybe using the applescript search command?). This solution works for me, but if you know of a more elegant way to solve this problem, feel free to mention it.

    To map the script to a keyboard shortcut ("command-s" is convenient), see here (the menu title is the name of your script). The script needs to first be saved to the ~/Library/Application Support/Skim/Scripts directory.

    I'm just learning applescript, and I realize that this may have been a very trivial exercise for most, but perhaps it will help out another beginner.