1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

indesign スクリプト 構造内の要素を選択(要素名から)

Last updated at Posted at 2021-04-29

構造内の要素名から要素を選択するスクリプトはこれで良いのかな・・・?

/*
 構造内の要素名から要素を選択
 更新 2021/6/03
*/

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

// スクリプト名
var scriptName = "構造内の要素を選択(要素名から)";

// 検索文字を入力
var findGrepText = prompt("構造内の要素名(JavaScriptの正規表現)\n(選択された構造内の要素がある場合はその中から検索)","",scriptName);

// キャンセルされた時の処理
if(findGrepText == null){

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

// エラーが発生した場合の処理
try{
        
    // 正規表現オブジェクトを作り検索文字に入れる
    findGrepText  = new RegExp(findGrepText,"g");
        
// エラーの場合
}catch(e){

    // 検索文字にnullを入れる
    findGrepText = null;
}

// 選択されているオブジェクト記憶用
var selectObjects = [];

// 選択が存在しない場合
if(app.activeDocument.selection.length == 0){

    // 構造内のすべての要素を選択する関数を使用
    selectObjects = selectAllElements(app.activeDocument.xmlElements,selectObjects);

// 選択が存在する場合
}else{

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

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

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

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

        // タグ名を検索して引っかかった場合
        if(selectObjects[i].markupTag.name.match(findGrepText)){

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

// 結果表示
alert("選択数 " + app.activeDocument.selection.length,scriptName);

/* 構造内の全ての要素を選択する関数、引数(xmlElements,再帰的処理の為の選択を記憶する配列) */
function selectAllElements(hierarchyElements,selectObjects){

    // 要素の数だけ繰り返す
    for (var i = 0; i < hierarchyElements.length; i++) {

        // 要素に子要素が存在するか
        if(hierarchyElements[i].xmlElements.length > 0){

            // 存在する場合再帰的処理
            selectAllElements(hierarchyElements[i].xmlElements,selectObjects);
        }

        // 要素を選択を記憶する為の配列に追加
        selectObjects.push(hierarchyElements[i]);
    }

    // 選択する為のオブジェクトの値を戻す
    return selectObjects;
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?