Search code examples
javascriptadobe-indesignextendscriptadobe-scriptuigrep-indesign

InDesign Script not working when dialog window is added


I am trying to create a script based on GREPs that replaces fonts in non-latin characters with a compatible font, but when I add it to a dialog window it does not work anymore (it works by itself). I have identified the point where it stops running and it's between these two lines:

var options = app.findChangeGrepOptions.properties;

app.findGrepPreferences = NothingEnum.NOTHING;

When I debug the script, I get the following error: Runtime Error: Error Code# 30486: Cannot handle the request because a modal dialog or alert is active.

There should be no dialog or alert at that point, and this doesn't happen when I run the script on its own. Can someone give me a hand here? I don't really understand why this is happening. The complete script is this one:

var dialog = new Window("dialog"); 
    dialog.text = "Asian font replacement"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"}); 
    statictext1.text = "Select the language"; 

var languages_array = ["-","Japanese","Simplified Chinese","Traditional Chinese","Korean","Thai"]; 
var languages = group1.add("dropdownlist", undefined, undefined, {name: "languages", items: languages_array}); 
    languages.selection = 0; 
    languages.preferredSize.width = 88; 

// GROUP2
// ======
var group2 = dialog.add("group", undefined, {name: "group2"}); 
    group2.orientation = "row"; 
    group2.alignChildren = ["left","center"]; 
    group2.spacing = 10; 
    group2.margins = 0; 

var Closebutton = group2.add("button", undefined, undefined, {name: "Closebutton"}); 
    Closebutton.text = "Cancel"; 
    Closebutton.justify = "left"; 
    Closebutton.alignment = ["left","top"]; 

var Runbutton = group2.add("button", undefined, undefined, {name: "Runbutton"}); 
    Runbutton.text = "Run"; 
    Runbutton.alignment = ["left","bottom"]; 


Runbutton.onClick = function(){
    if (languages.selection == "1")
{
    dialog.close();
    JP();
    alert("Font has been applied")  
}
    if (languages.selection == "4")
{
    dialog.close();
    KO();  
    alert("Font has been applied")
}
};

//Japanese GREP

function  JP() {
    if (app.layoutWindows.length == 0) return;
    var changeObject = app.documents[0];
    if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
    var doc = app.documents[0];
    var style;
    var scriptVersion = app.scriptPreferences.version;
    app.scriptPreferences.version.constructor.name // Result:String
    var options = app.findChangeGrepOptions.properties;
    //it stops here
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    
// Query [[JP_black]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Black"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Heavy"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_bold]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Bold"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_bolditalic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Bold", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_heavy]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Heavy"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Heavy"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_italic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Regular ", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_light]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Light"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_lightitalic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Light", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_reg]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Regular"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Regular "});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_semib1]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semi Bold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Medium"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[JP_semib2]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semibold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Medium"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findChangeGrepOptions.properties = options;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.scriptPreferences.version = scriptVersion;
};

function getStyleByString(docOrGroup, string, property) {
    if (string == '[No character style]') return docOrGroup[property][0];
    if (string == '[No paragraph style]') return docOrGroup[property][0];
    if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
    stringResult = string.match (/^(.*?[^\\]):(.*)$/);
    var styleName = (stringResult) ? stringResult[1] : string;
    styleName = styleName.replace (/\\:/g, ':');
    remainingString = (stringResult) ? stringResult[2] : '';
    var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
    var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
    if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
    return styleOrGroup;
};

// KO GREP
function KO() {
    if (app.layoutWindows.length == 0) return;
    var changeObject = app.documents[0];
    if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
    var doc = app.documents[0];
    var style;
    var scriptVersion = app.scriptPreferences.version;
    app.scriptPreferences.version = 15.1;
    var options = app.findChangeGrepOptions.properties;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_black]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Black"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Heavy"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_bold]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Bold"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_bolditalic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Bold", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_heavy]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Heavy"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Heavy"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_italic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Regular ", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_light]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Light"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_lightitalic]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light Italic"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Light", skew:11});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_reg]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Regular"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Regular "});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_semib1]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semi Bold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Medium"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    // Query [[KO_semib2]] -- If you delete this comment you break the update function
    try {
        app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
        app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semibold"});
        app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Medium"});
        changeObject.changeGrep();
    } catch (e) {alert(e + ' at line ' + e.line)}
    app.findChangeGrepOptions.properties = options;
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.scriptPreferences.version = scriptVersion;
};

function getStyleByString(docOrGroup, string, property) {
    if (string == '[No character style]') return docOrGroup[property][0];
    if (string == '[No paragraph style]') return docOrGroup[property][0];
    if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
    stringResult = string.match (/^(.*?[^\\]):(.*)$/);
    var styleName = (stringResult) ? stringResult[1] : string;
    styleName = styleName.replace (/\\:/g, ':');
    remainingString = (stringResult) ? stringResult[2] : '';
    var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
    var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
    if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
    return styleOrGroup;
};



dialog.show();

Solution

  • Refactored script (below) includes the following changes:

    1. All your logic for the ScriptUI dialog is now inside a createConfigDialog function.

      • In the body of the createConfigDialog function we also define a hasChosen variable, i.e.

        var hasChosen = undefined;
        
      • In the onClick event handler we assign the result of whatever the user selected to the hasChosen variable. For instance:

        Runbutton.onClick = function() {
            if (languages.selection.text === "Japanese") {
                hasChosen = "Japanese";
            }
            if (languages.selection.text === "Korean") {
                hasChosen = "Korean";
            }
            dialog.close();
        };
        
      • The createConfigDialog finally then returns an object with a hasChosen property. It's value is either Japanese or Korean. For instance:

        return {
            hasChosen: hasChosen
        }
        
    2. In the main function (IIFE) we invoke the createConfigDialog function.

      • Then conditionally invoke either the JP(), or KO() function according to the value of the hasChosen property that was returned. For instance:

        (function main() {
        
            var dialog = createConfigDialog();
        
            if (dialog.hasChosen === "Japanese") {
                JP();
                alert("Font has been applied")
            } else if (dialog.hasChosen === "Korean") {
                KO();
                alert("Font has been applied")
            }
        
        }());
        

    Complete refactored script:

    function createConfigDialog() {
    
        var dialog = new Window("dialog");
            dialog.text = "Asian font replacement";
            dialog.orientation = "column";
            dialog.alignChildren = ["center","top"];
            dialog.spacing = 10;
            dialog.margins = 16;
    
        // GROUP1
        // ======
        var group1 = dialog.add("group", undefined, {name: "group1"});
            group1.orientation = "row";
            group1.alignChildren = ["left","center"];
            group1.spacing = 10;
            group1.margins = 0;
    
        var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"});
            statictext1.text = "Select the language";
    
        var languages_array = ["-","Japanese","Simplified Chinese","Traditional Chinese","Korean","Thai"];
        var languages = group1.add("dropdownlist", undefined, undefined, {name: "languages", items: languages_array});
            languages.selection = 0;
            languages.preferredSize.width = 88;
    
        // GROUP2
        // ======
        var group2 = dialog.add("group", undefined, {name: "group2"});
            group2.orientation = "row";
            group2.alignChildren = ["left","center"];
            group2.spacing = 10;
            group2.margins = 0;
    
        var Closebutton = group2.add("button", undefined, undefined, {name: "Closebutton"});
            Closebutton.text = "Cancel";
            Closebutton.justify = "left";
            Closebutton.alignment = ["left","top"];
    
        var Runbutton = group2.add("button", undefined, undefined, {name: "Runbutton"});
            Runbutton.text = "Run";
            Runbutton.alignment = ["left","bottom"];
    
    
        var hasChosen = undefined;
    
        Runbutton.onClick = function() {
            if (languages.selection.text === "Japanese") {
                hasChosen = "Japanese";
            }
            if (languages.selection.text === "Korean") {
                hasChosen = "Korean";
            }
            dialog.close();
        };
    
        var showDialog = dialog.show();
    
        if (showDialog === 2) { // User clicked `Cancel` button.
          exit();
        }
    
        return {
            hasChosen: hasChosen
        }
    };
    
    //Japanese GREP
    
    function JP() {
        if (app.layoutWindows.length == 0) return;
        var changeObject = app.documents[0];
        if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
        var doc = app.documents[0];
        var style;
        var scriptVersion = app.scriptPreferences.version;
        app.scriptPreferences.version.constructor.name // Result:String
        var options = app.findChangeGrepOptions.properties;
        //it stops here
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
    
        // Query [[JP_black]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Black"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Heavy"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_bold]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Bold"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_bolditalic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Bold", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_heavy]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Heavy"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Heavy"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_italic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Regular ", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_light]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Light"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_lightitalic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Light", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_reg]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Regular"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Regular "});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_semib1]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semi Bold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Medium"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[JP_semib2]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semibold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1248357235, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans JP", fontStyle:"Medium"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findChangeGrepOptions.properties = options;
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        app.scriptPreferences.version = scriptVersion;
    };
    
    function getStyleByString(docOrGroup, string, property) {
        if (string == '[No character style]') return docOrGroup[property][0];
        if (string == '[No paragraph style]') return docOrGroup[property][0];
        if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
        stringResult = string.match (/^(.*?[^\\]):(.*)$/);
        var styleName = (stringResult) ? stringResult[1] : string;
        styleName = styleName.replace (/\\:/g, ':');
        remainingString = (stringResult) ? stringResult[2] : '';
        var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
        var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
        if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
        return styleOrGroup;
    };
    
    // KO GREP
    function KO() {
        if (app.layoutWindows.length == 0) return;
        var changeObject = app.documents[0];
        if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
        var doc = app.documents[0];
        var style;
        var scriptVersion = app.scriptPreferences.version;
        app.scriptPreferences.version = 15.1;
        var options = app.findChangeGrepOptions.properties;
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_black]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Black"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Heavy"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_bold]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Bold"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_bolditalic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Bold Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Bold", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_heavy]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Heavy"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Heavy"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_italic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Regular ", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_light]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Light"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_lightitalic]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Light Italic"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Light", skew:11});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_reg]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Regular"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Regular "});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_semib1]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semi Bold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Medium"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        // Query [[KO_semib2]] -- If you delete this comment you break the update function
        try {
            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
            app.findGrepPreferences.properties = ({findWhat:"[^.,;:?!-()%&\\[\\]\\d a-z|A-Z]+", fontStyle:"Semibold"});
            app.changeGrepPreferences.properties = ({kinsokuSet:1263692659, composer:"Adobe Japanese Paragraph Composer", appliedFont:"Source Han Sans KR", fontStyle:"Medium"});
            changeObject.changeGrep();
        } catch (e) {alert(e + ' at line ' + e.line)}
        app.findChangeGrepOptions.properties = options;
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        app.scriptPreferences.version = scriptVersion;
    };
    
    function getStyleByString(docOrGroup, string, property) {
        if (string == '[No character style]') return docOrGroup[property][0];
        if (string == '[No paragraph style]') return docOrGroup[property][0];
        if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
        stringResult = string.match (/^(.*?[^\\]):(.*)$/);
        var styleName = (stringResult) ? stringResult[1] : string;
        styleName = styleName.replace (/\\:/g, ':');
        remainingString = (stringResult) ? stringResult[2] : '';
        var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
        var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
        if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
        return styleOrGroup;
    };
    
    
    (function main() {
    
        var dialog = createConfigDialog();
    
        if (dialog.hasChosen === "Japanese") {
            JP();
            alert("Font has been applied")
        } else if (dialog.hasChosen === "Korean") {
            KO();
            alert("Font has been applied")
        }
    
    }());
    

    Additional notes regarding code style:

    By convention a variable name that begin with a capitalized name, such as KO and JP indicates that it is a constructor function, and should therefore be called with the new operator. As both JP and KO functions are not constructors in your code, I suggest you consider making them lowercase, e.g. jp and ko respectively (or e.g. japan and korean).

    Similarly with Runbutton and Closebutton - change instances of them to e.g. runButton and closeButton respectively, to omit the capitalized first letter.