Illustrator腳本開發(fā)大小寫轉(zhuǎn)換
發(fā)布時間:2024-11-08 11:55 [ 我要自學網(wǎng)原創(chuàng) ] 發(fā)布人: 快樂小女 閱讀: 160

var win = new Window("dialog", "我要自學網(wǎng)案例: 大小寫轉(zhuǎn)換")
var desc = win.add("panel", undefined, "說明")
desc.margins = 20 // 邊距

var selection = app.activeDocument.selection

var btnGroup = win.add("group")

if (selection.typename == "TextRange") {
    desc.add("statictext", undefined, "當前文本范圍里共有 " + selection.parent.length + " 個文字")
    desc.add("statictext", undefined, "選中了 " + selection.length + " 個文字")
    desc.add("statictext", undefined, "(轉(zhuǎn)換后, 請務必仔細核對)")

    var minBtn = btnGroup.add("button", undefined, "轉(zhuǎn)換為小寫")
    minBtn.onClick = function(){
        for(var i=0; i<selection.characters.length; i++){

            if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                alert(
                    "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                )
                continue
            }

            var attr = selection.characters[i].characterAttributes

            selection.characters[i].contents = selection.characters[i].contents.toLowerCase()
          
            selection.characters[i].characterAttributes = attr
        }  

        win.close()
    }

    var goBtn = btnGroup.add("button", undefined, "大小寫轉(zhuǎn)換")
    goBtn.onClick = function(){
        for(var i=0; i<selection.characters.length; i++){

            if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                alert(
                    "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                )
                continue
            }

            var attr = selection.characters[i].characterAttributes

            selection.characters[i].contents = toggleCase(selection.characters[i].contents)
          
            selection.characters[i].characterAttributes = attr
        }  

        win.close()
    }

    var maxBtn = btnGroup.add("button", undefined, "轉(zhuǎn)換為大寫")
    maxBtn.onClick = function(){
        for(var i=0; i<selection.characters.length; i++){

            if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                alert(
                    "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                )
                continue
            }

            var attr = selection.characters[i].characterAttributes

            selection.characters[i].contents = selection.characters[i].contents.toUpperCase()
          
            selection.characters[i].characterAttributes = attr
        }  

        win.close()
    }

} else {
    var textArr = []

    for(var i =0; i<selection.length; i++){
        getTextFrameByEl(selection[i], textArr)
    }

    desc.add("statictext", undefined, "當前選擇集合里共有 " + textArr.length + " 段文本對象")
    desc.add("statictext", undefined, "(轉(zhuǎn)換后, 請務必仔細核對)")

    var minBtn = btnGroup.add("button", undefined, "轉(zhuǎn)換為小寫")
    minBtn.onClick = function(){
        for(var j=0; j<textArr.length; j++){
            var selection = textArr[j]

            for(var i=0; i<selection.characters.length; i++){

                if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                    alert(
                        "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                    )
                    continue
                }
   
                var attr = selection.characters[i].characterAttributes
   
                selection.characters[i].contents = selection.characters[i].contents.toLowerCase()
              
                selection.characters[i].characterAttributes = attr
            }  

        }
        win.close()
    }

    var goBtn = btnGroup.add("button", undefined, "大小寫轉(zhuǎn)換")
    goBtn.onClick = function(){
        for(var j=0; j<textArr.length; j++){
            var selection = textArr[j]

            for(var i=0; i<selection.characters.length; i++){

                if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                    alert(
                        "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                    )
                    continue
                }
   
                var attr = selection.characters[i].characterAttributes
   
                selection.characters[i].contents = toggleCase(selection.characters[i].contents)
              
                selection.characters[i].characterAttributes = attr
            }  

        }
        win.close()
    }

    var maxBtn = btnGroup.add("button", undefined, "轉(zhuǎn)換為大寫")
    maxBtn.onClick = function(){
        for(var j=0; j<textArr.length; j++){
            var selection = textArr[j]

            for(var i=0; i<selection.characters.length; i++){

                if(selection.characters[i].characterAttributes.capitalization !== FontCapsOption.NORMALCAPS){
                    alert(
                        "提示: 當前第 " + (i+1) + "個文字: " + selection.characters[i].contents + " 設置了字符->大寫字母效果, 跳過這個文字的執(zhí)行"
                    )
                    continue
                }
   
                var attr = selection.characters[i].characterAttributes
   
                selection.characters[i].contents = selection.characters[i].contents.toUpperCase()
              
                selection.characters[i].characterAttributes = attr
            }  

        }
        win.close()
    }

}


win.show()

function toggleCase(str){
    var newStr = ""
   
    for(var i = 0; i<str.length; i++){
        newStr += str[i].toUpperCase() == str[i] ? str[i].toLowerCase() : str[i].toUpperCase()
   
    }

    return newStr
}

function getTextFrameByEl(el, arr){
    if(el.typename == "TextFrame"){
        arr.push(el)
    }

    if(el.typename == "GroupItem"){
        for(var i=0; i<el.pageItems.length; i++){
            getTextFrameByEl(el.pageItems[i], arr)
        }
    }

}

Illustrator腳本開發(fā)教程
我要自學網(wǎng)商城 ¥70 元
進入購買
文章評論
0 條評論 按熱度排序 按時間排序 /350
添加表情
遵守中華人民共和國的各項道德法規(guī),
承擔因您的行為而導致的法律責任,
本站有權(quán)保留或刪除有爭議評論。
參與本評論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
分類選擇:
電腦辦公 平面設計 室內(nèi)設計 室外設計 機械設計 工業(yè)自動化 影視動畫 程序開發(fā) 網(wǎng)頁設計 會計課程 興趣成長 AIGC