Creating a note to consolidate partial information from other notes

Some help or advice please?

I currently have a database that covers a number of separate small projects. The projects are set up as individual groups within the database. Within each group (project) I update a formatted note, within each group with my progress notes, and my ‘what to do next’ priorities.
Is there any way to automatically extract the ‘what to do next’ portion of each note, into another single note/file, without going through each file individually and cutting and pasting?
If this isn’t possible is there a way I can set up a database, or a number of databases, that will create this consolidated list of priorities in a more logical and workable way than I’m doing now?

Should be possible via AppleScript. How do you separate the “progress notes” part and the “what to do next” part in the notes?

Thanks pete31. It’s separated by a header in the note.

How do you make a header in a formatted note? I never use them …

A header is just a line of text e.g.

This is a header

information written here

How should a script know what to extract when a header is just a line of text? What text do you use in your header?

Err I don’t know pete31, it was you suggested that AppleScript might work surely?
Anyway, it doesn’t sound like the AppleScript approach will work in this context, but thanks anyway.

I’ll see if there are any other suggestions.

It can be easily done in AppleScript, but there must be some anchor in the text that can be used to tell where each part starts. If you could create an sample formatted note and upload it I’ll see if it’s possible.

And if it’s not possible with your notes in their current state you could manually add some structure to them. Afterwards it’s sure possible :slight_smile:

You can, of course, but a script is the only way

The language doesn’t matter. The structure (or rather the lack thereof) does matter. If your header is always the same, you can programmatically search for it. If it changes but always begins with the same characters, you can programmatically search for it.
Otherwise, ask yourself how you recognize this header. If you can explain that, it might be programmable.

Thanks again pete31, and now chrillek, it’s starting to make sense now.
I use the same header in each, it is simply to do/priorities

It is the heading, and list below that heading, that I want to extract from each note and make a new note …

The content and structure of the formatted note is as follows:

Progress Notes

This is a sample of what progress I have made etc etc
12/6/2019
31/9/2020

To do/priorities

  1. First priority
  2. Second to-do
  3. Third to do
1 Like

Where should the new record be created? Should it be also a formatted note?

And do your existing formatted records share a unique name or do you use this name also somewhere else? If the name is not unique is there something else that could be used in the script to find you “to do” formatted notes, (e.g. do you only use formatted notes for your “to do” notes).

I’m easy about where it is created and what type of file it is as long as its ‘easily’ done/scripted.
Ideally it would be created in the same database, and yes a formatted note would be great, but any format that can be read by myself is OK.

I need to know how the script can find your formatted notes :slight_smile:

Each note starts with a unique name i.e “UniqueName Progress Notes”

Do you just want to collect all to dos or should the name of each formatted note should be included?

The name of each formatted note should be included as this would be easier to contextualise.

1 Like

Should the name of each formatted note act as a link in the new record? This could be handy if you want to update your progress in the formatted notes.

If so, should the URL of each formatted note act as link to the new record or do you use the URL already for something else?

Don’t currently use urls and the linking would be good, makes sense.

This script collects all “To do/priorities” parts and creates a new markdown record in the database of the first search result.

You’ll find one line that begins with a #. If you want a bulleted list instead of the plain one the script currently produces just remove the # and add one to the line above.

-- Write "To do/priorities" text of search results into new markdown record

-- CAUTION: This script OVERWRITES the URL of every record in the search results! 
-- This is done in order to always link back to the newest markdown record that the script creates

tell application id "DNtp"
	try
		set theResults to search "kind:formattednote {any: name:>Progress Notes name:>Progress Notes.html}"
		if theResults = {} then error "Nothing found."
		
		set theToDoRecord to create record with {name:"All To do/priorities", type:markdown, plain text:""} in incoming group of (database of item 1 of theResults)
		set theToDoRecord_RURL to reference URL of theToDoRecord
		
		set allPriorities to {}
		set allNames to {}
		
		repeat with thisRecord in theResults
			set theText to plain text of thisRecord
			set theTextItems to my tid(theText, ("To do/priorities" & linefeed & linefeed) as string)
			if (count theTextItems) > 1 then
				set thePriorities to item 2 of theTextItems
				if thePriorities ends with linefeed then set thePriorities to (characters 1 thru -2 in thePriorities) as string
				set end of allPriorities to thePriorities
				set theName to my recordName(name of thisRecord, filename of thisRecord)
				set theName_clean to my replaceString(theName, " Progress Notes", "")
				set theReferenceURL to (reference URL of thisRecord) as string
				set theLinkName to ("[" & theName_clean & "](" & theReferenceURL & ")") as string
				set end of allNames to theLinkName
				set URL of thisRecord to theToDoRecord_RURL
			end if
		end repeat
		
		set theTextParts_list to {}
		
		repeat with i from 1 to (count allNames)
			set thesePriorities_clean to {}
			set theParagraphs to paragraphs of item i in allPriorities
			repeat with thisParagraph in theParagraphs
				set end of thesePriorities_clean to ((thisParagraph as string) & space & space) as string
				#set end of thesePriorities_clean to "* " & ((thisParagraph as string) & space & space) as string
			end repeat
			set thesePriorities_clean_string to my tid(thesePriorities_clean, linefeed)
			set theName to item i in allNames
			set thisPrioritiesPart to ("### " & theName & linefeed & linefeed & thesePriorities_clean_string & linefeed) as string
			set end of theTextParts_list to thisPrioritiesPart
		end repeat
		
		set theHeader to "## All To do/priorities"
		set theText to (theHeader & linefeed & linefeed & my tid(theTextParts_list, linefeed)) as string
		set plain text of theToDoRecord to "<style> a {text-decoration: none;} </style>" & linefeed & linefeed & theText
		open window for record theToDoRecord
		activate
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

on recordName(theName, theFilename)
	set theSuffix to my getSuffix(theFilename)
	if theName ends with theSuffix and theName ≠ theSuffix then set theName to characters 1 thru -((length of theSuffix) + 2) in theName as string
	return theName
end recordName

on getSuffix(thePath)
	set revPath to reverse of characters in thePath as string
	set theSuffix to reverse of characters 1 thru ((offset of "." in revPath) - 1) in revPath as string
end getSuffix

on replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

2 Likes

Thank you so much pete31 for your support and help with this. I’ve clocked off for today but will give it a whirl tomorrow.

1 Like