For completeness sake (and because I posted stupid things before), a corrected version of the script
(() => {
const app = Application("DEVONthink 3");
const docWindow = app.viewerWindows()[0];
const rowCount = docWindow.numberOfRows();
const columnCount = docWindow.numberOfColumns();
for (let i = 1; i <= rowCount; i++) {
const name = docWindow.getCellAt({column: columnCount-1, row:i});
const cite = docWindow.getCellAt({column: columnCount, row:i});
const targetRec = app.search(`filename: "${name}"`);
if (targetRec.length > 0) {
app.addCustomMetaData(cite, {for: 'cite', to: targetRec[0]});
}
}
})()
This version starts out from the first row of the CSV and uses the filename
to search for the … well, filename.