毎日モザイク

White Room Layout Works

2015-07-23T22:03:09+09:00 [Thu]

indesign 数字のみ文字種変換

全ての数字を全角にして、指定桁数の数字を半角に(0桁なら全角で統一)

全ての数字を半角にして、指定桁数の数字を全角に(0桁なら半角で統一)

選択したテキストフレームが対象。

途中。

次は選択範囲のテキストだけとかも付けたい。


//replace_digit.jsx
    
    main();
    
    function main(){
        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
        var myObjectList = new Array;
        if (app.documents.length !== 0){
            if (app.selection.length !== 0){
                for(myCounter = 0, myLength = app.selection.length; myCounter < myLength; myCounter++){
                    switch (app.selection[myCounter].constructor.name){
                        case "TextFrame":
                            myObjectList.push(app.selection[myCounter]);
                            break;
                        default:
                            break;
                    }
                }
                if (myObjectList.length !== 0){
                    myDisplayDialog(myObjectList);
                } else {
                    alert ("Please select a text frame and try again.");
                }
            } else {
                alert ("Please select an object and try again.");
            }
        } else {
            alert ("Please open a document, select an object, and try again.");
        }
    }
    
    function myDisplayDialog(myObjectList){
        var myStringList = ["0","1","2","3","4","5"]; //桁数選択ドロップダウンリスト
        myDialog = app.dialogs.add({name:"DigitRplace"});
        with(myDialog){
            with(dialogColumns.add()){
                with(borderPanels.add()){
                    var myDirectionButtons = radiobuttonGroups.add();
                    with(myDirectionButtons){
                        radiobuttonControls.add({staticLabel:"全てを全角にして指定桁数を半角にする", checkedState:true});
                        radiobuttonControls.add({staticLabel:"全てを半角にして指定桁数を全角にする"});
                    }
                }
                with(borderPanels.add()){
                    with (dialogColumns.add()){
                        staticTexts.add({staticLabel:"変更する:"});
                    }
                    with (dialogColumns.add()){
                        with (dialogRows.add()){
                            with (dialogColumns.add()){
                                staticTexts.add({staticLabel:"桁数:", minWidth:0});
                            }
                            with (dialogColumns.add()){
                                var myDigitDropdown = dropdowns.add({stringList:myStringList, selectedIndex:0});
                            }
                        }
                    }
                }
            }
        }
        var myReturn = myDialog.show();
    
        if (myReturn === true){
            var myDirectionType = myDirectionButtons.selectedButton;
            var myDigit = myStringList[myDigitDropdown.selectedIndex];
            myDialog.destroy();
            for(myCounter = 0, myLength = myObjectList.length; myCounter < myLength; myCounter++){
                myChangeCorners(myObjectList[myCounter], myDirectionType, myDigit);
            }
        } else {
            myDialog.destroy();
        }
    }
    
    function myChangeCorners(myObject, myDirectionType, myDigit){
        var zh       = myDirectionType;
        var kumi     = myDigit;
        var zen_list = '0123456789';
        var han_list = '0123456789';
        var zen_han  = {};
        var uni_fie  = {};
        var hl       = han_list.length;
        var regexUni, regexRep;
        if(zh === 0){
            regexUni = /[0-9]/g;
            regexRep = new RegExp("([^0-9])([0-9]{" + kumi + "})(?![0-9])","g");
            for(i = 0; i < hl; i++){
                zen_han[zen_list[i]] = han_list[i];
                uni_fie[han_list[i]] = zen_list[i];
            }
        } else {
            regexUni = /[0-9]/g;
            regexRep = new RegExp("([^0-9])([0-9]{" + kumi + "})(?![0-9])","g");
            for(i = 0; i < hl; i++){
                zen_han[han_list[i]] = zen_list[i];
                uni_fie[zen_list[i]] = han_list[i];
            }
        }
        var txt      = myObject.contents;
        var unified  = txt.replace(regexUni, function(){
            return(uni_fie[arguments[0]]);});
        var result   = unified.replace(regexRep, function(){
            var replist = arguments[2].split('');
            var stx     = arguments[1];
            for(i = 0, rpl = replist.length; i < rpl; i++){
                stx    += zen_han[replist[i]];
            }
            return(stx);});
        myObject.contents = result;
    }