LoginSignup
0
0

More than 1 year has passed since last update.

InDesign JavaScript XML 選択オブジェクトを兄弟関係の構造に

Last updated at Posted at 2022-06-17

選択オブジェクトを兄弟関係の構造にするスクリプトは、これで良いのかな・・・?

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

更新 2022/06/17
*/

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

// スクリプト名
var scriptName = "選択オブジェクトを兄弟関係の構造に";

// メモ:doScriptをすると元に戻したときおかしくなる事がある。

// 参照要素配列
var associatedXMLElementArray = [];
    
// タグ付きプリセットオプションの画像のタグがImageで無い場合
if(app.activeDocument.xmlPreferences.defaultImageTagName != "Image"){

    // Imageに
    app.activeDocument.xmlPreferences.defaultImageTagName = "Image";
}

// タグ付きプリセットオプションのテキストフレームのタグがStoryで無い場合
if(app.activeDocument.xmlPreferences.defaultStoryTagName != "Story"){

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

    // autoTagメソッドが有る場合
    if(app.activeDocument.selection[i].hasOwnProperty("autoTag")){

        // 参照する要素が無い場合
        if(app.activeDocument.selection[i].associatedXMLElement == null){

            // 自動タグ
            app.activeDocument.selection[i].autoTag();
        }   
    }
    
    // 追加
    associatedXMLElementArray.push(app.activeDocument.selection[i].associatedXMLElement);
}

// ルートに要素を追加
var parentElement = app.activeDocument.xmlElements.firstItem().xmlElements.add("Information");

// 参照要素の数だけ繰り返す
for(var i = 0; i < associatedXMLElementArray.length; i++){

    // 参照要素の名前がImageの場合
    if(associatedXMLElementArray[i].markupTag.name == "Image"){

        // 「href」名の属性が有る場合
        if(associatedXMLElementArray[i].xmlAttributes.itemByName("href").isValid == true){

            // 画像がファイル名を検索
            associatedXMLElementArray[i].xmlAttributes.itemByName("href").value.match(/([^\/]+)(\.[a-zA-Z]+$)/g);

            // 一つ目の検索結果が有る場合
            if(RegExp.$1 != null){

                // 「fileName」名の属性が有る場合
                if(associatedXMLElementArray[i].xmlAttributes.itemByName("fileName").isValid == true){

                    // 一つ目の検索結果を属性の値に入れる
                    associatedXMLElementArray[i].xmlAttributes.itemByName("fileName").value = RegExp.$1;

                // 無い場合
                }else{

                    // 「fileName」名の属性と値を追加
                    associatedXMLElementArray[i].xmlAttributes.add("fileName", RegExp.$1);
                }
            }
        }

        // 追加した要素の子(終わり)に移動
        associatedXMLElementArray[i].move(LocationOptions.AT_END, parentElement);

    // 以外の場合
    }else{

        // 追加した要素の子(初め)に移動
        associatedXMLElementArray[i].move(LocationOptions.AT_BEGINNING, parentElement);
    }
}
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