Skip to main content
Geppetto Luis
Legend
October 3, 2019
Answered

Store foreground color

  • October 3, 2019
  • 3 replies
  • 744 views

There is a way to store the foreground color
so when I open the ui window I set the stored color.

 

win = new Window("dialog", "test");
win.orientation = "column";
FC = win.add("group");

baton22 = FC.add("group");
Res = FC.add("button", undefined, "Memory foregroundColor");

Res.onClick = function () {
var flag = showColorPicker();
}

closeBtn = FC.add("button", undefined, "ok");
closeBtn.onClose = function () {
win.close();
};

win.show();
This topic has been closed for replies.
Correct answer Chuck Uebele

Try this:

#target photoshop
var colorFile = new File('~/desktop/colorFile.txt');
var cInfo

var dlg = new Window('dialog','Save Foreground Color');
var saveC = dlg.add('button',undefined,'Save Foreground Color');
var getC = dlg.add('button',undefined,'Retrieve Foreground Color');
colorFile.exists?getC.enabled = true:getC.enabled = false;
    
saveC.onClick = function(){
    var fC = new SolidColor();
    fC = foregroundColor;
    cInfo = fC.rgb.red +','+fC.rgb.green +','+fC.rgb.blue;
    writeFile (colorFile, cInfo)
    dlg.close();
    }

getC.onClick = function(){
    var cTxt = readFile (colorFile);
    var fC = new SolidColor();
    fC.rgb.red = cTxt.split(',')[0];
    fC.rgb.green = cTxt.split(',')[1];
    fC.rgb.blue = cTxt.split(',')[2];
    foregroundColor=fC;
    dlg.close();
    }

dlg.show();

//===============READ/WRITE functions========================================
//=========================================================================
 function readFile(file) {
	if (!file.exists) {
		alert( "Cannot find file: " + deodeURI(file.absoluteURI));
		}
	else{
		file.encoding = "UTF8";
		file.lineFeed = "unix";
		file.open("r", "TEXT", "????");
		var str = file.read();
		file.close();

		return str;
		};
};

function writeFile(file,str) {

		file.encoding = "UTF8";
		file.open("w", "TEXT", "????");
		//unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
		file.write("\uFEFF");
		file.lineFeed = "unix";
		file.write(str);
		file.close();
		
	};
 //=========================================================================

3 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
October 4, 2019

Try this:

#target photoshop
var colorFile = new File('~/desktop/colorFile.txt');
var cInfo

var dlg = new Window('dialog','Save Foreground Color');
var saveC = dlg.add('button',undefined,'Save Foreground Color');
var getC = dlg.add('button',undefined,'Retrieve Foreground Color');
colorFile.exists?getC.enabled = true:getC.enabled = false;
    
saveC.onClick = function(){
    var fC = new SolidColor();
    fC = foregroundColor;
    cInfo = fC.rgb.red +','+fC.rgb.green +','+fC.rgb.blue;
    writeFile (colorFile, cInfo)
    dlg.close();
    }

getC.onClick = function(){
    var cTxt = readFile (colorFile);
    var fC = new SolidColor();
    fC.rgb.red = cTxt.split(',')[0];
    fC.rgb.green = cTxt.split(',')[1];
    fC.rgb.blue = cTxt.split(',')[2];
    foregroundColor=fC;
    dlg.close();
    }

dlg.show();

//===============READ/WRITE functions========================================
//=========================================================================
 function readFile(file) {
	if (!file.exists) {
		alert( "Cannot find file: " + deodeURI(file.absoluteURI));
		}
	else{
		file.encoding = "UTF8";
		file.lineFeed = "unix";
		file.open("r", "TEXT", "????");
		var str = file.read();
		file.close();

		return str;
		};
};

function writeFile(file,str) {

		file.encoding = "UTF8";
		file.open("w", "TEXT", "????");
		//unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
		file.write("\uFEFF");
		file.lineFeed = "unix";
		file.write(str);
		file.close();
		
	};
 //=========================================================================
Geppetto Luis
Legend
October 5, 2019
Super perfect thanks how do you insert correct answer?
Chuck Uebele
Community Expert
Community Expert
October 5, 2019
There is either a "correct" link under the post, or you might have to click on the "more..." link to see it.
Geppetto Luis
Legend
October 4, 2019

Unfortunately it's not what I'm looking for
I would like that once the color is stored
when i open the window ui set me the foreground color
with the color saved previously

Chuck Uebele
Community Expert
Community Expert
October 3, 2019

This will save the current foregound color to the variable saveColor:

var saveColor = new SolidColor()
saveColor = foregroundColor