Updating reference links to Bates numbered documents

Typically after several months of research I will have documents which look like this:

  1. Fernando Veragio found the bloody knife Page40
  2. Inspector Spelozio bagged the knife x-devonthink-item://04F430E0-66C8-412C-8C28-9799F356A14C?page=23
  3. Crime lab analysed the knife Page453

Where the links point to a host of documents that will at some point be Bates numbered and used in trial.

I use my notes directly as chronologies and as part of argument which will be printed out and handed into the court. This involves tediously going through each reference, looking it up, and then substituting the Bates number for each reference. Not fun when there are hundreds of references

This script runs through a text document and substitutes the text of the link for the Bates number (calculated to the correct page) using the numbering scheme stored in a spotlight comment I describe in this thread [url]Bates numbering of pdfs from within Devonthink]

-- This script updates the text of devonthink references to match the virtual numbering in the spotlight comment of the referenced document
-- Where the source reference does not have a bates number in the spotlight comment field, the reference is skipped over
-- Usage run this script while the document containing the references is open.

--Change these parameters to match the bates numbering scheme as explained in this thread
-- https://discourse.devontechnologies.com/t/bates-numbering-of-pdfs-from-within-devonthink/17469/1
set prefix_length to 2
set number_length to 6

set marker_position to prefix_length + number_length + 1
set resetvalue to false

tell application ID "DNtp"
	tell text of think window 1
		repeat with theWord in words
			set k to properties of theWord
			if URL of k is not missing value then
				set theURL to (URL of k) as text
				set onlypagerefs to (characters 57 through 61) of theURL as string
				if onlypagerefs is "?page" then
					set theCurrentPage to ((characters 63 through -1) of theURL) as string
					-- have to do double step because as integer would coerce a list
					set theCurrentPage to theCurrentPage as integer
					set theUUID to (characters 21 thru 56 of theURL) as string
					set theRecord to (get record with uuid theUUID)
					set this_comment to the comment of theRecord
					if this_comment is not missing value and length of this_comment ≥ marker_position then
						if character (prefix_length + number_length + 1) of this_comment is "_" then
							set thePage to ((characters (prefix_length + 1) through (marker_position - 1)) of this_comment) as string
							set thePage to thePage as integer
							set thePage to thePage + theCurrentPage as number
							set thePage to thePage as string
							set thePage to texts -(number_length) thru -1 of ("000000" & thePage)
							set InsertPage to (characters 1 through prefix_length) of this_comment & thePage as string
						end if
						if resetvalue is true then
							set InsertPage to "XXX"
						end if
						set properties of theWord to {text:InsertPage}
						set resetvalue to true
					end if
				end if
				set resetvalue to false
			end if
		end repeat
	end tell
end tell

The end result looks like this:

  1. Fernando Veragio found the bloody knife A000056
  2. Inspector Spelozio bagged the knife AA000167
  3. Crime lab analysed the knife BB000453

a) References composed of more than one word, such as Page 2 can’t be removed. Instead the first reference is corrected to the bates numbers and the subsequent duplicate reference is substituted by XXX. This must be removed manually
b) Where references refer to documents that dont have a bates number, they are skipped over.
c) The script can be repeated at any time should the bates numbering scheme be changed
d) Only references to pages are substituted. Search links are not changed.

I know this may seem like a very obscure use case to most people, who having gone electronic, see no need to revert to a different referencing system. Nevertheless I think the general approach may of use to academics as well who want to put the correct citations into their research notes.