Changing links to a note

Hey all,

I’ve been wondering about how to best change links to a note. Here’s the scenario. I have several markdown files, variously named such as “Guide”, “Honor Guide”, “Currents Guide”,… all generally about the same thing. Each one of them has links to them from various other notes in my database.

I’d like to consolidate the notes (Guide, Honor Guide, and Currents Guide) into a single markdown file and discard the previous notes. Is there a smooth path to doing so? I can do this manually for a small number of linked files, but not if it gets into the dozens.

One thought I had was to create a central note called “Guide” for instance and then add the names of the others as aliases to this central note. But then I lose any item links. Any thoughts?

  • Kourosh
1 Like

Scripting?

  • put all related Guides in a (smart) group
  • have the script read all files in that group and
    • for each file, get the list of incomingReferences associated with this file.
  • consolidate all files in a master document
  • for each list of incoming references, open the referrer and replace the old link with a link to the new master document.

Not sure if that’s what you’re asking for, though.

That sounds like it might be it? I’m not adept at creating scripts, so I’d have to do some studying there. If I understand it right - the script would do all of these steps?

Completely different, quick & dirty approach: transclude them all into a single Markdown file, Export or Convert (doesn’t much matter which) that file to RTF, and re-import to Markdown; then delete or archive the original files. Would that do what you need?

How would that adjust the incoming links to point to the new document?

It wouldn’t. Ignore me; I was losing sight of the original question. You’d need to perform some kind of batch search & replace for the IDs of the original MD files, swapping in the ID of the new consolidated file – which is pretty much what your scripting solution was proposing in the first place.

@NickLowe I appreciate the suggestion, but you’re right that it would likely need some sort of batch search and replace. I’m going to see what I can come up with

I realized that I might want to do something similar, so I just wrote a script that redirects references to selected markdown documents to a single master document. Warning: primitive script, with barely any error catching, is not tested at all.

-- NOT TESTED! You MUST test this script before applying it to your production documents.

-- What this script does:
-- 1. Redirect all incoming references of selected items to a master item.
-- 2. Update aliases of master item to include names and aliases of selected items, without duplicate detection.
-- 3. This script does not actually make any change to the selected items.

property masterItemLink : "insert-item-link-of-master-document" -- manual input before running script

tell application id "DNtp"
	if (get record with uuid masterItemLink) is missing value then return
	display dialog "This is an experimental script. Do you really want to continue?" buttons {"Continue", "Cancel"} default button "Cancel" cancel button "Cancel"
	set theSelection to selected records
	if theSelection is {} then return
	my UpdateItemLinkInReferences(theSelection)
	my UpdateAliasOfMaster(theSelection)
	-- open tab for record (get record with uuid masterItemLink) -- optionally, open the master item automatically for checking the results
end tell

on UpdateItemLinkInReferences(theRecords)
	tell application id "DNtp"
		set someDocNotMarkdown to false
		set referenceList to my FindAllIncomingReferences(theRecords)
		set replaceItemLinks to my ListItemLinks(theRecords)
		repeat with theRecord in referenceList
			if (type of theRecord) is markdown then
				try
					set theText to plain text of theRecord
					repeat with anItemLink in replaceItemLinks
						set theText to my ReplaceText(theText, anItemLink, masterItemLink)
					end repeat
					set plain text of theRecord to theText
				end try
			else
				set someDocNotMarkdown to true
			end if
		end repeat
		if (someDocNotMarkdown is true) then display dialog "Item links may not be replaced in documents that are not markdown."
	end tell
end UpdateItemLinkInReferences

on UpdateAliasOfMaster(theSelection)
	tell application id "DNtp"
		set masterRecord to get record with uuid masterItemLink
		set theAliases to (aliases of masterRecord)
		repeat with theRecord in theSelection
			set addAliases to (aliases of theRecord)
			if addAliases is not "" then
				set theAliases to theAliases & "," & (name of theRecord) & "," & addAliases
			else
				set theAliases to theAliases & "," & (name of theRecord)
			end if
		end repeat
		set (aliases of masterRecord) to theAliases
	end tell
end UpdateAliasOfMaster

on FindAllIncomingReferences(theRecords)
	tell application id "DNtp"
		set aList to {}
		try
			repeat with theRecord in theRecords
				set theReferences to (incoming references of theRecord)
				set aList to aList & theReferences
			end repeat
		end try
		return aList
	end tell
end FindAllIncomingReferences

on ListItemLinks(theRecords)
	tell application id "DNtp"
		set urlList to {}
		try
			repeat with theRecord in theRecords
				set theItemLink to "x-devonthink-item://" & (uuid of theRecord)
				if (theItemLink is not masterItemLink) then set urlList to urlList & theItemLink
			end repeat
		end try
		return urlList
	end tell
end ListItemLinks

on ReplaceText(theText, searchStr, replaceStr)
	local prevTID
	set prevTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchStr
	set theItemList to text items of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theItemList as string
	set AppleScript's text item delimiters to prevTID
	return theText
end ReplaceText

Hi @meowky Thank you for the attempt. I couldn’t get it to work. I replaced “insert-item-link-of-master-document” and it came up with some process, but there were no changes to the documents.

In any case, @chrillek came up with a solution here

Much appreciated.

  • Kourosh