New Group creation inside a Group

If I select a group in the Groups Pane (left one in the 3 panes view) and create a new group, the group is created inside the selected one and that’s fine (much better than the Finder’s behaviour).

On the contrary, if the selected group is a leaf (lowest hierarchical level) the new group is created at the same level as the selected one and not inside it.

I think it would be better (more consistent and more efficient) having new groups always created inside the selected one.

(*
	see: http://forum.devontechnologies.com/posting.php?mode=reply&f=4&t=16841
	makes a child group of the currently selected group
	(a group must be selected, else the script fails)
	
	20130704 / 20141114 modified to create a new group with the proposed name 
	as a subgroup of the current parent
	set to the first item in a selection (name can be overwritten),
	then create a group with that name (or other specfied in the dialog),
	then move the selection to that new group
*)

property nameSep : ": "

tell application id "DNtp"
	set theSelection to (the first item of (the selection as list))
	set theMoveSet to the selection
	set currentGroup to current group
	if the kind of theSelection is "group" then
		set {nameOption, groupName} to {button returned, text returned} of (display dialog "Make group: enter group name" default answer "<name>" buttons {"Append to parent name", "Do not append to parent name", "Cancel"} default button 2 with title "Enter group name")
		if nameOption is "Append to parent name" then
			set groupName to (the name of currentGroup) & nameSep & groupName
		end if
		create record with {type:group, name:groupName} in currentGroup
	else
		set groupName to the name of (the first item of (the selection as list))
		set {nameOption, groupName} to {button returned, text returned} of (display dialog "Move seleciton into New Group: enter group name" default answer groupName buttons {"Append to parent name", "Do not append to parent name", "Cancel"} default button 2 with title "Enter group name")
		if nameOption is "Append to parent name" then
			set groupName to (the name of currentGroup) & nameSep & groupName
		end if
		set theNewGroup to create record with {type:group, name:groupName} in currentGroup
		repeat with thisItem in theMoveSet
			set theResult to move record thisItem to theNewGroup
		end repeat
	end if
	
end tell

Me too, thanks for the script. (In case I’ll be reborn, I plan to learn Apple Script.)

Thanks for the script, korm.
I tweaked it to expand the selected group and show the new group.


(*
   see: https://discourse.devontechnologies.com/t/new-group-creation-inside-a-group/15444/1
   makes a child group of the currently selected group
   (a group must be selected, else the script fails)
*)

tell application id "com.devon-technologies.thinkpro2"
	set theSelection to (the first item of (the selection as list))
	if the kind of theSelection is "group" then
		set thisGroup to current group
		set groupName to text returned of (display dialog "Group name:" default answer "<<enter name>>")
		create record with {type:group, name:groupName} in thisGroup
		tell application "System Events" to key code 124
	else
		display alert "No group selected"
	end if
	
end tell