LoginSignup
0
0

More than 1 year has passed since last update.

indesign スクリプト 検索結果を属性に書き込む(構造内の選択された要素の子供の要素も含む)

Last updated at Posted at 2021-06-02

構造内の選択された要素の子供の要素も含む内容の検索結果を属性に書き込むスクリプトはこれで良いのかな・・・?

/*
構造内の選択された要素の子供の要素も含む内容の検索結果を属性に書き込む
更新 2021/6/3
*/

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

// スクリプト名
var scriptName = "検索結果を属性に書き込む(構造内の選択された要素の子供の要素も含む内容の)";

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

    // ダイアログ
    var dialogueFlg = confirm("構造内の選択された要素の子供の要素も含めて「検索と置換」の正規表現の設定で検索して検索結果を属性に書き込みます", "", 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(); 
    }

    // ページ番号を書き込む属性名
    var searchResultAttributeName = "includingChildrenSearchResult";

    // 連結文字
    var joinString = ",";

    // 書き込み数
    var writeNumber = 0;

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

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

            // 正規表現での検索結果の配列に検索結果を入れる
            var findGrepResultArray = app.activeDocument.selection[i].findGrep();

            // 正規表現での検索結果の文字列
            var findGrepResultContentsString = "";

            // 検索された場合
            if(findGrepResultArray.length > 0){

                // 正規表現での検索結果の内容の配列
                var findGrepResultContentsArray = [];

                // 正規表現での検索結果の配列の数だけ繰り返す
                for(var ii = 0; ii < findGrepResultArray.length; ii++){

                    // 正規表現での検索結果の配列にラインがある場合
                    // 制御文字(?)が検索されている場合はラインがない
                    if(findGrepResultArray[ii].lines.length > 0){

                        // 正規表現での検索結果の内容の配列に正規表現での検索結果の配列の内容を追加
                        findGrepResultContentsArray.push(findGrepResultArray[ii].contents);
                    }
                }

                // 正規表現での検索結果の内容の配列を連結文字で結合して正規表現での検索結果文字列に入れる
                findGrepResultContentsString = findGrepResultContentsArray.join(joinString);
            }

            // 検索結果を書き込む属性が存在する場合
            if (app.activeDocument.selection[i].xmlAttributes.itemByName(searchResultAttributeName).isValid == true) {

                // 検索結果を属性に書き込み
                app.activeDocument.selection[i].xmlAttributes.itemByName(searchResultAttributeName).value = findGrepResultContentsString;

            // 存在しない場合
            } else {

                // 属性を追加して検索結果を属性に書き込み
                app.activeDocument.selection[i].xmlAttributes.add(searchResultAttributeName,findGrepResultContentsString);
            }

            // 書き込み数追加
            writeNumber++;
        }
    }

    // 結果表示
    alert("書き込み数 " + writeNumber, 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