如何使用 JavaScript 在 InDesign 中为字符加下划线?

How to underline charachters in InDesign with JavaScript?

我开始为 InDesign 编写这段代码,为除带有后代的字母外的所有字母添加下划线,并添加了一个对话框 window 来选择线条的笔划和偏移量。 现在我有两个问题:

  1. 程序为所有字母加下划线
  2. 行程和偏移不会改变

我是 Javascript 的初学者,这是第一次为 InDesign 编码。有人有线索吗?谢谢!

 // UNDERLINE ALL BUT NO DESCENDANTS



//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

if (app.documents.length != 0){
    try {
      // Run script with single undo if supported
      if (parseFloat(app.version) < 6) {
        main();
      } else {
        app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Expand State Abbreviations");
      }
      // Global error reporting
    } catch ( error ) {
      alert( error + " (Line " + error.line + " in file " + error.fileName + ")");
    }
}else{
    alert("Open a document first before running this script.");
}


///MAIN FUNCTION 
function main(){

     if(app.selection.length != 0){
        myDisplayDialog();
    }
}




//INTERFACE 
function myDisplayDialog(){
    //declare variables 
    //general 
    var myDoc = app.activeDocument;
    var mS = myDoc.selection; 
    // dialog 
    var myDialog = app.dialogs.add({name:"Underliner"});
    var myLabelWidth = 70;  
    with(myDialog.dialogColumns.add()){
        with(borderPanels.add()){
            with(dialogColumns.add()){
                with(dialogRows.add()){
                    staticTexts.add({staticLabel:"Stroke:", minWidth:myLabelWidth});
                    staticTexts.add({staticLabel:"Offset:", minWidth:myLabelWidth});
                }
            }              
                with(dialogRows.add()){
                    staticTexts.add({staticLabel:""});
                    var myStroke = measurementEditboxes.add({editValue:1, editUnits:MeasurementUnits.points});
                    var myOffset = measurementEditboxes.add({editValue: 15, editUnits:MeasurementUnits.points});
                    
                }
            
        }
    }

    var myResult = myDialog.show();
    if(myResult == true){
            var myStroke = myStroke.editValue;
            var myOffset = myOffset.editValue;
            myDialog.destroy();
            underline(mS,myStroke,myOffset);
        }
        else{
            myDialog.destroy();
            alert("Invalid page range.");
        }
 }



//REAL FUNCTION 
function underline(charList,stroke, offset){
var len = charList.length;
const doNotUnderline = ['g','j','p','q','y'];
    for (var i=0; i < len; i++){
         try{
            var myChar = charList[i];
            //console.log(typeof myText);
            if (includes(myChar, doNotUnderline) == false)
            {
                myChar.underline = true;
                myChar.underlineWeight == stroke;
                myChar.underlineOffset == offset;
                
            } else {
                myChar.underline = false;
                
            }

         }catch(r){
            alert(r.description);
            break;
        }
    }
}


//function to know if char is in array
function includes(elemento,array)
{
    var len = array.length;
    for(var i=0; i<len ;i++)
    {
        if(array[i]==elemento){return true;}
    }
    return false;
}

尝试在函数中进行这些更改 underline():

//REAL FUNCTION
function underline(words,stroke, offset) {  // <------ here 'words' instead of 'charList'
    var charList = words[0].characters;     // <------ here get 'characters' of the 'words'
    var len = charList.length;

    const doNotUnderline = ['g','j','p','q','y'].join(); // <------- here '.join()'
    for (var i=0; i < len; i++){
         try{
            var myChar = charList[i];
            // if (includes(myChar, doNotUnderline) == false) // <----- no need 
            if (doNotUnderline.indexOf(myChar.contents) < 0) // <------ 'indexOf()' instead of 'includes()' 
            {
                myChar.underline = true;
                myChar.underlineWeight = stroke; // <------- here '=' instead of '=='
                myChar.underlineOffset = offset; // <------- here '=' instead of '=='

            } else {
                myChar.underline = false;
            }

         }catch(r){
            alert(r.description);
            break;
        }
    }
}

可能还有其他改进。它需要额外的研究。但是,如果您更改这些行,它应该会在一定程度上起作用。

还有一件小事可以极大地改善用户体验:在输入字段中保留上次使用的值。它可以很容易地完成,让我知道你需要它。

更新

这是我用来存储和恢复我的脚本的任何首选项的方式。

在脚本开头的某处添加这些行:

// get preferences
var PREFS = { stroke: 1, offset: 15 };                        // set default prefs
var PREFS_FILE = File(Folder.temp + '/underline_prefs.json'); // the file with preferences
if (PREFS_FILE.exists) PREFS = $.evalFile(PREFS_FILE);        // get the prefs from the file

现在您可以在任何地方使用全局值 PREFS.strokePREFS.offset。在你的情况下,他们去这里:

with(dialogRows.add()){
    staticTexts.add({staticLabel:""});
    var myStroke = measurementEditboxes.add({editValue:PREFS.stroke, editUnits:MeasurementUnits.points});
    var myOffset = measurementEditboxes.add({editValue:PREFS.offset, editUnits:MeasurementUnits.points});
}

这样脚本将从文件 underline_prefs.json 中获取 strokeweight,这些文件将存储在当前用户的标准临时文件夹中。

最后一步是在脚本从对话框 window.

中获取值后将它们保存回文件中

我会把这段代码放在这里:

if (myResult == true) {
  var myStroke = myStroke.editValue;
  var myOffset = myOffset.editValue;
  myDialog.destroy();
  underline(mS, myStroke, myOffset);

  // save preferences here
  PREFS.stroke = myStroke;
  PREFS.offset = myOffset;
  PREFS_FILE.open('w');
  PREFS_FILE.write(PREFS.toSource());
  PREFS_FILE.close();

} else {
  myDialog.destroy();
  alert("Invalid page range.");
}

瞧瞧。现在不需要在每次与默认值不同时都键入值。