Groups Listing

This modified script copies the structure for the group you’ve selected in the sidebar or for the whole database.

Remove the tabs afterwards (in e.g. BBEdit) to get a plain names listing.

-- Copy group names (structure)

-- Create Listing.
-- Created by Christian Grunenberg on Sun Jul 24 2005.
-- Copyright (c) 2005-2009. All rights reserved.
-- Modified 20130602 to capture only groups
--Modified by @clane47 11_01_2013 to capture only groups to a rich text file
-- Modified by @korm 20131102 to put the listing on the clipboard for pasting elsewhere
-- Additional modification 20131102 to optionally set base group for the listing to the selected group

tell application id "DNtp"
	try
		if not (exists current database) then error "No database is open."
		set theDatabase to the current database
		
		show progress indicator "Creating Listing..."
		
		set listScope to button returned of (display dialog "Scope of Group Listing" buttons {"Whole Database", "Selected Group and SubGroups", "Cancel"} default button 1 cancel button 3 with title "Put a Group Listing on the Clipboard")
		
		if listScope is "Whole Database" then
			set the clipboard to my createListing(children of root of theDatabase, "")
		else if listScope is "Selected Group and SubGroups" then
			set rootGroup to the current group
			set the clipboard to my createListing(children of rootGroup, "")
		else
			return
		end if
		
		hide progress indicator
		
		display notification "Listing in clipboard!" with title "Create Listing"
		
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

on createListing(theseRecords, theTabs)
	local this_record, this_type, this_listing, this_name
	tell application id "DNtp"
		set this_listing to ""
		repeat with this_record in theseRecords
			set this_name to (name of this_record as string)
			set this_type to (type of this_record) as string
			if this_type is "group" or this_type is "«constant DtypDTgr»" then
				set this_listing to this_listing & theTabs & this_name & return
				step progress indicator this_name
				set this_listing to this_listing & my createListing(children of this_record, theTabs & (ASCII character 9))
			end if
		end repeat
	end tell
	return this_listing
end createListing

2 Likes

I am running the script which takes time (no problem). Not finished yet which is why I did not reply immediately. I will obviously give you a follow up. I greatly appreciate your help

1 Like

please let me know if I understand.

  • results copied to clipboard which is great
  • starts with list of tags which allows me to extract list of tags as separate file which is great
  • after that, groups are preceded by a tab and documents by 2 tabs which I can extract with a regex, also great.
    thank you

I don’t understand what you’re trying to understand here :slight_smile:

If you have

in your clipboard after the script ran, then you obviously did not choose the option which you wrote you want to use …

I wrote

So if you want to get the structure for the selected group you have to choose “Selected Group and Children” in the dialog …

@rufus123 please check if the script you used has this line in the handler

if this_type is "group" or this_type is "«constant DtypDTgr»" then

yes, the line of code is in the script.
I am running the script again with only Selected Group and Children .
Although I am definitely an idiot (my spouse says so all the time), I think that there is a misunderstanding.
I thought that Selected Group and Children meant exactly that: “selected” groups and children as in multiple selection. I will replace in the script with Groups and SubGroups only.
Does that make sense ?

Yeah, that’s because the original script created a listing of groups and documents and the dialog was not updated after it was modified.

Happened to me too and I didn’t change the dialog afterwards … changing it is a good idea :slight_smile:

hello @pete31 ,
I ran the script multiple times. I checked every step; I recopied the one you posted above (and which I recopy below), re-ran the script with script debugger, every time choose “Selected Group and Children” in the dialog and this is always the result:

  • results copied to clipboard
  • starts with list of tags each preceded by a tab
  • after that, groups are preceded by a tab and after each group a list of documents preceded by 2 tabs

thank you

tell application id “DNtp”

try

if not ( exists current database) then error “No database is open.”

set theDatabase to the current database

show progress indicator “Creating Listing…”

set listScope to button returned of ( display dialog “Scope of Group Listing” buttons {“Whole Database”, “Selected Group and Children”, “Cancel”} default button 1 cancel button 3 with title “Put a Group Listing on the Clipboard”)

if listScope is “Whole Database” then

set the clipboard to my createListing( children of root of theDatabase, “”)

else if listScope is “Selected Group and Children” then

set rootGroup to the current group

set the clipboard to my createListing( children of rootGroup, “”)

else

return

end if

hide progress indicator

display notification “Listing in clipboard!” with title “Create Listing”

on error error_message number error_number

hide progress indicator

if the error_number is not -128 then display alert “DEVONthink Pro” message error_message as warning

end try

end tell

on createListing(theseRecords, theTabs)

local this_record, this_type, this_listing, this_name

tell application id “DNtp”

set this_listing to “”

repeat with this_record in theseRecords

set this_name to (name of this_record as string )

set this_type to (type of this_record) as string

if this_type is “group” or this_type is “«constant DtypDTgr»” then

set this_listing to this_listing & theTabs & this_name & return

step progress indicator this_name

set this_listing to this_listing & my createListing( children of this_record, theTabs & ( ASCII character 9))

end if

end repeat

end tell

return this_listing

end createListing

This is my clipboard after running the script with a group selected in the sidebar and choosing “Selected Group and SubGroups” (I’ve changed the name of the button)

TheBrain Exporte
	TheBrain Export als JSON - konvertiert in CSV
	TheBrain Export als JSON
		e3e751bc-657a-48cb-a138-37e34666a692
		eb74794f-f22f-4c23-86cf-1dd8e6a41b02
		3258ab4a-7b21-4113-af7b-d092223d98f3
		a7777fc7-260d-4be1-bec2-09518775656a
		327b5e03-d2a3-4740-a18b-7167acb621f1
		1c5fd679-795a-4e9e-9463-227f0a80b49d
		1bba5818-a961-422a-bce4-3d01c2169a1a
		fcb92d1e-38fb-4625-8454-b9329aa5648c
Script Stuff

(Strings like e3e751bc-657a-48cb-a138-37e34666a692 are names of groups, not documents.)

Your result is only possible after choosing “Whole database”.

You have to choose “Selected Group and Children” (or “Selected Group and SubGroups” if you use the updated script).

I’m afraid there’s little I can do, sorry.

I know what happened.

I wrote a version without the dialog for you and tested it before posting but I didn’t make sure to select a group in the sidebar. Instead the database was selected and the resulting clipboard was exactly what you described - without choosing “Whole database”.

Again, there’s little I can do …

I won’t post the modified version as even this would not make sure that you select a group and not the database.

So please make sure to select a group, not a database, and everything should work as expected …

1 Like

I will test with selecting a group and let you know.
thank you very much for all your efforts

This is the result of my testing.
In each case I clicked on Selected Groups and SubGroups button

If I select the database, a new clipboard clip appears and contains

  • starts with list of tags which allows me to extract list of tags as separate file which is great
  • after that, list of groups and subgroups preceded by a tab and
  • for each group/subgroup is followed by a list of documents it contains preceded by 2 tabs

If I select either the main parent group, or select all the subgroups, the result it the same: a new clipboard clip appears and contains

  • does NOT start with a list of tags
  • list of groups and subgroups NOT preceded by a tab (starts at the margin)
  • for each group/subgroup is followed by a list of documents it contains preceded by one tab
    The is fine because I can easily extract the groups/subgroups with a regex

So all is great. You are fantastic and thanks again very much

1 Like

Here’s a revised, much faster version of the script which can also be canceled now:

-- Create Group Listing
-- Created by Christian Grunenberg on Sun Jul 24 2005.
-- Copyright (c) 2005-2009. All rights reserved.
-- Modified 20130602 to capture only groups
-- Modified by @clane47 11_01_2013 to capture only groups to a rich text file
-- Modified by @korm 20131102 to put the listing on the clipboard for pasting elsewhere
-- Additional modification 20131102 to optionally set base group for the listing to the selected group
-- Improved error handling and much higher performance by Christian Grunenberg on Wed Oct 28 2020

tell application id "DNtp"
	try
		if not (exists current database) then error "No database is open."
		
		set listScope to button returned of (display dialog "Scope of Group Listing" buttons {"Whole Database", "Selected Group and SubGroups", "Cancel"} default button 1 cancel button 3 with title "Put a Group Listing on the Clipboard")
		
		show progress indicator "Creating Listing..." with cancel button
		
		if listScope is "Whole Database" then
			set the clipboard to my createListing(children of root of current database whose type is group, "")
		else if listScope is "Selected Group and SubGroups" then
			set the clipboard to my createListing(children of current group whose type is group, "")
		else
			error -128 -- Cancelled
		end if
		
		hide progress indicator
		
		display notification "Listing in clipboard!" with title "Create Listing"
	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

on createListing(theseGroups, theTabs)
	local this_group, this_listing, this_name, nextTabs
	tell application id "DNtp"
		set this_listing to ""
		set nextTabs to theTabs & (ASCII character 9)
		repeat with this_group in theseGroups
			set this_name to (name of this_group as string)
			set this_listing to this_listing & theTabs & this_name & return
			step progress indicator this_name
			set this_listing to this_listing & my createListing(children of this_group whose type is group, nextTabs)
			if cancelled progress then error -128
		end repeat
	end tell
	return this_listing
end createListing
3 Likes

thank you Christian. It is great and faster as you say.
Do you think that you would have the patience to create a similar scripts just for tags ?
That way we can replace the initial script with 2 scripts: groups/docs and tags.
If not, no problem. I understand that you are very busy.
thank you again

This would be good exercise for the reader :smiling_imp:

2 Likes

I understand. thank you.

@pete31 @korm
I feel bad that my post could have triggered any kind of problem. It’s my fault for reigniting a thread so many years later which created confusion. You are obviously both brilliant.

Ugh, sorry @pete31 – I’m sorry to pain you – not intended. As for the rest, I’m blushing. I appreciate your thoughtfulness.

2 Likes