LoginSignup
0
0

More than 1 year has passed since last update.

InDesign スクリプト 属性の値を置換(構造内の選択された)

Last updated at Posted at 2021-06-03

構造内の選択された属性の値を置換するスクリプトはこれで良いのかな・・・?

/*
構造内の選択された属性の値を置換する
更新 2021/06/08
*/

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

// スクリプト名
var scriptName = "属性の値を置換(構造内の選択された)";

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

    // ダイアログ
    var dialogueFlg = confirm("「検索と置換」の正規表現の検索文字列と置換文字列の設定でJavaScript(マルチラインモード)の正規表現で置換します。", "", scriptName);

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

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

    // 正規表現の検索プロパティのフラグ
    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(); 
    }

    // 正規表現の検索文字に正規表現の検索形式の検索文字を入れる
    findGrepText  = app.findGrepPreferences.findWhat;

    // エラーが発生した場合の処理
    try{

        // 正規表現オブジェクトを作り正規表現の検索文字に入れる
        // gでグローバルマッチ(複数一致)、mでマルチラインモード(^と$を各行の行頭、行末に一致させる為)
        findGrepText  = new RegExp(findGrepText,"gm");

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

        // 正規表現の検索文字にnullを入れる
        findGrepText = null;
    }

    // 選択されているオブジェクト記憶用
    var selectObjects = app.activeDocument.selection;

    // すべての選択を解除
    app.activeDocument.selection = null;

    // 置換数
    var replaceNumber = 0;

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

        // 選択がXml要素の場合
        if (selectObjects[i].constructor.name == "XMLAttribute") {

            // 正規表現での検索結果にXml要素の値を正規表現で検索した結果を入れる
            var findGrepResult = selectObjects[i].value.match(findGrepText);

            // 正規表現での検索結果がnullではない場合
            if(findGrepResult != null){

                // Xml要素の値にXml要素の値を正規表現で置換した結果を入れる
                selectObjects[i].value = selectObjects[i].value.replace(findGrepText,app.changeGrepPreferences.changeTo);

                // 選択に追加
                selectObjects[i].select(SelectionOptions.addTo);

                // 置換数に検索された数を追加
                replaceNumber += findGrepResult.length;
            }
        }
    }

    // 置換数が0より多い場合
    if(replaceNumber > 0){

        // 結果表示
        alert("置換された属性を選択しました" + "\n" + "置換数 " + replaceNumber,scriptName);

    // 以外の場合
    }else{

        // 選択状態を元に戻す
        app.activeDocument.selection = selectObjects;

        // 結果表示
        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