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)
}
}
}
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。