var win = new Window("dialog", "我要自學網(wǎng)案例: 大小寫轉換")
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, "(轉換后, 請務必仔細核對)")
var minBtn = btnGroup.add("button", undefined, "轉換為小寫")
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, "大小寫轉換")
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, "轉換為大寫")
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, "(轉換后, 請務必仔細核對)")
var minBtn = btnGroup.add("button", undefined, "轉換為小寫")
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, "大小寫轉換")
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, "轉換為大寫")
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)
}
}
}
承擔因您的行為而導致的法律責任,
本站有權保留或刪除有爭議評論。
參與本評論即表明您已經(jīng)閱讀并接受
上述條款。