LoginSignup
0
0

More than 1 year has passed since last update.

InDesign JavaScript XML 絶対XPathを取得

Last updated at Posted at 2022-02-21

絶対XPathを取得するスクリプトは、これで良いのかな・・・?

動作動画:https://youtu.be/kBgB9fRsYhs

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

更新 2022/02/22
*/

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

// スクリプト名
var scriptName = "絶対XPathを取得";

// スクリプトを実行
app.doScript(function(){
    
    // メッセージ
    var dialogueFlg = confirm("選択された要素の絶対XPathを属性に書き込みます。", "", scriptName);

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

        // 終了
        exit();
    }

    // 即時関数
    var palette = (function(){
        
        // パレットを作成
        var myPalette = new Window("palette","進行状況");
    
        // 間隔
        myPalette.spacing = 5;
        
        // 余白
        myPalette.margins = 10; 

        // 文字
        myPalette.add("statictext", [0, 0, 570, 15], "", {name: "statictext1"});

        // 表示
        myPalette.show();
        
        // パレットオブジェクトを戻す
        return myPalette;
    }());

    // 絶対XPath
    var absoluteXPath = "";
    
    // 結果数
    var resultNumber = 0;
    
    // 選択数
    var selectionLength = app.activeDocument.selection.length;
        
    // 選択の数だけ繰り返す
    for(var i = 0; i < selectionLength; i++){
            
        // 選択が要素の場合
        if(app.activeDocument.selection[i].constructor.name == "XMLElement"){
            
            // 絶対XPathを取得
            absoluteXPath = getAbsoluteXPath(app.activeDocument.selection[i],"");
            
            // 指定する属性が存在する場合
            if(app.activeDocument.selection[i].xmlAttributes.itemByName("absoluteXPath").isValid == true){
                
                // 属性の値に絶対XPathを入れる
                app.activeDocument.selection[i].xmlAttributes.itemByName("absoluteXPath").value = absoluteXPath;
            
            // 以外の場合
            }else{
                
                // 属性を追加
                app.activeDocument.selection[i].xmlAttributes.add("absoluteXPath", absoluteXPath);
            }
        
            // 増やす
            resultNumber++;
            
            // 進行状況テキスト
            palette.statictext1.text = resultNumber + "/" + selectionLength + "" + absoluteXPath;
                
        // 以外の場合
        }else{
                
            // 繰り返しを抜ける
            break;
        }
    }

    // 結果
    alert("絶対XPath取得数:" + resultNumber);

// 一つのアンドゥ履歴にする
}, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, scriptName);

// 絶対XPathを取得(対象の要素、絶対XPath)
function getAbsoluteXPath(targetElement,absoluteXPath){
    
    // 要素名の数
    var elementNameCount = 0;
    
    // 選択の親が要素の場合
    if(targetElement.parent.constructor.name == "XMLElement"){

        // 対象のindexまで繰り返す
        for(var i = 0; i <=  targetElement.index; i++){
                
            // 要素名が同じ場合
            if(targetElement.parent.xmlElements.item(i).markupTag.name == targetElement.markupTag.name){
                    
                // 増やす
                elementNameCount++;
            }
        }
    
        // 整形
        absoluteXPath = "/" + targetElement.markupTag.name + "[" + elementNameCount + "]" + absoluteXPath ;
                        
        // 再帰処理
        absoluteXPath = getAbsoluteXPath(targetElement.parent,absoluteXPath);
    
    // 以外の場合
    }else{
        
        // Rootの要素を追加
        absoluteXPath = "/" + targetElement.markupTag.name  + absoluteXPath ;
    }

    // 絶対XPathを返す
    return absoluteXPath;
}
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