0
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-06-29

選択を条件テキストにするスクリプトは、これで良いのかな・・・?

/*
更新 2021/06/30
*/

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

// スクリプト名
var scriptName = "選択を条件テキストに";

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

    // ダイアログ
    var dialogueFlg = confirm("選択された要素に条件テキストを適用します。","", scriptName);

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

        // 終了
        exit();
    }

    /* ダイアログ処理開始 */
    // ダイアログX座標
    var dialogWidthXCoordinates = 0;

    // ダイアログY座標 
    var dialogWidthYCoordinates = 0;

    // ダイアログ幅
    var dialogWidth = 400;

    // ダイアログ高さ
    var dialogHeight = 105;

    // ダイアログ作成
    var dialogWindow = new Window("dialog", scriptName, [dialogWidthXCoordinates, dialogWidthYCoordinates, dialogWidthXCoordinates + dialogWidth, dialogWidthYCoordinates + dialogHeight]);

    // 説明文
    dialogWindow.add("statictext",[10,10,390,25],"条件テキスト:");

    // 条件テキストの名前一覧
    var conditionalTextNameList = ["[条件なし]"];

    // 条件テキストの数だけ繰り返す
    for (var i = 0; i < app.activeDocument.conditions.count(); i++) {

        // 条件テキストを収集
        conditionalTextNameList.push(app.activeDocument.conditions.item(i).name);
    }

    // 条件テキスト選択用ドロップダウンリスト作成
    var dropdownlist = dialogWindow.add("dropdownlist", [10,35,390,35], conditionalTextNameList);

    // 段落スタイル選択用ドロップダウンリストの初期選択
    dropdownlist.selection = 0;

    // ボタンの幅
    var buttonWidth = 80;

    // ボタンの高さ
    var buttonHeight = 25;

    // ボタンをウィンドウ下部中央に配置
    dialogWindow.add("button", [(dialogWidth / 2) - 10 - buttonWidth, dialogHeight - 10 - buttonHeight, (dialogWidth / 2) - 10, dialogHeight - 10], "適用", {name: "ok"});
    dialogWindow.add("button", [(dialogWidth / 2) + 10, dialogHeight - 10 - buttonHeight, (dialogWidth / 2) + 10 + buttonWidth, dialogHeight - 10], "キャンセル", {name: "cancel"});

    // ダイアログを画面のセンターに
    dialogWindow.center();

    // ダイアログを表示
    // キャンセルの場合
    if (dialogWindow.show() == 2) {

        // スクリプトを終了
        exit();
    }
    /* ダイアログ処理完了 */

    // 選択
    var selectObjects = app.activeDocument.selection;
    
    // 対象
    var targetObject;

    // 適用数
    var appliedNumber = 0;

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

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

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

            // プロパティが存在する場合
            if (selectObjects[i].hasOwnProperty("characters") == true) {
                
                // charactersがない場合
                if(selectObjects[i].characters.count() == 0){
                    
                    // insertionPointsの最後を入れる
                    targetObject = selectObjects[i].insertionPoints.lastItem();

                }else{
                    
                    // 全てのcharactersを入れる
                    targetObject = selectObjects[i].characters.itemByRange(0,-1);
                }
                
                // ドロップダウンリストの一番目ではない場合
                if(dropdownlist.selection.index != 0){

                    // 条件テキストが存在する場合
                    if(app.activeDocument.conditions.itemByName(dropdownlist.selection.text).isValid == true){

                        // 条件テキストを適用
                        targetObject.applyConditions(app.activeDocument.conditions.itemByName(dropdownlist.selection.text));
                    }

                // 以外の場合
                }else{

                    // 条件テキストを削除
                    targetObject.appliedConditions = [];
                }

                // 選択
                selectObjects[i].select(SelectionOptions.ADD_TO);

                // 数を増やす
                appliedNumber++;
            }
        }
    }

    // 適用数が0の場合
    if(appliedNumber == 0){

        // 選択を戻す
        app.activeDocument.selection = selectObjects;
    }

    // 結果表示
    alert("適用数 " + appliedNumber,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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?