LoginSignup
0
0

More than 1 year has passed since last update.

InDesign JavaScript 変数の値を変数名に

Last updated at Posted at 2021-11-27

変数の値を変数名にするスクリプトは、これで良いのかな・・・?

/*
このスクリプトを利用して起こった不具合の責任は取れません。
ご了承下さい。

更新 2021/11/27
*/

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

// スクリプト名
var scriptName = "変数の値を変数名に";

// 変更した数
var changeNumber = 0;

// 失敗した数
var failNumber = 0;

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

    // ダイアログ
    var dialogueFlg = confirm("activeDocumentに存在するカスタムテキスト変数の値を変数名にします。","", scriptName);

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

        // 終了
        exit();
    }

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

        // カスタムテキスト変数の場合
        if(app.activeDocument.textVariables.item(i).variableType == VariableTypes.CUSTOM_TEXT_TYPE){

            // 名前と内容が違う場合
            if(app.activeDocument.textVariables.item(i).name != app.activeDocument.textVariables.item(i).variableOptions.contents){

                // エラー対策
                try{

                    // 内容を名前にする
                    app.activeDocument.textVariables.item(i).name = app.activeDocument.textVariables.item(i).variableOptions.contents;

                    // 変更数を増やす
                    changeNumber++;

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

                    // 失敗数を増やす
                    failNumber++;
                }
            }
        }
    }

    // 結果
    alert("変更数 " + changeNumber + "\r\r" + "変更出来なかった数 "  + failNumber, 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