0
0

More than 1 year has passed since last update.

InDesign JavaScript XML 要素の内容を置換

Last updated at Posted at 2021-11-08

要素の内容を置換するスクリプトは、これで良いのかな・・・?

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

更新 2021/11/08
*/

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

// スクリプト名
var scriptName = "要素の内容を置換";

//スクリプトの動作指定(一つのアンドゥ履歴にする、及び、アンドゥ名)
app.doScript(function(){

    // ダイアログ
    var dialogueFlg = confirm("選択された要素の(子供の要素を含む)内容を「検索と置換」メニューの『正規表現』の入力で置換します。" + "\r\r"
    + "正規表現の例" + "\r"
    + "ストーリーの先頭にある数字と英語の大文字と「-」の文字 (?-m)^[\\d\\u-]+"
    , "", scriptName);

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

        // スクリプトを終了
        exit();
    }

    // 置換数
    var replaceNumber = 0;

    // 正規表現の検索プロパティのフラグ
    var findGrepPreferencesPropertyFlg = false;

    // 正規表現の検索のプロパティ名の数だけ変数に入れて繰り返す
    for(var findGrepPreferencesProperty in app.findGrepPreferences.properties){

        // 正規表現の検索のプロパティ名が「parent」、「bulletChar」、「numberingRestartPolicies」では無い場合
        if(findGrepPreferencesProperty != "parent" && findGrepPreferencesProperty != "bulletChar" && findGrepPreferencesProperty != "numberingRestartPolicies"){

            // 正規表現の検索のプロパティの内容が「NothingEnum.NOTHING」、「」、「null」ではない場合
            if(app.findGrepPreferences[findGrepPreferencesProperty] != NothingEnum.NOTHING && app.findGrepPreferences[findGrepPreferencesProperty] != "" && app.findGrepPreferences[findGrepPreferencesProperty] != null){

                // 正規表現の検索プロパティのフラグにtrueを入れる
                findGrepPreferencesPropertyFlg = true;

                // 繰り返しを抜ける
                break;
            }
        }
    }

    // 正規表現の検索プロパティのフラグがfalseの場合
    if(findGrepPreferencesPropertyFlg == false){

        // 警告
        alert("「検索と置換」の正規表現の検索に値がありません。");

        // スクリプトを終了
        exit(); 
    }

    // 選択の数だけ繰り返す
    for (var i = 0; i < app.activeDocument.selection.length; i++){

        // 選択がXML要素の場合
        if (app.activeDocument.selection[i].constructor.name == "XMLElement") {

            // 正規表現で置換して置換された数を入れる
            replaceNumber += app.activeDocument.selection[i].changeGrep().length;
        }
    }

    // 結果表示
    alert("置換数 " + replaceNumber, scriptName);

// スクリプトの動作指定の続き
}, ScriptLanguage.JAVASCRIPT, [scriptName], UndoModes.ENTIRE_SCRIPT, scriptName);
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