LoginSignup
0
0

More than 1 year has passed since last update.

InDesign JavaScript XML 要素に文字を挿入もしくは要素を文字に変換

Last updated at Posted at 2021-11-25

要素に文字を挿入もしくは要素を文字に変換するスクリプトは、これで良いのかな・・・?

/*
このスクリプトを利用して起こった不具合の責任は取れません。
ご了承下さい。

更新 2021/11/25
*/

// アプリ指定
#target "indesign";

// スクリプト名
var scriptName = "要素に文字を挿入もしくは要素を文字に変換";

// 挿入対象の要素
var afterElementInsertTextAsContentElementArray = ["p"];

// 挿入文字
var afterElementInsertTextAsContentArray = ["\r"];

// 挿入文字の読み
var afterElementInsertTextAsContentReadingArray = ["段落の終わり"];

// 変換対象の要素
var escapeSpecialCharacterElementArray = ["CR","LF"];

// 変換文字
var escapeSpecialCharacterArray = ["\r","\n"];

// 変換文字の読み
var escapeSpecialCharacterReadingArray = ["段落の終わり","強制改行"];

// ダイアログのテキスト
var daialogTextArray = [];

/* 文字を入れる */
daialogTextArray.push("要素の後が「Storyの終わり」もしくは「段落の終わり」の場合は「段落の終わり」は挿入されません。" + "\r");

for(var i = 0; i < afterElementInsertTextAsContentElementArray.length; i++){

    daialogTextArray.push("" + afterElementInsertTextAsContentElementArray[i] +"」要素の後に「" +afterElementInsertTextAsContentReadingArray[i] +"」を挿入します。");
}

daialogTextArray.push("");

for(var i = 0; i < escapeSpecialCharacterElementArray.length; i++){

    daialogTextArray.push("" + escapeSpecialCharacterElementArray[i] +"」要素を「" +escapeSpecialCharacterReadingArray[i] +"」に変換します。");
}
/* 文字を入れる */

// ダイアログ
var dialogueFlg = confirm(daialogTextArray.join("\r"),"", scriptName);

// Noの場合
if(dialogueFlg == false){

    // 終了
    exit();
}

// エラー処理
try {

    // glue code.jsxを取り込み
    $.evalFile(app.filePath + "/Scripts/XML Rules/glue code.jsx");

// エラーの場合
}catch(e){

    // 警告
    alert("「glue code.jsx」が見つかりませんでした。");

    // 終了
    exit();
}

/* ルールセット */
var myRuleSet = [];

for(var i = 0; i < afterElementInsertTextAsContentElementArray.length; i++){

    myRuleSet.push(new afterElementInsertTextAsContent("//" + afterElementInsertTextAsContentElementArray[i], afterElementInsertTextAsContentArray[i]));
}

for(var i = 0; i < escapeSpecialCharacterElementArray.length; i++){

    myRuleSet.push(new escapeSpecialCharacters("//" + escapeSpecialCharacterElementArray[i], escapeSpecialCharacterArray[i]));
}
/* ルールセット */

// スクリプトを実行
app.doScript(function(){

    // 処理するxmlRule
    __processRuleSet(app.activeDocument.xmlElements.firstItem(),myRuleSet);

// 一つのアンドゥ履歴にする
}, ScriptLanguage.JAVASCRIPT,[], UndoModes.ENTIRE_SCRIPT, scriptName);
/* スクリプトを実行 */

// 要素の後に文字を挿入するXmlRule
function afterElementInsertTextAsContent(myXpath, myContents){
    this.name = "afterElementInsertTextAsContent" ;
    this.xpath = myXpath;
    this.apply = function(myElement, myRuleProcessor){

        // 要素に挿入
        myElement.insertTextAsContent(myContents, XMLElementPosition.AFTER_ELEMENT);
        return true;
    }
}

// 要素を文字に変えるXmlRule
function escapeSpecialCharacters(myXpath, myContents){
    this.name = "escapeSpecialCharacters" ;
    this.xpath = myXpath;
    this.apply = function(myElement, myRuleProcessor){

        // 要素の内容に入れる
        myElement.contents = myContents;

        // 子要素をスキップ
        __skipChildren(myRuleProcessor);

        // タグを外す
        myElement.untag();
        return true;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0