Creating year and month groups in DEVONthink Pro Office using AppleScript

In DEVONthink Pro Office I often organize certain content by year and month. To do that, I created a simple AppleScript to build year and month groups in the following format:

To use the script, you select the parent group in which the year will reside.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--
-- Created by: Alan Duncan
-- Created on: 2018-12-14
--
-- Copyright (c) 2018 Ojisan Seiuchi
-- All Rights Reserved
--

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theMonths to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

tell application id "DNtp"
try
set theSelection to selection
if theSelection is {} then error "Please select some contents."
set theYear to display name editor "Year:"
set theYearGroup to create record with {name:theYear, type:group} in current group
repeat with i from 1 to 12
set theMonth to item i of theMonths

set theMonth to (my add_leading_zeros(i, 1)) & " - " & theMonth
set theMonthGroup to create record with {name:theMonth, type:group} in theYearGroup
end repeat
on error
display dialog "Error"
end try
end tell

on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros