Color tags with JavaScript

As I haven’t found a way to let tags inherit the color of its tag group (nested tag) I’m writing a script for it where I get the color of the tags parent and then assigning it to the tag itself. The problem is that when I assign the parents color something is happening to it. Here’s my code and then the output:

(() => {
	const app = Application("DEVONthink 3");
    app.includeStandardAdditions = true;
    const selection = app.selectedRecords();
    selection.forEach(record => {

        const parent = record.parents.uuid()
        let parentRecord = app.getRecordWithUuid(String(parent))
        console.log('(1) Parent color:', parentRecord.color())
        record.color = parentRecord.color() // Give the color of the parent to the record.
        console.log('(2) Records new color:', record.color())
        record.color = [324232, 2342342, 4212352] // Set the color of the tag to some random and very high values (for demonstration).
        console.log('(3) Records random color:', record.color())
    })
})()
(1) Parent color: 0.501960813999176,0.001541161211207509,0.998504638671875
(2) Records new color: 0,0,0
(3) Records random color: 0.9474021792411804,0.7413138151168823,0.27537956833839417

What kind of color conversion is happening here? In what format are DP handeling colors? And how do I fix it?

#javascript

The valid range for the RGB values is 0-65535. All the other “magic” is the joy of JXA.

1 Like

Ah, thanks! Knowing this and adding a function for multiply by 65535 I get the correct color.
If someone else wants to color their tags by the “parent tag” (or updated on Github):

function rgb(colorArray){
    newColorArray = []
    colorArray.forEach(i => {
        newColorArray.push('65535' * i)
    });
    return newColorArray
};
(() => {
	const app = Application("DEVONthink 3");
    app.includeStandardAdditions = true;
    const selection = app.selectedRecords();
    selection.forEach(record => {
        const parent = record.parents.uuid()
        let parentRecord = app.getRecordWithUuid(String(parent))
        record.color = rgb(parentRecord.color())
    })
})()
2 Likes