javascriptadobe-indesignadobe-illustratorextendscriptphotoshop-script

Edit running script and replace an array


I'm writing a script that should update itself, but I'm stuck.
Basically I have to replace a variable containing an RGB color array (which value changes during this script) and overwrite this variable in the .jsx file.
For clarity I give an example:

var RGBColor = [0, 0, 0]; //Black RGB color
var RGBColor2 = RGBColor;
// The script runs and change RGBColor2 value to [0.2, 0.55, 0.2]
var UpdateFile = function () { 
    var JSX = File ($.fileName);
    JSX.open ("e");
    JSX.write ((JSX.read ()).replace (RGBColor, RGBColor2));
    JSX.close ();
    }
UpdateFile ();

This is what I should theoretically do, but I can't replace the variable in any way (I tried everything, even with the method "open ("r")", "read", "close", "open ("w")", "write").
Does anyone know how to make this script work or know a better way to write it?.


UPDATE

The final script will be a scriptui panel with six sliders, three for the first color (R, G, B), and three for the second color.
When the "Apply" button is pressed, the script will have to replace the rgb variables inside the script.
After messing with Ghoul Fool's code, I decided to replace the variables and not the arrays because it seemed more difficult. However I cannot overwrite the variables.
Here is the code:

    var Red1 = 0;
    var Green1 = 0;
    var Blue1 = 0;
    var Red2 = 1;
    var Green2 = 1;
    var Blue2 = 1;
    var FirstColor = [Red1, Green1, Blue1];
    var SecondColor = [Red2, Green2, Blue2];
    var MainPanel = new Window ("dialog", "Panel1");
    var Text1 = MainPanel.add ("statictext");
    Text1.text = "First color";
    var RSlider1 = MainPanel.add ("slider");
    RSlider1.minvalue = 0;
    RSlider1.maxvalue = 255;
    RSlider1.value = Math.round (FirstColor[0] * 255);
    var RNumber1 = MainPanel.add ("statictext", undefined, RSlider1.value);
    RNumber1.preferredSize.width = 25;
    RNumber1.graphics.foregroundColor = RNumber1.graphics.newPen (RNumber1.graphics.PenType.SOLID_COLOR, [1, 0, 0], 1);
    RNumber1.graphics.disabledForegroundColor = RNumber1.graphics.foregroundColor;
    RNumber1.graphics.font = ScriptUI.newFont (RNumber1.graphics.font.name, "Bold", RNumber1.graphics.font.size);
    var GSlider1 = MainPanel.add ("slider");
    GSlider1.minvalue = 0;
    GSlider1.maxvalue = 255;
    GSlider1.value = Math.round (FirstColor[1] * 255);
    var GNumber1 = MainPanel.add ("statictext", undefined, GSlider1.value);
    GNumber1.preferredSize.width = 25;
    GNumber1.graphics.foregroundColor = GNumber1.graphics.newPen (GNumber1.graphics.PenType.SOLID_COLOR, [0, 1, 0], 1);
    GNumber1.graphics.disabledForegroundColor = GNumber1.graphics.foregroundColor;
    GNumber1.graphics.font = ScriptUI.newFont (GNumber1.graphics.font.name, "Bold", GNumber1.graphics.font.size);
    var BSlider1 = MainPanel.add ("slider");
    BSlider1.minvalue = 0;
    BSlider1.maxvalue = 255;
    BSlider1.value = Math.round (FirstColor[2] * 255);
    var BNumber1 = MainPanel.add ("statictext", undefined, BSlider1.value);
    BNumber1.preferredSize.width = 25;
    BNumber1.graphics.foregroundColor = BNumber1.graphics.newPen (BNumber1.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
    BNumber1.graphics.disabledForegroundColor = BNumber1.graphics.foregroundColor;
    BNumber1.graphics.font = ScriptUI.newFont (BNumber1.graphics.font.name, "Bold", BNumber1.graphics.font.size);
    RSlider1.onChanging = GSlider1.onChanging = BSlider1.onChanging = function () {
        RNumber1.text = Math.round (RSlider1.value);
        GNumber1.text = Math.round (GSlider1.value);
        BNumber1.text = Math.round (BSlider1.value);
        Red1 = Math.floor ((Math.round (RSlider1.value) / 255) * 100) / 100;
        Green1 = Math.floor ((Math.round (GSlider1.value) / 255) * 100) / 100;
        Blue1 = Math.floor ((Math.round (BSlider1.value) / 255) * 100) / 100;
        }
    var Text2 = MainPanel.add ("statictext");
    Text2.text = "Second color";
    var RSlider2 = MainPanel.add ("slider");
    RSlider2.minvalue = 0;
    RSlider2.maxvalue = 255;
    RSlider2.value = Math.round (SecondColor[0] * 255);
    var RNumber2 = MainPanel.add ("statictext", undefined, RSlider2.value);
    RNumber2.preferredSize.width = 25;
    RNumber2.graphics.foregroundColor = RNumber2.graphics.newPen (RNumber2.graphics.PenType.SOLID_COLOR, [1, 0, 0], 1);
    RNumber2.graphics.disabledForegroundColor = RNumber2.graphics.foregroundColor;
    RNumber2.graphics.font = ScriptUI.newFont (RNumber2.graphics.font.name, "Bold", RNumber2.graphics.font.size);
    var GSlider2 = MainPanel.add ("slider");
    GSlider2.minvalue = 0;
    GSlider2.maxvalue = 255;
    GSlider2.value = Math.round (SecondColor[1] * 255);
    var GNumber2 = MainPanel.add ("statictext", undefined, GSlider2.value);
    GNumber2.preferredSize.width = 25;
    GNumber2.graphics.foregroundColor = GNumber2.graphics.newPen (GNumber2.graphics.PenType.SOLID_COLOR, [0, 1, 0], 1);
    GNumber2.graphics.disabledForegroundColor = GNumber2.graphics.foregroundColor;
    GNumber2.graphics.font = ScriptUI.newFont (GNumber2.graphics.font.name, "Bold", GNumber2.graphics.font.size);
    var BSlider2 = MainPanel.add ("slider");
    BSlider2.minvalue = 0;
    BSlider2.maxvalue = 255;
    BSlider2.value = Math.round (SecondColor[2] * 255);
    var BNumber2 = MainPanel.add ("statictext", undefined, BSlider2.value);
    BNumber2.preferredSize.width = 25;
    BNumber2.graphics.foregroundColor = BNumber2.graphics.newPen (BNumber2.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
    BNumber2.graphics.disabledForegroundColor = BNumber2.graphics.foregroundColor;
    BNumber2.graphics.font = ScriptUI.newFont (BNumber2.graphics.font.name, "Bold", BNumber2.graphics.font.size);
    RSlider2.onChanging = GSlider2.onChanging = BSlider2.onChanging = function () {
        RNumber2.text = Math.round (RSlider2.value);
        GNumber2.text = Math.round (GSlider2.value);
        BNumber2.text = Math.round (BSlider2.value);
        Red2 = Math.floor ((Math.round (RSlider2.value) / 255) * 100) / 100;
        Green2 = Math.floor ((Math.round (GSlider2.value) / 255) * 100) / 100;
        Blue2 = Math.floor ((Math.round (BSlider2.value) / 255) * 100) / 100;
        }
    var Apply = MainPanel.add ("button", undefined, "Apply changes");
    Apply.onClick = function () {
        var JSX = File ($.fileName);
        JSX.open ("r");
        var JSXXX = JSX.read ();
        JSX.close ();
        JSX.open ("w");
//  This part is not clear to me. What should I replace the colors with?
        JSXXX = JSXXX.replace (Red1, FirstColor [0]);
        JSXXX = JSXXX.replace (Green1, FirstColor [1]);
        JSXXX = JSXXX.replace (Blue1, FirstColor [2]);
        JSXXX = JSXXX.replace (Red2, SecondColor [0]);
        JSXXX = JSXXX.replace (Green2, SecondColor [1]);
        JSXXX = JSXXX.replace (Blue2, SecondColor [2]);
        JSX.write(JSXXX);
        JSX.close ();
        MainPanel.close ();
        }
    alert ("Color 1 : " + Red1 + " - " + Green1 + " - " + Blue1);
    alert ("Color 2 : " + Red2 + " - " + Green2 + " - " + Blue2);
    MainPanel.show ();

The dialog will be included in the final script and will be used to change the color of the dialog itself.


Solution

  • As Ghoul Fool suggested to me, I tried to post a question on community.adobe.com, but to date the "Actions and Scripting" section seems not very popular.
    In the end I was unable to do what I wanted, so I opted for the creation of an external XML file that the script can read and write.
    Here is the code:

        var XMLFile = File (Folder.desktop + "/config.xml");
        if (!XMLFile.exists) {
            Create ();
            }
        XMLFile.open ("r");
        var XMLObj = XML (XMLFile.read ());
        XMLFile.close ();
        var Red1 = XMLObj["firstred"];
        var Green1 = XMLObj["firstgreen"];
        var Blue1 = XMLObj["firstrblue"];
        var Red2 = XMLObj["secondred"];
        var Green2 = XMLObj["secondgreen"];
        var Blue2 = XMLObj["secondblue"];
        var FirstColor = [Number (Red1), Number (Green1), Number (Blue1)];
        var SecondColor = [Number (Red2), Number (Green2), Number (Blue2)];
        var MainPanel = new Window ("dialog", "Panel1");
        var Text1 = MainPanel.add ("statictext");
        Text1.text = "First color";
        var RSlider1 = MainPanel.add ("slider");
        RSlider1.minvalue = 0;
        RSlider1.maxvalue = 255;
        RSlider1.value = Math.round (Red1 * 255);
        var RNumber1 = MainPanel.add ("statictext", undefined, RSlider1.value);
        RNumber1.preferredSize.width = 25;
        RNumber1.graphics.foregroundColor = RNumber1.graphics.newPen (RNumber1.graphics.PenType.SOLID_COLOR, [1, 0, 0], 1);
        RNumber1.graphics.disabledForegroundColor = RNumber1.graphics.foregroundColor;
        RNumber1.graphics.font = ScriptUI.newFont (RNumber1.graphics.font.name, "Bold", RNumber1.graphics.font.size);
        var GSlider1 = MainPanel.add ("slider");
        GSlider1.minvalue = 0;
        GSlider1.maxvalue = 255;
        GSlider1.value = Math.round (Green1 * 255);
        var GNumber1 = MainPanel.add ("statictext", undefined, GSlider1.value);
        GNumber1.preferredSize.width = 25;
        GNumber1.graphics.foregroundColor = GNumber1.graphics.newPen (GNumber1.graphics.PenType.SOLID_COLOR, [0, 1, 0], 1);
        GNumber1.graphics.disabledForegroundColor = GNumber1.graphics.foregroundColor;
        GNumber1.graphics.font = ScriptUI.newFont (GNumber1.graphics.font.name, "Bold", GNumber1.graphics.font.size);
        var BSlider1 = MainPanel.add ("slider");
        BSlider1.minvalue = 0;
        BSlider1.maxvalue = 255;
        BSlider1.value = Math.round (Blue1 * 255);
        var BNumber1 = MainPanel.add ("statictext", undefined, BSlider1.value);
        BNumber1.preferredSize.width = 25;
        BNumber1.graphics.foregroundColor = BNumber1.graphics.newPen (BNumber1.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
        BNumber1.graphics.disabledForegroundColor = BNumber1.graphics.foregroundColor;
        BNumber1.graphics.font = ScriptUI.newFont (BNumber1.graphics.font.name, "Bold", BNumber1.graphics.font.size);
        RSlider1.onChanging = GSlider1.onChanging = BSlider1.onChanging = function () {
            RNumber1.text = Math.round (RSlider1.value);
            GNumber1.text = Math.round (GSlider1.value);
            BNumber1.text = Math.round (BSlider1.value);
            Red1 = Math.floor ((RSlider1.value / 255) * 100) / 100;
            Green1 = Math.floor ((GSlider1.value / 255) * 100) / 100;
            Blue1 = Math.floor ((BSlider1.value / 255) * 100) / 100;
            }
        var Text2 = MainPanel.add ("statictext");
        Text2.text = "Second color";
        var RSlider2 = MainPanel.add ("slider");
        RSlider2.minvalue = 0;
        RSlider2.maxvalue = 255;
        RSlider2.value = Math.round (Red2 * 255);
        var RNumber2 = MainPanel.add ("statictext", undefined, RSlider2.value);
        RNumber2.preferredSize.width = 25;
        RNumber2.graphics.foregroundColor = RNumber2.graphics.newPen (RNumber2.graphics.PenType.SOLID_COLOR, [1, 0, 0], 1);
        RNumber2.graphics.disabledForegroundColor = RNumber2.graphics.foregroundColor;
        RNumber2.graphics.font = ScriptUI.newFont (RNumber2.graphics.font.name, "Bold", RNumber2.graphics.font.size);
        var GSlider2 = MainPanel.add ("slider");
        GSlider2.minvalue = 0;
        GSlider2.maxvalue = 255;
        GSlider2.value = Math.round (Green2 * 255);
        var GNumber2 = MainPanel.add ("statictext", undefined, GSlider2.value);
        GNumber2.preferredSize.width = 25;
        GNumber2.graphics.foregroundColor = GNumber2.graphics.newPen (GNumber2.graphics.PenType.SOLID_COLOR, [0, 1, 0], 1);
        GNumber2.graphics.disabledForegroundColor = GNumber2.graphics.foregroundColor;
        GNumber2.graphics.font = ScriptUI.newFont (GNumber2.graphics.font.name, "Bold", GNumber2.graphics.font.size);
        var BSlider2 = MainPanel.add ("slider");
        BSlider2.minvalue = 0;
        BSlider2.maxvalue = 255;
        BSlider2.value = Math.round (Blue2 * 255);
        var BNumber2 = MainPanel.add ("statictext", undefined, BSlider2.value);
        BNumber2.preferredSize.width = 25;
        BNumber2.graphics.foregroundColor = BNumber2.graphics.newPen (BNumber2.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
        BNumber2.graphics.disabledForegroundColor = BNumber2.graphics.foregroundColor;
        BNumber2.graphics.font = ScriptUI.newFont (BNumber2.graphics.font.name, "Bold", BNumber2.graphics.font.size);
        RSlider2.onChanging = GSlider2.onChanging = BSlider2.onChanging = function () {
            RNumber2.text = Math.round (RSlider2.value);
            GNumber2.text = Math.round (GSlider2.value);
            BNumber2.text = Math.round (BSlider2.value);
            Red2 = Math.floor ((RSlider2.value / 255) * 100) / 100;
            Green2 = Math.floor ((GSlider2.value / 255) * 100) / 100;
            Blue2 = Math.floor ((BSlider2.value / 255) * 100) / 100;
            }
        var Apply = MainPanel.add ("button", undefined, "Apply changes");
        Apply.onClick = function () {
            var XML = new File (Folder.desktop + "/config.xml");
            XML.open ('w');
            XML.writeln ('<?xml version="1.0" encoding="utf-8"?>');
            XML.writeln ("<variables>");
            XML.writeln ("  <firstred>" + Red1 + "</firstred>");
            XML.writeln ("  <firstgreen>" + Green1 + "</firstgreen>");
            XML.writeln ("  <firstrblue>" + Blue1 + "</firstrblue>");
            XML.writeln ("  <secondred>" + Red2 + "</secondred>");
            XML.writeln ("  <secondgreen>" + Green2 + "</secondgreen>");
            XML.writeln ("  <secondblue>" + Blue2 + "</secondblue>");
            XML.writeln ("</variables>");
            XML.close ();
            MainPanel.close ();
            }
        alert ("Color 1 : " + Red1 + " - " + Green1 + " - " + Blue1);
        alert ("Color 2 : " + Red2 + " - " + Green2 + " - " + Blue2);
        MainPanel.show ();
        function Create () {
            var XML = new File (Folder.desktop + "/config.xml");
            XML.open ('w');
            XML.writeln ('<?xml version="1.0" encoding="utf-8"?>');
            XML.writeln ("<variables>");
            XML.writeln ("  <firstred>0</firstred>");
            XML.writeln ("  <firstgreen>0</firstgreen>");
            XML.writeln ("  <firstrblue>0</firstrblue>");
            XML.writeln ("  <secondred>1</secondred>");
            XML.writeln ("  <secondgreen>1</secondgreen>");
            XML.writeln ("  <secondblue>1</secondblue>");
            XML.writeln ("</variables>");
            XML.close ();
            }