Get Records Created On This Day via AppleScript

Hi! I enjoy On This Day feature in Day One and tried to replicate it in DEVONthink. So long I have been able to get records which matches today’s day and month, but I can’t get a prompt with all of them at once.

For test purposes, I created a new group with 4 markdown records. Three of them has today’s date. On line 18 I added a dialog prompt and it appears 3 times since the script found 3 records created today. So far, so good. I tried different syntax, but was unable to achieve what I want. Also, get all founded records, create a prompt with each button named of record name and by pressing one of buttons, the record will be opened in DEVONthink respectively.

Thanks to @DTLow for helping with syntax.

This is an early version of the script. I will add several if statements to check how many records were found. If only 1, then open directly without prompt. Otherwise, create a prompt with buttons. I store one note per day, so it should not be overload with buttons in my case.

Below are working script:

property GroupUUID : "" -- group UUID here
set currentDay to day of (current date) as number
set currentMonth to month of (current date) as number

tell application id "DNtp"
	if (exists database "All") then set theDB to database "All"
	try
		set theGroup to (get record with uuid GroupUUID)
	end try
	set theNewList to {}
	
	set theList to (get every child of theGroup) as list
	repeat with theItem in theList
		set theCreationDate to creation date of theItem
		set theMonth to month of theCreationDate as number
		set theDay to day of theCreationDate as number
		if theMonth contains currentMonth and theDay contains currentDay then
			
			set theRecNames to name of theItem & (reference URL of theItem)
			
			
			set the end of theNewList to name of theItem
		end if
	end repeat
	
	set theLength to get length of theNewList
	if theLength is greater than 1 then
		
		set theSelected to (choose from list theNewList with prompt "Which Records" with multiple selections allowed)
		
		set theSelectedRecord to theSelected as text
--#		set theDB to current database
		set existingNotes to every content in theDB whose name is theSelectedRecord
		set theNewRecord to item 1 of existingNotes
		open window for record theNewRecord
		activate
		
	else
		set theOneRecord to theNewList as text
--#		set theDB to current database
		set existingNotes to every content in theDB whose name is theOneRecord
		set theNewRecord to item 1 of existingNotes
		open window for record theNewRecord
		activate
		
	end if
end tell
1 Like

You need to build a list for your prompt
Initially, set thePromptList to {}
For each date match, set the end of thePromptList to name of theItem

To display the list,
set theSelected to (choose from list thePromptList with prompt “Which Records” with multiple selections allowed)

1 Like

I tried your method, but instead of three dialogs, I get instead prompt window three times. I want to to get all records in one prompt, not three.

If I put the choose from prompt line after repeat loop, I get only one record (last one) instead of all matches which is three.

It seems that new list replaces own value and not appending it.

property GroupUUID : "C52CCDB8-29C5-43AA-9926-B35D3958EFE0"
set currentDay to day of (current date) as number
set currentMonth to month of (current date) as number

tell application id "DNtp"
	try
		set theGroup to (get record with uuid GroupUUID)
	end try
	
	set theList to (get every child of theGroup) --# as list
	repeat with theItem in theList
		set theCreationDate to creation date of theItem
		set theMonth to month of theCreationDate as number
		set theDay to day of theCreationDate as number
		if theMonth contains currentMonth and theDay contains currentDay then

			set theRecNames to name of theItem & (reference URL of theItem)

			set theList to {}
			set the end of theList to name of theItem
		end if
	end repeat
	
	
	set theSelected to (choose from list theList with prompt "Which Records" with multiple selections allowed)
	
end tell

:person_facepalming:.

Thank you so much! Now I get the right result.

Could you post your updated, corrected AppleScript? Thanks!

property GroupUUID : "" -- group UUID here
set currentDay to day of (current date) as number
set currentMonth to month of (current date) as number

tell application id "DNtp"
	if (exists database "All") then set theDB to database "All"
	try
		set theGroup to (get record with uuid GroupUUID)
	end try
	set theNewList to {}
	
	set theList to (get every child of theGroup) as list
	repeat with theItem in theList
		set theCreationDate to creation date of theItem
		set theMonth to month of theCreationDate as number
		set theDay to day of theCreationDate as number
		if theMonth contains currentMonth and theDay contains currentDay then
			
			set theRecNames to name of theItem & (reference URL of theItem)
			
			
			set the end of theNewList to name of theItem
		end if
	end repeat
	
	set theLength to get length of theNewList
	if theLength is greater than 1 then
		
		set theSelected to (choose from list theNewList with prompt "Which Records" with multiple selections allowed)
		
		set theSelectedRecord to theSelected as text
--#		set theDB to current database
		set existingNotes to every content in theDB whose name is theSelectedRecord
		set theNewRecord to item 1 of existingNotes
		open window for record theNewRecord
		activate
		
	else
		set theOneRecord to theNewList as text
--#		set theDB to current database
		set existingNotes to every content in theDB whose name is theOneRecord
		set theNewRecord to item 1 of existingNotes
		open window for record theNewRecord
		activate
		
	end if
end tell