Script: Auto group by name pattern

Hi there,

this script groups selected records on a pattern you specified, in my case it was YYYY-MM-DD at the name beginning. It saves the user from selecting records that share e.g. the same name beginning manually, meaning that you don’t have to select records “one pattern by one”. Just select all records and run it.

It’s the normal DEVONthink 3 behaviour when grouping records, but without the manually selecting

-- Auto group by name pattern (in this case a YYYY-MM-DD date at the beginning)

tell application id "DNtp"
	try
		set windowClass to class of window 1
		if {viewer window, search window} contains windowClass then
			set currentRecord_s to selection of window 1
		else if windowClass = document window then
			set currentRecord_s to content record of window 1 as list
		end if
		
		repeat with thisRecord in currentRecord_s
			set theName to name of thisRecord
			set thePattern to characters 1 thru 10 in theName as string -- date as YYYY-MM-DD
			set theGroup to create location (location of current group) & "/" & (name of current group) & "/" & thePattern & "/" in current database
			move record thisRecord to theGroup
		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

Which version of DEVONthink do you use? Version 3 recognizes common prefixes/suffixes/words and uses them for the group’s default name.

Hi, I’m using 3.0.1 but that’s all fine. Only used this script to “auto group” by a given pattern (to bypass the manual selection of records that I want DEVONthink 3 to built its pattern from). See the two captures

1

Using DEVONthink 3’s built-in feature results in

2

But what I needed was only 2019-11-02 so that’s what this script is for.
DEVONthink 3’s own feature is great, I like it a lot, this was just a “special case” I had to do once (but on many records) :slight_smile:

In case you’re trying to run the script I posted in this thread you’ll find that it doesn’t work in DEVONthink 3.6. That’s due to DEVONthink’s new handling of “invalide arguments”.

After the release of DEVONthink 3 I decided to continue to use “search window” in scripts so that DEVONthink 2 users could use them in, well, search windows. With version 3.6 that’s not possible anymore.

If you want to use the script you’ll have to replace this voluminous block …

set windowClass to class of window 1
if {viewer window, search window} contains windowClass then
	set currentRecord_s to selection of window 1
else if windowClass = document window then
	set currentRecord_s to content record of window 1 as list
end if

… with this neat line …

set currentRecord_s to selected records

… which does what the six lines have done. Wow, that’s great! :smiley: