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

Hi @RDK this script is exactly what I have been looking for! Thanks for creating it.

I am in the process of moving away from DayOne to DEVONthink completely. I also value the “On This Day” function in DayOne and would like to replicate it.

For some reason I am receiving an error when I try and run the script. Do I need to modify any parts of the script or be in a certain place before I run it?

I don’t get any dialog boxes or prompts.

The error is:

You haven’t set a value for GroupUUID
So no record, and no children

1 Like

Thanks @DTLow, have done that using the format “494CE67D-0993-4A4F-B1D7-775BF89DF5AB” (derived from group item link)

I now get a different error “The variable theDB is not defined”.

Do I need to change the database name “All” to my database name? (I tried that and got a different error).

Unless you have a database named All, yes you need to use the appropriate name.

Thanks @BLUEFROG

Ok, so I have changed to one of my DB names “LifeOS”.

Now I get another error.

Any thoughts?

I would gladly help you to find out why it is not working. I’m an blind user and therefore your screenshots are useless for me heh. Could you provide the error you get in text?:blush:

1 Like

Thanks @RDK, appreciate your assistance.

The latest error is:

Script Error

Can’t get item 1 of {}.

I have set the group ID to the group containing the items I wish to search, and I have set the database name to the database containing that group.

Turn on the protocol in script editor (the three horizontal lines at the bottom of the window) to see exactly where the error occurs.

I have not used this script in a while. However, I copied the script, changed UUID of the group and renamed to the Database where the group is located. It works here.

However, I get the same error as you when I press “Cancel” on the prompt with several records.

I tried to use UUID of a group where I know there is no results to get. Then I get the same error as you. The script uses creation date of a record to match the correcrt records. Can you check in the group you added that there are files created a year ago at 31. dec etc?

For test purposes, you can duplicate several records and modify creation date to 31. dec in different years.

I don’t want to derail this thread—or take it off at a tangent—but I have for a long time journalled in Day One and imported the Day One entries regularly into DEVONthink. I have a working "on this day” script that I use without problem and can contribute it if it’s any help.

Use of my script requires entries to be named, in DEVONthink, by date…and knowing what that date format is.

I’ll go back to lurking now and not intrude here again unless anyone wishes.

Stephen

Hi @Stephen_C I would love to see it if you are happy to share. Thanks!

I was also thinking about you earlier - was reading all your helpful posts about switching from DayOne to DEVONthink. I think I might have found an easier way (which is probably too late for you!)

I have been exporting my DayOne entries as JSON into MacJournal, which brings in most metadata. MacJournal uses RTF not markdown. I then export from MacJournal to DEVONthink. I can use RFT or MD. MD, of course, will lose embedded images.

As to the script, here you are (but if trying to use anything similar just be careful about date formats):

(* This script displays in the search results window every diary
entry for the date and month which is input in the dialog. It emulates
"On This Day" in Day One. *)

-- Set the database we want to use
property pDatabase : [path to your Diaries database]

tell application id "DNtp"
	try
		set theDatabase to open database pDatabase
		-- Set today's date as the initial default date for the input dialog
		set theDate to short date string of (current date)
		set dAnswer to theDate
		-- Get the date of the wanted diary entry
		set userEntry to display dialog ¬
			"Enter date for all entries on this day as DD/MM/YYYY" default answer dAnswer with title "Find all entries on this day of each year" with icon 1
		set dAnswer to text returned of userEntry
		-- Now reformat it to match the name of a diary entry record
		-- Here we use the getName handler to construct
		-- a name like "08-30"
		set theTarget to my formatDate(dAnswer)
		-- Search for the named record in the database
		set foundRecords to (search "name:" & theTarget & " kind:markdown") in theDatabase
		if foundRecords = {} then
			error theTarget & " was not found in " & pDatabase
		end if
		set theResult to foundRecords
		set search results of viewer window 1 to theResult
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

on formatDate(LongDateStr)
	set {dd, mm, yyyy} to words of (short date string of (date LongDateStr))
	set formattedDate to (mm & "-" & dd)
	return formattedDate
end formatDate

As to exporting from Day One, I continue to write my diaries in Day One but export them as markdown every three days or so then use my various scripts in DEVONthink. With Hazel, DEVONthink and AppleScript everything is reasonably well automated now.

Stephen

1 Like