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

Adapting a script to auto click 'ok'

Participant ,
Mar 09, 2020 Mar 09, 2020

Copy link to clipboard

Copied

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 ]

TOPICS
Scripting

Views

1.9K

Translate

Translate

Report

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

Community Expert , Mar 11, 2020 Mar 11, 2020

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: f
...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 09, 2020 Mar 09, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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();

 

Votes

Translate

Translate

Report

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 ,
Mar 10, 2020 Mar 10, 2020

Copy link to clipboard

Copied

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();
})()		

Votes

Translate

Translate

Report

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 ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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();
})()

Votes

Translate

Translate

Report

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 ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

You, sir, are a total star!! Thanks so much for this, it does exactly what I need it to now.

 

I really appreciate your help with this.

Votes

Translate

Translate

Report

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 ,
Apr 29, 2020 Apr 29, 2020

Copy link to clipboard

Copied

LATEST

This script is working really well but I am finding I have to create one for each character style I want to apply.

Would it be possible to 'stack' different character styles to apply and to choose different objects on the artboard to apply them to, possibly through Attribute notes within one script?

Votes

Translate

Translate

Report

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