Help to modify AppleScript inside DEVONthink 3 to process Markdown

Hi everyone,

New user, first post here. :slight_smile:

I am using DEVONthink 3 to extract my pdf highlights as markdown. I need help with AppleScript.

In short 2 things:

1)get the text part of H1, which is a link.
H1 is the first line in the file: “# I.Eliacik
Extract text from within the first occurrence of “[ ]” in this example "“I.Eliacik”

2)Change all "(Page " instances with the extracted text, in this case with "(I.Eliacik "

The apple script itself looks like this:

 
on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			if (type of theRecord as string) = "markdown" then
				-- Only process Markdown docs
				
				set newText to {}
				-- Set up an empty list to push the paragraphs into)
				
				set currentText to (plain text of theRecord)
				-- Get the current text of the document
				
				repeat with thisLine in (paragraphs of currentText)
					-- Loop through the document, paragraph by paragraph
					
					if thisLine begins with "##" then
						-- If this is an H2 page link
						set cachedText to (characters 4 thru -1 of thisLine as string) -- Cache all but the control characters in a variable
						
					else if thisLine begins with "* " then
						-- If it's just a bulleted paragraph 
						
						copy ((characters 3 thru -1 of thisLine as string) & " (" & (cachedText) & ")  " & return) to end of newText
						-- Get all but the control characters and concatenate the cachedText.
						-- Then push it to the newText list
					else
						copy thisLine & return to end of newText
						-- Anything else, including the H1 line at the top, just push into the newText list
					end if
				end repeat
				
				set plain text of theRecord to (newText as string)
				-- Set the text of the document to the new text
				
			end if
		end repeat
	end tell
end performSmartRule

MORE DETAILS:

A user called BLUEFROG shared a script that processes the markdown file into a more useful form:

Which transforms the file like this: (original on left) (for some reason I could not embed image links, that is why I am posting the URLs for them, I am sorry)

https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/8/85006c0a3eb1717e94ecec990c25f3fa6075175d.jpeg

But instead of this:

https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/6/2/6285a663befa3ee73f4abf5bbfc3887a9da00ce8.png

I need them to be like this:
https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/3/b/3bcfe6f5b69422f4884d49445f48407c89b0faac.png

So basically “(Page 6)” is a link that opens page 6 of that pdf in DEVONthink3.
I need to get the text of the H1 which is “I.Eliacik” and change all "(Page " with "(I.Eliacik " without changing the link to page 6. (I.Eliacik is also a link to the pdf itself, not to a specific page. That is why I only want to get the text part of it, not the link)

The main reason for this is to directly be able to see which file I am referencing instead of seeing (Page 6) for every pdf.

You could see the original post and my question also at DEVONthink forum:
(the DEVONthink rule file which includes the apple script is also here )
https://discourse.devontechnologies.com/t/how-to-run-javascript-on-markdown-summarize-highlights-output/53375/8

And the apple script itself looks like this:
https://wtools.io/paste-code/b3Rt

If I can manage to do this I could stop using Zotero altogether, which would simplify my workflow a lot.

Any help is much appreciated. Peace.