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 1 year has passed since last update.

InDesign JavaScript XML 要素を追加

Posted at

要素を追加するスクリプトは、これで良いのかな・・・?

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

更新 2022/06/15
*/

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

// スクリプト名
var scriptName = "要素を追加";

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

    // ダイアログ
    var dialogueFlg = confirm("選択された要素を基準にして要素を追加します。"
    , "", scriptName);

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

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

    // ここから--------------------------------------------------
    // 設定ダイアログ 
    var dialog = new Window("dialog");
    dialog.text = scriptName;
    dialog.orientation = "column";
    dialog.alignChildren = ["left","top"];
    dialog.spacing = 10;
    dialog.margins = 16;

    // GROUP1
    var group1 = dialog.add("group", undefined, {name: "group1"});
    group1.orientation = "row";
    group1.alignChildren = ["left","center"];
    group1.spacing = 10;
    group1.margins = 0;

    group1.add("statictext", undefined, undefined).text = "選択された要素の";
    
    // 要素移動位置指定配列
    var locationOptionsArray = [LocationOptions.BEFORE, LocationOptions.AFTER, LocationOptions.AT_BEGINNING, LocationOptions.AT_END];
    
    var dropdown_position = group1.add("dropdownlist", undefined, undefined, {items: ["","","初め","終わり"]});
    dropdown_position.selection = 0;
    
    group1.add("statictext", undefined, undefined).text = "";


    // タグを取得してドロップダウンリストを追加
    var dropdown_Tag = group1.add("dropdownlist", undefined, undefined, {items: app.activeDocument.xmlTags.everyItem().name});
    dropdown_Tag.selection = 0;
    
    group1.add("statictext", undefined, undefined).text = "要素を追加";
    group1.add("statictext", undefined, undefined, {multiline: true}).text = "(ExtendScriptのメソッドの仕様で、前に追加する場合は前にある要素の直ぐ後ろに、後ろに追加する場合は、選択されている要素の直ぐ後ろに追加されます)";

    // GROUP2
    var group2 = dialog.add("group", undefined);
    group2.orientation = "row";
    group2.alignChildren = ["left","center"];
    group2.spacing = 10;
    group2.margins = 0;

    var checkbox1 = group2.add("checkbox", undefined, undefined);
    checkbox1.text = "制御文字の追加"
    checkbox1.value = true;
    
    group2.add("statictext", undefined, undefined).text = "追加された要素の";

    // 要素に文字追加位置指定配列
    var xmlElementPositionArray = [XMLElementPosition.BEFORE_ELEMENT, XMLElementPosition.AFTER_ELEMENT, XMLElementPosition.ELEMENT_START, XMLElementPosition.ELEMENT_END];
    
    var dropdown_position2 = group2.add("dropdownlist", undefined, undefined, {items: ["","","初め","終わり"]});
    dropdown_position2.selection = 0;
    
    group2.add("statictext", undefined, undefined).text = "";

    var dropdown_specialCharacter = group2.add("dropdownlist", undefined, undefined, {items: ["段落の終わり", "強制改行", "改フレーム", "右インデントタブ"]});
    dropdown_specialCharacter.selection = 0;
    
    group2.add("statictext", undefined, undefined).text = "を追加";
    group2.add("statictext", undefined, undefined, {multiline: true}).text = "(ExtendScriptのメソッドの仕様で、元々制御文字が有る場合は新たに追加されない場合が有ります)";
    
    // GROUP3
    var group3 = dialog.add("group", undefined);
    group3.preferredSize.width = 700;
    group3.orientation = "row";
    group3.alignChildren = ["center","center"];
    group3.spacing = 30;
    group3.margins = 0;

    var button1 = group3.add("button", undefined, undefined);
    button1.text = "OK";

    var button2 = group3.add("button", undefined, undefined);
    button2.text = "Cancel";

    var buttonResult = dialog.show();
    // ここまで--------------------------------------------------
    
    // Cancelの場合
    if(buttonResult == 2){

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

    // 回数
    var numberOfTimes = 0;

    // 対象
    var targetObject;
    
    // 特殊文字指定配列
    var specialCharacterArray = ["\r", SpecialCharacters.FORCED_LINE_BREAK, SpecialCharacters.FRAME_BREAK, SpecialCharacters.RIGHT_INDENT_TAB];

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

        // 選択がXML要素の場合
        if(app.activeDocument.selection[i].constructor.name == "XMLElement"){
 
            // 親がXML要素の場合
            if(app.activeDocument.selection[i].parent.constructor.name == "XMLElement"){
                
                // 要素を追加
                targetObject = app.activeDocument.selection[i].parent.xmlElements.add(app.activeDocument.xmlTags.itemByName(dropdown_Tag.selection.text));

                // 移動(戻値を入れなおす事)
                targetObject = targetObject.move(locationOptionsArray[dropdown_position.selection.index], app.activeDocument.selection[i]);
                
                // チェックボックスのフラグがtrueの場合
                if(checkbox1.value == true){
                    
                    // 改行等を追加
                    targetObject.insertTextAsContent(specialCharacterArray[dropdown_specialCharacter.selection.index], xmlElementPositionArray[dropdown_position2.selection.index]);
                }

                // 回数を増やす
                numberOfTimes++;
            }
        }
    }

    // 結果表示
    alert("追加数 " + numberOfTimes, 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?