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



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

  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

最后一步是在脚本从对话框窗口获取值后,将这些值保存回文件中。

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

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

Voilá。现在不需要每次键入与默认值不同的值。

相关内容

  • 没有找到相关文章

最新更新