How to create a new group from selected text

I have thousands of articles to classify to groups and the CLASSIFY function is really helpful, but it’s hard when need a new group because no existing group is right. You have to leave what you’re doing, create a new group and then come back and drag the item into it. Then HOPEFULLY it will show up on the CLASSIFY list next time you find a similar article. It would be great to SELECT a word or phrase and be able to CREATE NEW GROUP from that – even if it just goes into the GROUP ROOT DIRECTORY.

Am I missing something obvious, because I swear I have searched for an answer.

This script creates a group from selected text and moves the record.

If you set property useGroupSelector to true you’ll be asked in which group the new group should be created.

If you set property openWindow to true you’ll get, well, a new window.

-- Create group from selected text and move record

property useGroupSelector : true -- set to false if you want to create the group in the database's root
property openWindow : false -- set to true to open a window for the group

tell application id "DNtp"
	try
		set theRecord to content record of think window 1
		if theRecord = missing value then error "Please open a record"
		
		try
			set selectedText to selected text of think window 1 & "" as string
		on error
			error "Please select some text"
		end try
		
		if selectedText contains "/" then set selectedText to my escapeSlash(selectedText)
		if useGroupSelector = true then
			set theGroup to display group selector "Create group in:" for database of theRecord
			set theGroup_Name to name of theGroup
			if theGroup_Name contains "/" then set theGroup_Name to my escapeSlash(theGroup_Name)
			set theLocation to (location of theGroup) & theGroup_Name & "/"
		else
			set theLocation to "/"
		end if
		
		set newGroup to create location theLocation & selectedText in database of theRecord
		move record theRecord to newGroup
		
		if openWindow = true then
			open window for record newGroup
			activate
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

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

2 Likes

THANK YOU THANK YOU THANK YOU @pete31

Is there some collection of these scripts somewhere?

1 Like

No. You could search, e.g. tell application after:2019-09-12 @pete31