How to get at records in annotation group in a script?

EDITThe problem arose from having several DT windows open at the same time. Since the thread might still be interesting for others, I’ll leave it as it is.

I have a single PDF record with an annotation (not a PDF annotation, though), namely a link to another DT record (an e-mail). So, the annotation inspector shows the title of this record as a link, and when I click on it, the e-mail is opened. Also, the database’s annotations group contains an RTFD record with this single URL as a link. So far, so good.

Now, with this PDF record (i.e. the annotated one) selected, I want to get all the records in the corresponding annotation group:

(() => {
	const app = Application("DEVONthink 3");
	const r = app.selectedRecords()[0]; /* get the currently selected record */
	const annotation = r.annotation(); /* returns undefined because its a PDF? */
	const annGroup= r.database.annotationsGroup(); /* get the annotation group for the record's database */
	console.log(annGroup.children().length); /* returns 0 - WTF? There is one record in this group and I can _see_ it */
})()

So, annotation() of a non-text record does not work (as per the documentation). But shouldn’t the database’s annotationGroup behave like any other group and tell me about its children? I checked that the type of annotationGroup is indeed “group” and that the group has a UUID.

And group.children().length on a “normal” group works just fine, BTW.

I’m probably doing something stupid here. But what?

Not sure I understand what you’re trying to do. Do you want to get all children of the annotation group?

-- Get all annotation records of current database

tell application id "DNtp"
	try
		set theAnnotationsGroup to annotations group of current database
		set theAnnotationsGroup_children to children of theAnnotationsGroup
		
	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

Is there actually a shared Annotations group in your database? See Preferences > General > General > Annotations

I must admit that I find the wording in the German UI a bit confusing – what would be the difference between “in gemeinsamer Gruppe” and “in derselben Gruppe”? Both expressions seem to imply that there’s exactly one group, one time it’s a shared one and the other time it’s “the same”. The linguistic problem here seems to be (I may be wrong) that “group” refers to the annotations group itself (in the first alternative) and to the group of the record (in the second alternative).

After having read the documentation, it’s a bit (but only a bit) clearer: “shared group” means “one group at the root of the database” whereas “the same group” means “an annotation group that’s located in the same group as the record” (probably).

Anyway, yes, my annotations group is shared for the database.

@pete31: That script actually does what I want (also the JavaScript version). Now, I thought that I could find out the annotations group of a database starting from a record. Something like that:
set annotationGroup to theRecord's database's annotationGroup
(I apologize for my broken AppleScript lingo) or (in JavaScript)
annotationGroup = r.database.annotationsGroup();

This, however, gets weirdly wrong. I have a record selected in the currentDatabase (intuitively, I’d say that it is not possible to select a record in any other database but the current one, but maybe I’m wrong?). However, when I run my script (see above, it gets the first element of the selectedRecords list), this record has a different name and belongs to a different database.


This little script

let r = app.selectedRecords()[0]; /* get the currently selected record */
let db = r.database();
console.log(`records DB ${db.name()} record ${r.name()}`);

gives me this output:

records DB Bru6 record Ausgaben
current DB ck Privat

which is (at least to me) completely bogus: “Ausgaben” is a group in database “Bru6”, and it’s very much not currently selected. And yes, I’ve already restarted DT, which didn’t change anything.

@cgrunenberg: Is it possible to modify the selectedRecords (involuntarily) with scripting? I am currently working on the Alfred workflow for DT. But I’m still puzzled how the selectedRecords would survive a restart of DT.

Any suggestions not being excessively long?

selected records can’t be changed but you could remember the UUIDs. After restarting look up the records and set the selection of the main window.

Just to clear that up: I had actually three DT windows open (probably from fooling around with the Alfred workflow). The selectedRecords I received in my script were from the bottom-most window, most likely the first one I opened. An interesting border case for scripting, I think. One would have to figure out the active window before getting selectedRecords, it seems. Another thing learned.

  • In einer Gruppe für die Datenbank/In a group for the database
  • In der Gruppe des Datensatzes/In the group of the record
    Not sure about the singular/plural in the second version.

The next release will revise the German localization.

1 Like