Cannot set foregroundColor or backgroundColor using PS DOM

Hello,

I’m trying to use app.foregroundColor and app.backgroundColor to set new foreground and background colors using events. Very oddly while I can read them just fine like Solid Color object in the console, I cannot set them at all although the documentation indicates they are R+W. I tried passing a SolidColor, a SolidColor through lab, rgb, and rgb.hexValue, none would work.

Is there something I’m doing incorrectly or is it a bug?

I don’t know if that’s what you’re looking for
I use this to set the foreground and background color.

async function test2() {

setForegroundColor(255, 255, 255);

function setForegroundColor(r,g,b) {
    var color = app.foregroundColor;
    color.rgb.red = r;
    color.rgb.green = g;
    color.rgb.blue = b;
    app.foregroundColor = color;
 }

setBackgroundColor(0, 0, 0);

function setBackgroundColor(r,g,b) {
    var color = app.backgroundColor;
    color.rgb.red = r;
    color.rgb.green = g;
    color.rgb.blue = b;
    app.backgroundColor = color;
 }


}

Yes, but for some reason it doesn’t work. I even just tried copy/pasting your code to see if I made any typo, but it doesn’t do anything and the console doesn’t return any error :confused:

that example code is not complete, be sure you are executing as modal and it should work.

If you’re changing the state of PS then you need to wrap your code in an executeAsModal call.

1 Like

sorry, I didn’t add the 2 lines above
you now have the complete code

  const app = require('photoshop').app,
  shell = require('uxp').shell

document.getElementById("btnPopulate").addEventListener("click", test2run);

async function test2() {



setForegroundColor(255, 255, 255);

function setForegroundColor(r,g,b) {
    var color = app.foregroundColor;
    color.rgb.red = r;
    color.rgb.green = g;
    color.rgb.blue = b;
    app.foregroundColor = color;
 }

setBackgroundColor(0, 0, 0);

function setBackgroundColor(r,g,b) {
    var color = app.backgroundColor;
    color.rgb.red = r;
    color.rgb.green = g;
    color.rgb.blue = b;
    app.backgroundColor = color;
 }


}


async function test2run() {
  await require('photoshop').core.executeAsModal(test2, {
    commandName: 'Action Commands',
  })
}