First off, I’m trying that with my very limited custom meta data, which is only one (“Betrag”, the amount in € for an invoice). Now, I select two records in DT and run a minimal script:
Stupidly simple (and as I supposed before):
app.selectedRecords.customMetadata()
returns a JavaScript Array
whose elements are simple objects of the form
{ key1: value1, key2: value2, ...}
What you apparently want is to return a string containing the values for one custom meta data key separated by newlines.
(() => {
const app = Application("DEVONthink 3");
return app.selectedRecords.customMetaData().map(cmd => cmd[key]).join('\n');
})()
Looks ok to me. As you can see, you have to prepend md
to the name of the custom meta data field and use only lower case letters. I seem to remember that that’s mentioned somewhere in the documentation. And put the key you’re using in quotes (single/double/backquotes, whatever you fancy).
TL;DR
I’ll try to explain what this horrible one-liner does.
app.selectedRecords
returns an object specifier to the currently selected records. Think of it as “something like an array of records, but not quite”. Not quite an array, because you can append customMetaData
to it to get another object specifier. This time, think of it as “something like an array of custom meta data, but not quite”. The parenthesis in
app.selectedRecords.customMetaData()
give you a real array of customMetadata
.
Now, looking that up in the scripting dictionary, you see
customMetaData (*Record*) : User-defined metadata of a record as a dictionary containing key-value pairs.
which is unfortunately quite confusing, since this “Record” is not at all the same thing as a DT record. It’s simply a Record
in AppleScript lingo, which translates into an object in JavaScript. The record’s keys are the object’s attributes/properties, and the values are the same for both languages.
So now we’re dealing with an array of objects, and we want the values for a particular attribute/property of these objects. That’s what the map
method above is doing: for every element of the array, it returns the value of it’s “key” property. Since map
builds a new array from these values, you can append join("\n")
to it which finally gives you what you want.
What you did before was something like
app.selectedRecords.customMetadata().join('\n')
effectively joining the string representation of the custom meta data objects with newlines. Since JavaScript can’t translate an arbitrary object into a string, you get “[object Object]\n[object Object]”. If you’d turned on the “Replies” tab, you’d probably have seen what customMetaData()
really is.