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 JavaScript XML ストーリーの段落を要素に

Last updated at Posted at 2021-10-31

ストーリーの段落を要素にするスクリプトは、これで良いのかな・・・?

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

更新 2021/10/31
*/

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

// スクリプト名
var scriptName = "ストーリーの段落を要素に";

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

    // ダイアログ
    var dialogueFlg = confirm("ストーリーの段落を段落の「終わりの記号」を除いて要素にします。" + "\r\r"
    + "段落の先頭に「索引マーカー」がある場合は追加された要素にその「索引マーカー」は含まれないので注意して下さい(手抜き仕様)。" + "\r\r"
    + "ストーリーの要素を選択してスクリプトを実行して下さい。"
    ,"", scriptName);

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

    // 検索の初期化
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    
    // 検索文字列
    app.findGrepPreferences.findWhat = ".+";
    
    // 段落の最大数
    var paragraphCountMaxNumber = 0;

    if(app.activeDocument.selection.length > 0){

        // 最大の段落数を調べる
        for(var i = 0; i < app.activeDocument.selection.length; i++){
            if(app.activeDocument.selection[i].constructor.name == "XMLElement"){
                if(app.activeDocument.selection[i].xmlContent.constructor.name == "Story"){
                    if(paragraphCountMaxNumber < app.activeDocument.selection[i].xmlContent.paragraphs.count()){
                        paragraphCountMaxNumber = app.activeDocument.selection[i].xmlContent.paragraphs.count();
                     }
                 }
             }
         }

        // タグを追加する
        for(var i = 1; i <= paragraphCountMaxNumber; i++){
            if(app.activeDocument.xmlTags.itemByName("Paragraph" + i).isValid == false){
                app.activeDocument.xmlTags.add("Paragraph" + i);
            }
        }

        // 段落に要素を関連付け
        for(var i = 0; i < app.activeDocument.selection.length; i++){
            if(app.activeDocument.selection[i].constructor.name == "XMLElement"){
                if(app.activeDocument.selection[i].xmlContent.constructor.name == "Story"){
                    for(var ii = 0; ii < app.activeDocument.selection[i].xmlContent.paragraphs.count(); ii++){
                        
                        // 置換でタグを付ける
                        app.changeGrepPreferences.markupTag = app.activeDocument.xmlTags.itemByName("Paragraph" + (ii + 1));
                        app.activeDocument.selection[i].xmlContent.paragraphs.item(ii).changeGrep();
                    }
                }
            }
        }
    }

    // 検索の初期化
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

//スクリプトの動作指定の続き
}, 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?