Lloyd Smith
Lloyd Smith

Reputation: 77

Update text in UI in Adobe Illustrator scripting

Is it possible to edit the static text in adobe illustrator UI? For example, I have a number at the bottom that displays the progress of something. I want to update that in a loop to show the progress (X item out of X total items).

I tried creating the text with the UI and updating the variables later, however that didn't work because the GUI code doesn't run again (unless via event listeners).

I also tried to edit the text in the loop, but there is no built-in method for that (like there is in Java).

I just want to know if this is possible in any way (still learning AI scripting). If it's not possible, that's fine, it's not vital, just a neat feature to show the progress of a long/slow loop.

 function startGUI() {
        var inputText, inputNum;

        // Create Main Window
        win = new Window( "dialog", "title", undefined );

        // Enable use of 'Enter' key
        win.addEventListener ("keydown", function(kd) {enter(kd) });

        // Style for Main Window
        win.orientation = "column";
        win.alignChildren = ["fill", "fill"];
        win.preferredSize = [250, 150];

        // Style for group
        var objGrp = win.add("panel", undefined, "group");
        objGrp.orientation = "column";
        objGrp.alignChildren = ["fill", "fill"];

        var titleMsg = objGrp.add ("statictext", undefined, "name:");
        var txt_Input = objGrp.add("edittext { characters: 1, justify: 'center', active: true }");
        txt_Input.helpTip = "";

        var titleMsg = objGrp.add ("statictext", undefined, "Number to start from:");
        var txt_Num = objGrp.add("edittext { characters: 1, justify: 'center' }");
        txt_Num.helpTip = "Object number to start from, other than 1";

        var txt_Count = win.add("statictext", undefined, "Current object: ".concat (itemCount).concat(" out of: ").concat(countTotal)); 

        // Button
        var objBtn = objGrp.add("button", undefined, "Start");
        objGrp.helpTip = "";
        objGrp.onClick = function() {
            inputText = txt_Input.text;
            inputNum = txt_Num.text;

            start(inputText, inputNum);
            app.redraw();
        }

        // Use Enter key
        function enter(k) {
            if (k.keyName == "Enter") {
                inputText = txt_Input.text;
                inputNum = txt_Num.text;

                start(inputText, inputNum);
                app.redraw();
            }
        }

        // Listener for the input
        txt_Input.onChanging = function() {
            app.redraw();
        }


        // Close button
        var quitBtn = win.add("button", undefined, "Close");
        quitBtn.helpTip = "Press Esc to Close";

        // Event listener for the quit button
        quitBtn.onClick = function() {   
            win.close();   
        }  

        // Centering & Show Window
        win.center();
        win.show(); 
    }// end startGUI

Update:

#target illustrator

if (app.documents.length > 0) {

    // Update with new script versions
    var alphaLayersFile = "/AlphabatizeLayers-v1.03.jsx";
    var deleteLayersFile = "/DeleteEmptyLayers-v1.00.jsx";

    var dialogName = "Layer Search Selection"
    var doc = app.activeDocument;
    var docLayers = doc.layers;

    var inputText;
    var caseSense = false;
    var exactWord = false;
    var match = false;
    var textSearch = false;
    var itemCount = 0, countTotal;

    // Display GUI
    startGUI();

    // Main Search function
    function searchAll(layers, txt_Count, win) {
        match = false;
        searchLayers(layers, txt_Count, win);

        // When match is found, show dialog
        if (match) {
            alert("Found match!");
            match = false;
        } else {
            alert("No match found.");
            match = false;
        }
    }


    // Recursive loop to search all layers in active document
    function searchLayers ( layers, txt_Count, win ) {
        var input = inputText;
        var length = layers.length;
        var currentLayer ;

        try {
            countTotal = length;
            for (var i = length; i--;) { //var i = length; i--; //var i = 0; i <= length; i++
                currentLayer = layers[i];

                itemCount++;
                txt_Count.text = "Current object: ".concat(itemCount).concat(" out of: ").concat(countTotal);
                win.update();
                $.sleep(1000);

                var locked = currentLayer.locked;
                var visible = currentLayer.visible;

                if (visible == true || locked == false) {
                    searchLayerName(input, currentLayer);

                    // Search for sublayers, page items or group items
                    if (currentLayer.layers) {
                        searchLayers(currentLayer.layers);                          
                        searchLayers(currentLayer.groupItems);
                        searchLayers(currentLayer.pathItems);
                        searchLayers(currentLayer.compoundPathItems);
                        searchLayers(currentLayer.symbolItems);
                        searchLayers(currentLayer.textFrames);
                    }
                }
            }
        } catch (error) {
            logger (error);
        }
    }// end SearchLayers

    // Search for match between input and layer name
    function searchLayerName( inputText, currentLayer ) {
        try {
            if (inputText) {
                var layerName = "";
                var layerType = currentLayer.typename;
                var searchIndex = -1;
                var exact = false;

                switch (layerType) {
                    default:
                        layerName = currentLayer.name;
                        searchIndex = searchLayer(inputText, layerName);

                        selectLayer(searchIndex, currentLayer, layerType) 
                        break;
                    case "SymbolItem":
                        layerName = currentLayer.symbol.name;
                        searchIndex = searchLayer(inputText, layerName);

                        selectLayer(searchIndex, currentLayer, layerType) 
                        break;
                    case "TextFrame":
                        layerName = currentLayer.contents;
                        searchIndex = searchLayer(inputText, layerName);

                        selectLayer(searchIndex, currentLayer, layerType) 
                        break;
                    case "Layer":
                        layerName = currentLayer.name;
                        searchIndex = searchLayer(inputText, layerName);

                        selectLayer(searchIndex, currentLayer, layerType) 
                        break;
                } // end Switch


            } // end inputText
        } catch (error) {
            logger(error);
        }
    } // end Search

    function searchLayer(inputText, layerName) {
        var searchIndex = -1;
        var caseSensitive = caseSense;
        var exact = exactWord;

        //inputText = fixString(inputText);

        if (caseSensitive) {
            searchIndex = layerName.indexOf(inputText) ;
        } else if (exact) {                 
            if (layerName === inputText) {
                    searchIndex = 0;
            }
        } else {
            searchIndex = layerName.toLowerCase().indexOf(inputText.toLowerCase());
        }

        return searchIndex;
    }

    function selectLayer(searchIndex, currentLayer, layerType) {

        if ( searchIndex != -1 ) {
            if (layerType != "Layer") {
                currentLayer.selected = true;
                match = true;
            } else if (layerType == "Layer") {
                currentLayer.hasSelectedArtwork = true;
                //layer.selected = true;
                match = true;
            }
        }
    } // end selectLayer

    // Display GUI
    function startGUI() {

        // Create Main Window
        var win = new Window( "dialog", dialogName );   

        // Style for Main Window
        win.orientation = "column";
        win.alignChildren = ["fill", "fill"];
        //win.preferredSize = [150, 350];

        // Style for Search group
        var searchGrp = win.add("panel", undefined, "Search Layers");
        searchGrp.orientation = "column";
        searchGrp.alignChildren = ["fill", "fill"];

        var titleMsg = searchGrp.add ("statictext", undefined, "Layer name to search:");
        var txt_Input = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");
        txt_Input.helpTip = "Input letters to search";

        countTotal = docLayers.length;

        txt_Count = win.add("statictext", undefined, "Current object ".concat(itemCount).concat(" out of: ").concat(countTotal));

        // Search Button
        var searchBtn = searchGrp.add("button", undefined, "Search");
        searchBtn.helpTip = "Search from text items";
        searchBtn.onClick = function() {
            inputText = txt_Input.text;
            searchAll(docLayers, txt_Count, win);
            app.redraw();
        }

        // Listener for the input
        txt_Input.onChanging = function() {
            app.redraw();
        }

        // Options
        var optionsGrp = win.add("panel", undefined, "Options");
        optionsGrp.orientation = "row";
        optionsGrp.margins = [10, 15, 10, 6];
        optionsGrp.alignChildren = ["fill", "fill"];

        // Radio button: Case Sensitive
        var rdb_caseSensitive = optionsGrp.add ("radiobutton", undefined, "Match Case");
        rdb_caseSensitive.helpTip = "Case sensitive search";
        rdb_caseSensitive.value = false;

        // Listener: Case Sensitive
        rdb_caseSensitive.onClick = function() {
            caseSense = rdb_caseSensitive.value;
            app.redraw();
        }

        // Radio button: Exact Word
        var rdb_exactWord = optionsGrp.add ("radiobutton", undefined, "Match Exact Word");
        rdb_exactWord.helpTip = "Search exact word";
        rdb_exactWord.value = false;

        // Listener: Exact Word
        rdb_exactWord.onClick = function() {
            exactWord = rdb_exactWord.value;
            app.redraw();
        }

        // Radio button: None
        var chk_none = optionsGrp.add ("radiobutton", undefined, "None");
        chk_none.helpTip = "Use no extra option";
        chk_none.value = false;

        // Radio button: None
        chk_none.onClick = function() {
            exactWord = false;
            caseSense = false;
            //textSearch = chk_textSearch.value;
            app.redraw();
        }

        // Close button
        var quitBtn = win.add("button", undefined, "Close");
        quitBtn.helpTip = "Press Esc to Close";

        // Event listener for the quit button
        quitBtn.onClick = function() {   
            win.close();   
        }  

        // Centering & Show Window
        win.center();
        win.show(); 
    }// end startGUI

        // Check to see if parent layer is visible
        // Sometimes child layers that inheirit locked/hidden
        // status from parent will return 'undefined' for visibility
        function checkParentVisibility(layer) {
            for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
                var pvis = parent.visible;
                if(!pvis) {
                    return false;
                } else {
                    return true;
                }
            }
         }
    }

    function fixString(str) {
        str = str.split("_").join(" ");
        return str;
    }

    // Prints stack trace
    // Note: Don't print error message unless debugging
    function logger(e) {
        var errorMsg = "";

        errorMsg = errorMsg.concat(e.line, "\n", e.message, "\n", e.stack);
        $.writeln(errorMsg);
    }
} else {
        alert("You do not have any document opened!");
}

Upvotes: 1

Views: 2684

Answers (2)

Iconify
Iconify

Reputation: 11

I second the recommendation to use a progress bar. I have created a demo for progress bars in Illustrator using JSX (as well as some other demos) here: https://github.com/iconifyit/illustrator-jsx-demos

Let me know if you have any trouble with the demos and I will clarify whatever I can.

Upvotes: 1

Charu Rajput
Charu Rajput

Reputation: 653

Here is the one script, that will update static text

var countTotal = 10;
var itemCount = 0;

function startGUI() {
    var inputText, inputNum;
    // Create Main Window
    win = new Window("dialog", "title", undefined);

    // Enable use of 'Enter' key
    win.addEventListener("keydown", function(kd) {
        enter(kd)
    });

    // Style for Main Window
    win.orientation = "column";
    win.alignChildren = ["fill", "fill"];
    win.preferredSize = [250, 150];

    // Style for group
    var objGrp = win.add("panel", undefined, "Panel");
    objGrp.orientation = "column";
    objGrp.alignChildren = ["fill", "fill"];

    var titleMsg = objGrp.add("statictext", undefined, "Number to start from:");
    var txt_Num = objGrp.add("edittext { characters: 1, justify: 'center' }");
    txt_Num.helpTip = "Object number to start from, other than 1";

    txt_Count = win.add("statictext", undefined, "Current object: ".concat(itemCount).concat(" out of: ").concat(countTotal));

    // Button
    var btn = objGrp.add("button", undefined, "Start");
    btn.onClick = function() {
        inputText = txt_Num.text;
        itemCount = inputText
        inputNum = txt_Num.text;
        for (var i = 0; i < 10; i++) {
            itemCount++;
            txt_Count.text = "Current object: ".concat(itemCount).concat(" out of: ").concat(countTotal);
            win.update();
            $.sleep(1000);
        }
        win.close();
    }

    // Use Enter key
    function enter(k) {
        if (k.keyName == "Enter") {
            inputText = txt_Num.text;
            inputNum = txt_Num.text;
            inputNum = txt_Num.text;
            for (var i = 0; i < 10; i++) {
                itemCount++;
                txt_Count.text = "Current object: ".concat(itemCount).concat(" out of: ").concat(countTotal);
                win.update();
                $.sleep(1000);
            }
            win.close();
        }
    }

    // Close button
    var quitBtn = win.add("button", undefined, "Close");

    // Event listener for the quit button
    quitBtn.onClick = function() {
        win.close();
    }

    // Centering & Show Window
    win.center();
    win.show();
} // end startGUI

startGUI();

You can change the script as per requirement. I have just removed some fields from your code. I have used for loop just to update the value of itemCount in static text and used $.sleep(1000) so that change should be visible otherwise in for loop you will not able to see the change.

Hope this will help you. Also, you can use progressbar if you want to show progress

Upvotes: 0

Related Questions