Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Communication issue between functions into JS and JSX files

Participant ,
Oct 26, 2019 Oct 26, 2019

I have made this function into a JS file...

 

 

    function getColors(isPick, isForecolor)
    {
        var chosenFunction = 'getColor(' + isPick + ', ' + isForecolor + ')';
        csInterface.evalScript(chosenFunction, function(result)
        {
            if(result !== 'undefined')
            {
                if (isForecolor == true){
                    foregroundHexColor = result;
                    //etc...
                }
                else
                {
                    backgroundHexColor = result;
                    //etc...
                };
            };
        });
    };

 

 

which get a hexadecimal color value from this function from a JSX file.

 

 

function getColor(isPick, isForecolor)
{
	var color_PickerCase;
	var decimal_Color;
	var hexadecimal_Color;

	if (isForecolor == true)
	{
		color_PickerCase = app.foregroundColor.rgb.hexValue;
	}
	else
	{
		color_PickerCase = app.backgroundColor.rgb.hexValue;
	};
	
	if (isPick == true)
	{
		if (app.showColorPicker(isForecolor)){
			decimal_Color = color_PickerCase;
			hexadecimal_Color = decimal_Color.toString(16);
		}
		else
		{
			return;
		};
	}
	else
	{
		decimal_Color = color_PickerCase;
		hexadecimal_Color = decimal_Color.toString(16);
	};

	return hexadecimal_Color;    
};

 

 

In some way it works, but for some reason I have to do the same thing two times so to get the value!!!
Any idea why is this happening?

Thank you for your time!!!

 

UPDATE: A correction, it works only at first click. Then needs to clicked two times so to get the value!!!

TOPICS
Actions and scripting , Windows
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Oct 27, 2019 Oct 27, 2019

Well solution found!!! 🙂

 

function getColor(isPick, isForecolor)
{
    var color_PickerCase;
    var decimal_Color;
    var hexadecimal_Color;

    if (isPick === true && app.showColorPicker(isForecolor) === false)
    {
            return;
    }

    if (isForecolor === true)
    {
        color_PickerCase = app.foregroundColor.rgb.hexValue;
    }
    else
    {
        color_PickerCase = app.backgroundColor.rgb.hexValue;
    }

    decimal_Color = color_PickerCase;
    hexadecimal_Color = deci
...
Translate
Adobe
Community Expert ,
Oct 26, 2019 Oct 26, 2019

How are you using this function. How are you associating it with a click. What values are being passed for isPick and isForecolor and how are you using any color that may be returned.   The variable isPick and isForecolor seem to be used only as true false conditionals.  If isForecolor is true color_PicketCase is set to the foreground color if false the background color.  If  isPicker is true nothing will most likely be returned.  If  isPicker is false either the foreground or background color will be returned depends on the if isForecolor value passed was 1.

 

You seem to be using the boolean showColorPicker();  in a strange way  I do not know JavaScript so I do not know what using the isForecolor boolean in the showColorPicker  booleans method statement would do.  It should not accept any parameters IMO it just returns a boolean about the color picker dialog being canceled or not.

if (app.showColorPicker(isForecolor))

 

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 26, 2019 Oct 26, 2019

Well, I think is better to show you my code from my JS file...

(function()
{
   'use strict';

    var csInterface = new CSInterface();

    var foregroundHexColor;
    var foregroundRGBColor;
    var backgroundHexColor;
    var backgroundRGBColor;
        
    var currentColorDiv = document.getElementById("current-color-div");
    var currentColorRedTextbox = document.getElementById("current-color-red-textbox");
    var currentColorGreenTextbox = document.getElementById("current-color-green-textbox");
    var currentColorBlueTextbox = document.getElementById("current-color-blue-textbox");
    var currentColorHexTextbox = document.getElementById("current-color-hex-textbox");

    var newColorDiv = document.getElementById("new-color-div");  
    var newColorRedTextbox = document.getElementById("new-color-red-textbox");
    var newColorGreenTextbox = document.getElementById("new-color-green-textbox");
    var newColorBlueTextbox = document.getElementById("new-color-blue-textbox");
    var newColorHexTextbox = document.getElementById("new-color-hex-textbox");
    
    init();
    
    function init()
    {
        themeManager.init();
        $(document).ready(function()
        {
            getColors(false, true); // Gets the foreground color when document is ready.
            getColors(false, false); // Gets the background color when document is ready.
        });
        $("#current-color-pick-button").click(function()
        {
            getColors(true, true); // Gets the foreground color when "Pick" button clicked.
        });
        $("#new-color-pick-button").click(function()
        {
            getColors(true, false); // Gets the background color when "Pick" button clicked.
        });
    };

    function hexToRGB(hex)
    {
	    var r = hex >> 16;
	    var g = hex >> 8 & 0xFF;
	    var b = hex & 0xFF;
        return [r,g,b];
    };
    function getColors(isPick, isForecolor)
    {
        var chosenFunction = 'getColor(' + isPick + ',' + isForecolor + ')';       
        csInterface.evalScript(chosenFunction, function(result)
        {
            if(result !== 'undefined')
            {
                if (isForecolor == true){
                    foregroundHexColor = result;
                    currentColorDiv.style.backgroundColor = "#" + foregroundHexColor;
                    currentColorHexTextbox.value = "#" + foregroundHexColor;
                    foregroundRGBColor = hexToRGB(parseInt(foregroundHexColor, 16));
                    currentColorRedTextbox.value = foregroundRGBColor[0]
                    currentColorGreenTextbox.value = foregroundRGBColor[1]
                    currentColorBlueTextbox.value = foregroundRGBColor[2]
                }
                else
                {
                    backgroundHexColor = result;
                    newColorDiv.style.backgroundColor = "#" + backgroundHexColor;
                    newColorHexTextbox.value = "#" + backgroundHexColor;
                    backgroundRGBColor = hexToRGB(parseInt(backgroundHexColor, 16));
                    newColorRedTextbox.value = backgroundRGBColor[0]
                    newColorGreenTextbox.value = backgroundRGBColor[1]
                    newColorBlueTextbox.value = backgroundRGBColor[2]
                };
            };
        });
    };
}());

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2019 Oct 26, 2019

Your more a Geek than I am I do not know Java and looking at your Java code I see

var csInterface = new CSInterface();

That is Greek to me. If you writing a Photoshop extension that well beyond my knowledge. I only hack a little at Photoshop scripting which has many limitations like you can not get at thing like cursor position  so the only way to get at color at locations is to use the color simpler tool to get at colors.  You need to know the locations  to set color sampler or you can manually use the tool to set up to 10 locations and then use Photoshop scripting to get the locations and get colors. I use the color sampler tools both way. I would think that Adobe would use   showColorPicker();  as a boolean in application extensions as well as in extendscript  to avoid confusion programmers.  But then we are dealing with Adobe development who knows what gone on between groups....  I still see you JSX code may return no value. else return;

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 27, 2019 Oct 27, 2019

Well solution found!!! 🙂

 

function getColor(isPick, isForecolor)
{
    var color_PickerCase;
    var decimal_Color;
    var hexadecimal_Color;

    if (isPick === true && app.showColorPicker(isForecolor) === false)
    {
            return;
    }

    if (isForecolor === true)
    {
        color_PickerCase = app.foregroundColor.rgb.hexValue;
    }
    else
    {
        color_PickerCase = app.backgroundColor.rgb.hexValue;
    }

    decimal_Color = color_PickerCase;
    hexadecimal_Color = decimal_Color.toString(16);
    return hexadecimal_Color;    
};

 

As joojaa from graphicdesign on stackexchange said, I was asking for the color before picking it and I was getting the color form the last time!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2019 Oct 27, 2019

I still do not understand why you are passing a parameter the documentation shows that showColorPicker(). does not use any parameter it just returns a true|false???  You are now testing if its false and return nothing if it is.   

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 28, 2019 Oct 28, 2019

The "isForecolor" variable works as boolean. If it's "true", color picker opens for foreground color, else opens for background color. The "false" after color picker call " if (isPick === true && app.showColorPicker(isForecolor) === false){return;}; " is in case where user clicks cancel without pick a color.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Something is being lost here  Adobe manuals Photoshop JavaScript DOM method showColorPicker() does not accept any parameters so your boolean isFore color would not be used by showColorPicker?

Capture.jpg

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 28, 2019 Oct 28, 2019

Take a look here please!!! 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 28, 2019 Oct 28, 2019

So in external reference there is more information than in official Adobe documents?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 29, 2019 Oct 29, 2019
LATEST

@Kukurykus: Truth is that I have find many extra information in external references... As another guy said, the danger with this, is that they may remove undocumented functionalities!!! I hope they don't. I think it would be better just to update their documents a bit!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2019 Oct 28, 2019

There is much incorrect information one the web so which is right and which is wrong .  The documentation I posted is from Adobe's javascript references manual I believed that to be correct and have checked 5 versions of it. What you posted seems to be from Brett Dixon.  His document shows it takes a boolean parameter. 

 

So now I had to test to see what is what.  Adobe documentation is clearly bad.  It does not state what the method does it just states it return a boolean and failed to state showColorPicker takes a boolean parameter and defaults to true. True open foreground picker, false open background picker  It open the color picker dialog. Which ever color picker dialog is your preference.  And it does take a parameter.  True to set the foreground color false to set the background color. A true return code means a color was set for the foreground or background depending on how the dialog  was opened. False the user did not set a color they canceled the dialog. Most of the scripts I write do no involve user interaction other then its own dialog  and file system dialogs. I never had the need to open the color picker dialog for a user. 

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines