【问题标题】:InDesign Script not working when dialog window is added添加对话框窗口时,InDesign 脚本不起作用
【发布时间】:2020-07-02 08:12:30
【问题描述】:

我正在尝试创建一个基于 GREP 的脚本,该脚本将非拉丁字符中的字体替换为兼容的字体,但是当我将其添加到对话窗口时,它不再起作用(它自己起作用)。 我已经确定了它停止运行的点,它位于这两行之间:

var options = app.findChangeGrepOptions.properties;

app.findGrepPreferences = NothingEnum.NOTHING;

调试脚本时,我收到以下错误:运行时错误:错误代码# 30486:无法处理请求,因为模式对话框或警报处于活动状态。

此时应该没有对话框或警报,当我自己运行脚本时不会发生这种情况。有人可以帮我一下吗?我真的不明白为什么会这样。 完整的脚本是这个:

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

【问题讨论】:

    标签: javascript adobe-indesign extendscript adobe-scriptui grep-indesign


    【解决方案1】:

    重构的脚本(如下)包括以下更改:

    1. ScriptUI 对话框的所有逻辑现在都在 createConfigDialog 函数中。

      • createConfigDialog 函数的主体中,我们还定义了一个hasChosen 变量,即

        var hasChosen = undefined;
        
      • onClick 事件处理程序中,我们将用户选择的任何结果分配给hasChosen 变量。例如:

        Runbutton.onClick = function() {
            if (languages.selection.text === "Japanese") {
                hasChosen = "Japanese";
            }
            if (languages.selection.text === "Korean") {
                hasChosen = "Korean";
            }
            dialog.close();
        };
        
      • createConfigDialog 最后返回一个带有hasChosen 属性的对象。它的值是JapaneseKorean。例如:

        return {
            hasChosen: hasChosen
        }
        
    2. main 函数(IIFE)中,我们调用createConfigDialog 函数。

      • 然后根据返回的hasChosen 属性的值,有条件地调用JP()KO() 函数。例如:

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

    完整的重构脚本:

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

    关于代码风格的补充说明:

    按照惯例,以大写名称开头的变量名,例如KOJP 表示它是constructor 函数,因此应使用new 运算符调用。由于 JPKO 函数都不是您代码中的构造函数,我建议您考虑将它们设为小写,例如分别为jpko(或例如japankorean)。

    RunbuttonClosebutton 类似 - 将它们的实例更改为例如runButtoncloseButton 分别省略大写的第一个字母。

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多