A niche Day One/DEVONThink script to substitute links

This is probably a niche script which nobody else will find useful as a whole. However, I have learned so much from this forum by reading scripts posted by others I thought I might post this in the hope that some may find parts of it useful.

In essence (see the summary at the start of the script) it takes an imported Day One journal record in markdown format and loops through the record substituting the Day One links with DT links. It does rely on the user manually inputting the date of each Day One link so is by no means as sophisticated at the JavaScript posted here by a Very Helpful Member. However, it will pick up multiple Day One links in a single diary record.

As I’ve managed to convert all Day One links in existing imported journal entries I now import new entries every couple of weeks or so and simply run through them with this script updating the links for use in DT.

If you recognise in the script portions of your own programming thank you for your help and sorry for not specifically acknowledging you!

(* This script replaces Day One links in a single selected diary record
with DEVONThink links. It assumes each diary record is markdown
with the name of the record in the format "Thursday 8 July 2021".
The name format can be changed in the getName handler. You are invited to
input the date of the linked record - obtained manually from checking
the Day One link in Day One - and the script then substitutes
the DEVONThink link for the Day One link in the selected
diary record, looping through the record until all Day One links
have been replaced. *)

-- Set the the name of the database you want to use
property pDatabase : "~/DEVONThink/something.dtBase2"

tell application id "DNtp"
	try
		set theDatabase to open database pDatabase
		-- Test for selection of single markdown record
		set theSelection to the selection
		if (count of theSelection) is 1 then
			set theRecord to the content record
			if (type of theRecord) is markdown then
				-- Set up a repeat loop to replace Day One links with DEVONThink links
				repeat
					set theText to plain text of theRecord
					if theText contains ("dayone://") then
						-- Ask for the date of the target record in the Diaries database
						-- and provide clear guidance as to the format required
						set dAnswer to "01/07/2020"
						set userEntry to display dialog ¬
							"Enter the link date as DD/MM/YYYY" default answer dAnswer
						-- Now reformat it to match the name of a diary entry record
						-- Here we use the getName handler to construct
						-- a name like "Thursday 8 July 2021"
						set theTarget to my getName(userEntry)
						-- Search for the named record in the database
						set foundRecord to (search "name:" & theTarget & " kind:markdown") in theDatabase
						if foundRecord = {} then
							error theTarget & " was not found in " & pDatabase
						end if
						-- Get the UUID of the record
						set theUUID to uuid of item 1 of foundRecord
						-- Assemble the DEVONThink link
						set theDTLink to "x-devonthink-item://" & theUUID
						-- Extract part of the Day One link…
						set theDOurl to my extractBetween(theText, "dayone://", ")")
						-- …and assemble it
						set theDOLink to "dayone://" & theDOurl
						-- Now replace the Day One link with the DT link
						set theText to my replaceText(theText, theDOLink, theDTLink)
					else
						error "There are no more Day One links in the selected record."
					end if
					-- Set the record to the amended text
					set plain text of theRecord to theText
				end repeat
			else
				error "Please select a single diary markdown record."
			end if
		else
			error "Please select a single diary record."
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on getName(targetDate)
	--  Format the name of the target
	set userDate to text returned of targetDate
	set linkDate to date userDate
	set wDay to weekday of linkDate
	set dateDay to day of linkDate
	set dateMonth to month of linkDate
	set dateYear to year of linkDate
	set theName to (wDay & " " & dateDay & " " & dateMonth & " " & dateYear) as string
	return theName
end getName

on extractBetween(SearchText, startText, endText)
	set tid to AppleScript's text item delimiters -- save them for later.
	set AppleScript's text item delimiters to startText -- find the first one.
	set endItems to text of text item 2 of SearchText -- everything after the first.
	set AppleScript's text item delimiters to endText -- find the end one.
	set beginningToEnd to text of text item 1 of endItems -- get the first part.
	set AppleScript's text item delimiters to tid -- back to original values.
	return beginningToEnd -- pass back the piece.
end extractBetween

on replaceText(theString, find, replace)
	-- with acknowledgment and thanks to @cgruenberg
	-- https://discourse.devontechnologies.com/t/search-replace-text-in-rtf-document/65757/10
	local od
	set {od, text item delimiters of AppleScript} to {text item delimiters of AppleScript, find}
	set theString to text items of theString
	set text item delimiters of AppleScript to replace
	set theString to "" & theString
	set text item delimiters of AppleScript to od
	return theString
end replaceText

Stephen

1 Like

Thanks for sharing!

The question is: Are you pleased with how it turned out?

Extremely pleased: very happy - but didn’t want to revel in it too much in case it sounded egotistical!

Stephen

1 Like

To me, the sense of accomplishment is part of the joy of automating.

For years I have said, “There are two types of computer users: Ones that do what the computer will let them do, and those who make the computer do what they want it to.” You have crossed the great divide into the latter group. :slight_smile:

You should see some of the comments in some of my personal scripts. I pat myself on the back when it’s warranted. Nothing wrong with that. :slight_smile:

3 Likes