Quantcast
Channel: DEVONtechnologies Community - Latest posts
Viewing all articles
Browse latest Browse all 16117

Changing links to a note

$
0
0

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

Viewing all articles
Browse latest Browse all 16117

Trending Articles