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

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

var selection = app.activeDocument.selection

var btnGroup = win.add("group")

if (selection.typename == "TextRange") {
    desc.add("statictext", undefined, "當(dāng)前文本范圍里共有 " + selection.parent.length + " 個(gè)文字")
    desc.add("statictext", undefined, "選中了 " + selection.length + " 個(gè)文字")
    desc.add("statictext", undefined, "(轉(zhuǎn)換后, 請(qǐng)務(wù)必仔細(xì)核對(duì))")

    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(
                    "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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(
                    "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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(
                    "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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, "當(dāng)前選擇集合里共有 " + textArr.length + " 段文本對(duì)象")
    desc.add("statictext", undefined, "(轉(zhuǎn)換后, 請(qǐng)務(wù)必仔細(xì)核對(duì))")

    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(
                        "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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(
                        "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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(
                        "提示: 當(dāng)前第 " + (i+1) + "個(gè)文字: " + selection.characters[i].contents + " 設(shè)置了字符->大寫字母效果, 跳過(guò)這個(gè)文字的執(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腳本開(kāi)發(fā)教程
我要自學(xué)網(wǎng)商城 ¥70 元
進(jìn)入購(gòu)買
文章評(píng)論
0 條評(píng)論 按熱度排序 按時(shí)間排序 /350
添加表情
遵守中華人民共和國(guó)的各項(xiàng)道德法規(guī),
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
分類選擇:
電腦辦公 平面設(shè)計(jì) 室內(nèi)設(shè)計(jì) 室外設(shè)計(jì) 機(jī)械設(shè)計(jì) 工業(yè)自動(dòng)化 影視動(dòng)畫 程序開(kāi)發(fā) 網(wǎng)頁(yè)設(shè)計(jì) 會(huì)計(jì)課程 興趣成長(zhǎng) AIGC