Skip to main content
Inspiring
March 9, 2020
Answered

Adapting a script to auto click 'ok'

  • March 9, 2020
  • 4 replies
  • 3230 views

Hi, I am using a useful script to alter character styles within Illustrator, as part of an automated workflow, but I need the script to auto click the 'OK' button to save having to manually press it everytime.

 

I am very new to scripting but have managed to alter some other elements to make the script do what I need it to, but this has me stumped. Would anyone please be able to help me on this?

 

I presume the area I should be looking at is within the following:

  win.buttons = win.add("group");
        win.buttons.alignment = "center";
        win.buttons.ok = win.buttons.add("button", undefined, "OK");
        win.buttons.cancel = win.buttons.add("button", undefined, "Cancel");

        var isValuesOK = function(){
            _opt.rex  = win.etext1.text;
            if(_opt.rex  == ""){
                showError("please input a regular expression to search");
                return false;
            }
            
            var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
            if(style_name == ""){
                showError("please select a character style to apply");
                return false;
            }
            _opt.character_style = app.activeDocument.characterStyles.getByName(style_name);

            _opt.ignore_case = win.check0.value;
            _opt.clearing_overrides = win.check1.value;
            return true;
        }
        
        win.buttons.ok.onClick = function(){
            try{
                win.enabled = false;
                if(isValuesOK() == false) return;
                if(main() == false) return;
            } catch(e) {
                alert(e);
            } finally {
                win.enabled = true;
            }
            win.close();
        }

        win.buttons.cancel.onClick = function(){
            win.close();
        }
        
        win.show();

win.buttons.ok.click;

    }

 

[ Scripting tag added and formatted as code by moderator ]

This topic has been closed for replies.
Correct answer Disposition_Dev

I have tried that but it seemed to crash Illustrator. Maybe I have caused confusion by not providing the entire script. Not sure if this makes it any easier to determine if it is possible:

 

(function(){
    const SCRIPTNAME = "apply Style With RegExp";

    var _opt = {
        ignore_case : false,
        clearing_overrides : true,
        styles : [],
        input_field_width : 100,
        rex : "",
        character_style : "",
        text_frames : []
    }

    function showDialog(){
        getCharacterStyleNames();
    
        _opt.text_frames = [];
        getTextFramesFromSelection(_opt.text_frames);
        if(_opt.text_frames.length < 1){
            showError("no text object selected");
            return;
        }

        if(app.documents.length < 1) return;
        var adoc = app.activeDocument;
        
        var win = new Window("dialog", SCRIPTNAME);
        win.alignChildren = "left";
        
        win.add("statictext", undefined, "regular expression to search");
        win.etext1 = win.add("edittext", undefined, "EN|FR|PL|DE|RO|RUS|RO|ES|PT|TR");
        win.etext1.characters = _opt.input_field_width;

        win.add("statictext", undefined, "character style to apply");
        win.list1 = win.add("dropdownlist", undefined, _opt.styles);
        win.list1.selection = 1;
        
        win.check0 = win.add("checkbox", undefined, "ignore case");
        win.check0.value = _opt.ignore_case;

        win.check1 = win.add("checkbox", undefined, "clearing overrides");
        win.check1.value = _opt.clearing_overrides;

        win.buttons = win.add("group");
        win.buttons.alignment = "center";
        win.buttons.ok = win.buttons.add("button", undefined, "OK");
        win.buttons.cancel = win.buttons.add("button", undefined, "Cancel");

        var isValuesOK = function(){
            _opt.rex  = win.etext1.text;
            if(_opt.rex  == ""){
                showError("please input a regular expression to search");
                return false;
            }
            
            var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
            if(style_name == ""){
                showError("please select a character style to apply");
                return false;
            }
            _opt.character_style = app.activeDocument.characterStyles.getByName(style_name);

            _opt.ignore_case = win.check0.value;
            _opt.clearing_overrides = win.check1.value;
            return true;
        }
        
        win.buttons.ok.onClick = function(){
            try{
                win.enabled = false;
                if(isValuesOK() == false) return;
                if(main() == false) return;
            } catch(e) {
                alert(e);
            } finally {
                win.enabled = true;
            }
            win.close();
        }

        win.buttons.cancel.onClick = function(){
            win.close();
        }
        
        win.show();
	main();

    }

    function getCharacterStyleNames(){
        var styles = app.activeDocument.characterStyles;
        _opt.styles = [];
        for(var i = 0; i < styles.length; i++){
            _opt.styles.push(styles[i].name);
        }
    }

    
    function main(){
 
        // regex_changeContentsOfWordOrString_RemainFormatting.jsx
        // regards pixxxel schubser
        
        var result, aCon;
        var regex_option = _opt.ignore_case ? "gmi" : "gm";
    
        for(var i = _opt.text_frames.length - 1; i >= 0; i--){
            var atf = _opt.text_frames[i];
    
            var s = new RegExp(_opt.rex, regex_option);

            while ( result = s.exec(atf.contents)) {
                
                try {
     
                    aCon = atf.characters[result.index];
                    
                    aCon.length = result[0].length;
                    
                    _opt.character_style.applyTo(aCon, _opt.clearing_overrides);

                    
                } catch (e) {};
            }
            		
        }
    }
    
    function getTextFramesFromSelection(textFrames, items){
        if(!items) items = app.activeDocument.selection;
        for(var i = 0; i < items.length; i++){
            if(items[i].locked || items[i].hidden){
                continue;
            } else if(items[i].typename == "TextFrame"){
                textFrames.push(items[i]);
            } else if(items[i].typename == "GroupItem"){
                getTextFramesFromSelection(textFrames, items[i].pageItems);
            }
        }
    }

    function showError(msg){
        alert(SCRIPTNAME + "\rERROR :\r"
              + msg);
    }
    
    showDialog();
})()		

Here. Try this. Be sure to manually assign the name of the desired Character Style to the MY_STYLE constant. 

 

#target illustrator
(function()
{
    const SCRIPTNAME = "apply Style With RegExp";

    //if you're using the same character style every time, just hard code it here
    const MY_STYLE = "[Normal Character Style]"; //the name of the character style you want to use
    const CHARACTER_STYLE = app.activeDocument.characterStyles.getByName(MY_STYLE);

    var _opt = {
        ignore_case: false,
        clearing_overrides: true,
        styles: [],
        input_field_width: 100,
        rex: "EN|FR|PL|DE|RO|RUS|RO|ES|PT|TR",
        character_style: CHARACTER_STYLE,
        text_frames: []
    }

    function showDialog()
    {
        // getCharacterStyleNames();

        getTextFramesFromSelection(_opt.text_frames);
        if (_opt.text_frames.length < 1)
        {
            showError("no text object selected");
            return;
        }

        if (app.documents.length < 1) return;
        var adoc = app.activeDocument;
        main();

        // _opt.character_style = app.activeDocument.characterStyles.getByName(style_name);
        

    //     var win = new Window("dialog", SCRIPTNAME);
    //     win.alignChildren = "left";

    //     win.add("statictext", undefined, "regular expression to search");
    //     win.etext1 = win.add("edittext", undefined, "EN|FR|PL|DE|RO|RUS|RO|ES|PT|TR");
    //     win.etext1.characters = _opt.input_field_width;

    //     win.add("statictext", undefined, "character style to apply");
    //     win.list1 = win.add("dropdownlist", undefined, _opt.styles);
    //     win.list1.selection = 1;

    //     win.check0 = win.add("checkbox", undefined, "ignore case");
    //     win.check0.value = _opt.ignore_case;

    //     win.check1 = win.add("checkbox", undefined, "clearing overrides");
    //     win.check1.value = _opt.clearing_overrides;

    //     win.buttons = win.add("group");
    //     win.buttons.alignment = "center";
    //     win.buttons.ok = win.buttons.add("button", undefined, "OK");
    //     win.buttons.cancel = win.buttons.add("button", undefined, "Cancel");

    //     var isValuesOK = function()
    //     {
    //         _opt.rex = win.etext1.text;
    //         if (_opt.rex == "")
    //         {
    //             showError("please input a regular expression to search");
    //             return false;
    //         }

    //         var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
    //         if (style_name == "")
    //         {
    //             showError("please select a character style to apply");
    //             return false;
    //         }
    //         _opt.character_style = app.activeDocument.characterStyles.getByName(style_name);

    //         _opt.ignore_case = win.check0.value;
    //         _opt.clearing_overrides = win.check1.value;
    //         return true;
    //     }

    //     win.buttons.ok.onClick = function()
    //     {
    //         try
    //         {
    //             win.enabled = false;
    //             if (isValuesOK() == false) return;
    //             if (main() == false) return;
    //         }
    //         catch (e)
    //         {
    //             alert(e);
    //         }
    //         finally
    //         {
    //             win.enabled = true;
    //         }
    //         win.close();
    //     }

    //     win.buttons.cancel.onClick = function()
    //     {
    //         win.close();
    //     }

    //     win.show();
    //     main();

    }

    function getCharacterStyleNames()
    {
        var styles = app.activeDocument.characterStyles;
        _opt.styles = [];
        for (var i = 0; i < styles.length; i++)
        {
            _opt.styles.push(styles[i].name);
        }
    }


    function main()
    {

        // regex_changeContentsOfWordOrString_RemainFormatting.jsx
        // regards pixxxel schubser

        var result, aCon;
        var regex_option = _opt.ignore_case ? "gmi" : "gm";

        for (var i = _opt.text_frames.length - 1; i >= 0; i--)
        {
            var atf = _opt.text_frames[i];

            var s = new RegExp(_opt.rex, regex_option);

            while (result = s.exec(atf.contents))
            {

                try
                {

                    aCon = atf.characters[result.index];

                    aCon.length = result[0].length;

                    _opt.character_style.applyTo(aCon, _opt.clearing_overrides);


                }
                catch (e)
                {};
            }

        }
    }

    function getTextFramesFromSelection(textFrames, items)
    {
        if (!items) items = app.activeDocument.selection;
        for (var i = 0; i < items.length; i++)
        {
            if (items[i].locked || items[i].hidden)
            {
                continue;
            }
            else if (items[i].typename == "TextFrame")
            {
                textFrames.push(items[i]);
            }
            else if (items[i].typename == "GroupItem")
            {
                getTextFramesFromSelection(textFrames, items[i].pageItems);
            }
        }
    }

    function showError(msg)
    {
        alert(SCRIPTNAME + "\rERROR :\r" + msg);
    }

    showDialog();
})()

4 replies

Mayhem SWE
Inspiring
March 10, 2020

Your last line will never do anything because you are not actually calling the function. Try adding the parantheses below:

win.buttons.ok.click();

However, unless this is actually a floating window, I believe Illustrator will pause execution of the rest of the script until the dialog has been dismissed. Programmatically clicking the button may not be convenient or even possible.

Would be much better to bypass the dialog entirely as already suggested by CarlosCanto.

glibshaftAuthor
Inspiring
March 10, 2020

Ok, great , thanks.

 

Would the script need to be rewritten to remove the dialogue box due to the 'onclick' function, or would it simply be a case of adding something to the script to tell it not to show the dialogue box?

Mayhem SWE
Inspiring
March 10, 2020

Difficult to say for sure without combing through the entire script. Based on only the small snippet above, the least intrusive edit would be to comment out the line that shows the dialog box and add in only the bare minimum relevant parts of the onClick function. Replace this…

 win.show();

win.buttons.ok.click;

…with this… 

//win.show();
main();

 

CarlosCanto
Community Expert
Community Expert
March 10, 2020

exactly, if the options in the dialog never change, the data collected in the fields could be incorporated into the script. 

pixxxelschubser
Community Expert
Community Expert
March 10, 2020

Removing the whole dialog isn't an option for you? That seems to be the best solution.

glibshaftAuthor
Inspiring
March 10, 2020

Would that mean that the dialogue would just not appear, but still run the script in the way it does in it's current state (sorry if these are silly questions, like I say, I am very new to this)?

 

If so, that would be ideal, but I am not sure of how to go about this.

CarlosCanto
Community Expert
Community Expert
March 9, 2020

I'm not clear, can you elaborate? does the ok button start the process? does the ok button save the file?

glibshaftAuthor
Inspiring
March 10, 2020

Hi, the 'ok' button runs the script to apply a character style to the selected text. I have amended the script to pre-populate all the required areas of the dialogue box, so that is not an issue. I just need to remove the human element of clicking the ok button and have it automated within the script, if that is possible?