http://bjoernstechblog.rueffer.info/posts/bibdesk/applescript/2009/11/05/Generating-local-url-fields-in-BibDesk/
last updated on 25 May 2018

05 November 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
Björn Rüffer — Copyright © 2009–2018 — bjoern.rueffer.info