Script: Create custom date register

This script creates a custom date register.

It can be used to create a wide range of date formats from a provided Start and End Date.


The default is a YYYY/YYYY-MM/YYYY-MM-DD register.


If you want a custom date format


  • Select the destination group in the navigation sidebar
  • Run the script



'Random string' yyyy/'Test' yyyy-MM/yyyy-MM-dd
Random string 2021/Test 2021-03/2021-03-01

yyyy/yyyy-MM/dd. MMMM yyyy
2021/2021-03/01. März 2021

yyyy/QQQ yyyy/yyyy-MM/yyyy-MM-dd
2021/Q1 2021/2021-03/2021-03-01

2021/KW 09 2021/2021-03-01, Mo.

Although it might seem redundant it’s a good idea to make sure that group names on each level are meaningful. DEVONthink is superb in finding stuff but that experience is easily destroyed if one routinely encounters groups with identical names in search results.

It’s of course not necessary to use the year at the start:

e EEEE/yyyy-MM/yyyy-MM-dd
1 Montag/2021-03/2021-03-01

-- Create custom date register

-- Date Format Patterns:

-- Examples:
-- "yyyy/yyyy-MM/yyyy-MM-dd" --> 2021/2021-03/2021-03-01
-- "'Random string' yyyy/'Test' yyyy-MM/yyyy-MM-dd" --> Random string 2021/Test 2021-03/2021-03-01
-- "yyyy/yyyy-MM/dd. MMMM yyyy" --> 2021/2021-03/01. März 2021
-- "yyyy/QQQ yyyy/yyyy-MM/yyyy-MM-dd" --> 2021/Q1 2021/2021-03/2021-03-01
-- "YYYY/'KW' ww YYYY/YYYY-MM-dd, E" --> 2021/KW 09 2021/2021-03-01, Mo.
-- "e EEEE/yyyy-MM/yyyy-MM-dd" --> 1 Montag/2021-03/2021-03-01

-- Note: If you use a slash "/" make sure to escape it "\\/"

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property createGroups : true -- set to false while testing 
property theDateFormat : "yyyy/yyyy-MM/yyyy-MM-dd" --> 2021/2021-03/2021-03-01

set theCalendar to current application's NSCalendar's currentCalendar()
set theCalendarOptions to current application's NSCalendarMatchNextTime
set theDefaultStartDate to current application's NSDate's |date|()
set theDateComponents to current application's NSDateComponents's alloc()'s init()
set theDateComponents's |day| to 1
set theDateComponents's |month| to 1
set theDefaultEndDate to theCalendar's nextDateAfterDate:theDefaultStartDate matchingComponents:theDateComponents options:theCalendarOptions
set theDateFormatter to current application's NSDateFormatter's alloc()'s init()
set theDateFormatter's timeZone to (current application's NSTimeZone's timeZoneWithName:"GMT")
set theDateFormatter's dateFormat to "yyyy-MM-dd"

tell application id "DNtp"
		if not (exists viewer window 1) then error "Please open a window"
		set theStartDate to missing value
		repeat while theStartDate = missing value
			set theStartDate to theDateFormatter's dateFromString:(display name editor "Create Register" info "Start Date:" default answer ¬
				(theDateFormatter's stringFromDate:theDefaultStartDate) as string)
		end repeat
		set theEndDate to missing value
		repeat while theEndDate = missing value
			set theEndDate to theDateFormatter's dateFromString:(display name editor "Create Register" info "End Date:" default answer ¬
				(theDateFormatter's stringFromDate:theDefaultEndDate) as string)
		end repeat
	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

set theNSOrderedAscending to current application's NSOrderedAscending
if (theStartDate's compare:theEndDate) ≠ theNSOrderedAscending then set {theStartDate, theEndDate} to {theEndDate, theStartDate}

set theDateComponents's |month| to 0
set theDateFormatter's dateFormat to theDateFormat
set thisDate to theStartDate
set theDateStringArray to current application's NSMutableArray's arrayWithArray:{theDateFormatter's stringFromDate:theStartDate}

	set thisDate to (theCalendar's dateByAddingComponents:theDateComponents toDate:thisDate options:theCalendarOptions)
	if (theEndDate's compare:thisDate) ≠ theNSOrderedAscending then
		(theDateStringArray's addObject:(theDateFormatter's stringFromDate:thisDate))
		exit repeat
	end if
end repeat

if createGroups = true then
	script s
		property theLocations : missing value
	end script
	set s's theLocations to theDateStringArray as list
	tell application id "DNtp"
			set theDatabase to current database
			set theGroup to current group
			if theGroup ≠ root of theDatabase then
				set theGroupName to name of theGroup
				if theGroupName contains "/" then set theGroupName to my escapeSlash(theGroupName)
				set theGroupLocation to location of theGroup & theGroupName & "/"
				set theGroupLocation to "/"
			end if
			set theContainerName to "Register"
			if exists record at theGroupLocation & theContainerName in theDatabase then
				set theDateFormatter's timeZone to current application's NSTimeZone's localTimeZone()
				set theDateFormatter's dateFormat to "yyyy-MM-dd"
				set theContainerName to theContainerName & space & ((theDateFormatter's stringFromDate:(current application's NSDate's |date|())) as string)
			end if
			show progress indicator "Creating Register... " steps (count (s's theLocations)) as string with cancel button
			repeat with thisLocation in s's theLocations
				step progress indicator thisLocation as string
				create location theGroupLocation & theContainerName & "/" & thisLocation as string in theDatabase
			end repeat
			hide progress indicator
		on error error_message number error_number
			hide progress indicator
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
	end tell
	set theDateStringList to theDateStringArray as list
	return item 1 of theDateStringList
end if

on escapeSlash(theText)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to "\\/"
	set theText_escaped to theTextItems as string
	set AppleScript's text item delimiters to d
	return theText_escaped
end escapeSlash

1 Like

Thank you for the script

I’m interested in hearing about the uses for such a register structure

See e.g. Historical Jounals.

I use YYYY-MM-DD structures wherever I need chronological order and create them via selected records’ creation dates (which were set to e.g. the publication dates) - but obviously one first needs records to create a structure this way.

With the script it’s possible to create an empty structure and fill it afterwards. It’s also nice to provide custom strings to be able to differentiate groups, especially in search results.

To get chronological order I can sort on the date column
I actually prefix note titles with the subject date so sorting by name works

THanks you so much. Yes I did create the calanders but this is an invaluable script. Thanks

1 Like