0
0

More than 3 years have passed since last update.

InDesign スクリプト 要素を配置(作成したフレームに)

Last updated at Posted at 2021-06-06

作成したフレームに要素を配置するスクリプトは、これで良いのかな・・・?

/*
作成したフレームに要素を配置
更新 2021/06/12

構造に読み込むXMLの例
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<Root>
<!--文字コードに注意-->
<猫の名前 placePageNumber='1'>こはく</猫の名前>
<猫の名前 placePageNumber='2'>ぼんた</猫の名前>
<猫の名前 placePageNumber='3'>くろい</猫の名前>
</Root>
*/

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

// スクリプト名
var scriptName = "要素を配置(作成したフレームに)";

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

    //  配置ページ番号を指定する属性の属性名
    var placePageNumberAssignAttributeName ="placePageNumber";

    // ダイアログ
    var dialogueFlg = confirm("構造の選択された要素を作成したフレームに配置します。" + "\r\r" + "ページを指定するのに" + placePageNumberAssignAttributeName + "名の属性とその値(ページ番号)を使用します。", "", scriptName);

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

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

    // 配置ページ番号
    var placePageNumber;

    // 配置の位置と大きさ
    var placeBounds = [0,0,0,0];

    // 配置数
    var placeNumber = 0;

    // 選択を入れる
    selectObjects = app.activeDocument.selection;

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

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

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

            // ストーリーのコンストラクタ名を取得する関数を使い結果がXmlストーリーの場合
            if(getStoryConstructorName(selectObjects[i]) == "XmlStory"){ 

                // placePageNumberAssignAttributeName名の属性がある場合
                if(selectObjects[i].xmlAttributes.itemByName(placePageNumberAssignAttributeName).isValid == true){

                    // 属性の値が正数の場合
                    if(selectObjects[i].xmlAttributes.itemByName(placePageNumberAssignAttributeName).value.match(/^[1-9][0-9]*$/) != null){

                        // 属性の値を入れる
                        placePageNumber = selectObjects[i].xmlAttributes.itemByName(placePageNumberAssignAttributeName).value;

                        // ページの数だけ繰り返す
                        for(var ii = 0; ii < app.activeDocument.pages.length; ii++){

                            // 配置ページ番号のページが存在する場合
                            if(app.activeDocument.pages[ii].name == placePageNumber){

                                // ページの大きさを半分にして入れる
                                placeBounds[0] = app.activeDocument.pages[ii].bounds[0];
                                placeBounds[1] = app.activeDocument.pages[ii].bounds[1];
                                placeBounds[2] = app.activeDocument.pages[ii].bounds[0] + (app.activeDocument.pages[ii].bounds[2] - app.activeDocument.pages[ii].bounds[0]) / 2;
                                placeBounds[3] = app.activeDocument.pages[ii].bounds[1] + (app.activeDocument.pages[ii].bounds[3] - app.activeDocument.pages[ii].bounds[1]) / 2;

                                // テキストフレーム配置
                                selectObjects[i].placeIntoFrame(app.activeDocument.pages[ii],placeBounds);

                                // 配置数を追加
                                placeNumber++;

                                // 繰り返しから抜ける
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    // 結果表示
    alert("選択数 " + selectObjects.length + "\r" + "配置数 " + placeNumber, scriptName);

//スクリプト動作指定(一つのアンドゥ履歴にする、及び、アンドゥ名の指定)の続き
}, ScriptLanguage.JAVASCRIPT, [scriptName], UndoModes.ENTIRE_SCRIPT, scriptName);

/* ストーリーのコンストラクタ名を取得する関数、引数(オブジェクト)の宣言 */
function getStoryConstructorName(anyObject) {

    // parentStoryプロパティが存在する場合
    if (anyObject.hasOwnProperty("parentStory") == true) {

        // エラー処理
        try{

            // parentStoryの値がある場合
            if (anyObject.parentStory) {

                // コンストラクタ名を戻す
                return anyObject.parentStory.constructor.name;
            }

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

            // 抜ける
            return;
        }

    // Applicationの場合
    } else if (anyObject.constructor.name == "Application") {

        // 抜ける
        return;
    }

    // オブジェクトの階層を一つ上げて再帰関数
    return getStoryConstructorName(anyObject.parent);
}
/* ストーリーのコンストラクタ名を取得する関数の宣言終了 */
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