Wednesday, November 4, 2009

Generating local-url fields in BibDesk

Here's a script that I use to generate the local-url fields for my BibDesk databases necessary for my own scripts to be able to access linked files. The script is only a very minor modification of something I found a while ago on the BibDesk wiki. Also, it is still not perfect, as it would overwrite local-url fields very time it is run. I just don't have the time and motivation to fix that right now :-(

(*
Add Local-Url fields to all publications, containing a relative (if possible) path to the file encoded in the Bdsk-File-1 entry.
*)

tell application "BibDesk"
        activate
        set theDoc to get first document
        set theFile to (get file of theDoc)
        set theDocDir to my containerPath(get file of theDoc)
        tell theDoc
                set thePublications to displayed publications
                repeat with currentpub in thePublications
                        tell currentpub
                                set theID to get id
                                set the value of field "Local-Id" to theID
                                if ((count of (get linked files)) > 0) and ((get linked file 1) is not missing value) then
                                        set linkedFile to linked file 1
                                        tell linkedFile
                                                set thePath to get POSIX path
                                        end tell
                                        --set thePath to my relativePath(thePath, theDocDir)
                                        (* we might want to change the following line to a conditional, 
                                        to prevent that manually changed local-urls get overwritten when 
                                        this script is used again *)
                                        set the value of field "Local-Url" to thePath
                                end if
                        end tell
                end repeat
        end tell
end tell

on relativePath(fullPath, basePath)
        set theLength to length of basePath
        if (length of fullPath > theLength) and (text 1 thru theLength of fullPath) = basePath then
                return text theLength thru end of fullPath
        else
                return fullPath
        end if
end relativePath

on containerPath(theFile)
        set AppleScript's text item delimiters to "/"
        set theComponents to text items of (POSIX path of theFile)
        if last item of theComponents is "" and (count of theComponents) > 2 then
                set theComponents to items 1 thru -3 of theComponents
        else
                set theComponents to items 1 thru -2 of theComponents
        end if
        return (theComponents as text) & "/"
end containerPath

0 comments:

Post a Comment

Comments are moderated to prevent abuse and may not be made public immediately.


Björn Rüffer, Copyright 20092011
Disclaimer: The author of this page cannot accept any responsibility for content of pages that this page provides hyperlinks to. Also the author cannot accept responsibility or liability for any damage caused by following or not following instructions given on this page. This page, its contents and style, are the responsibility of the author and do not represent the views, policies or opinions of any other party. The author will not be held responsible for comments posted by third parties on this site. Inappropriate comments will be removed. There is no warranty for any program or source code provided here, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the program “as is” without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.