2017-08-21 67 views
0

我嘗試創建一個Adobe InDesign腳本來選擇文本,將所選文本更改爲大寫,然後複製相同的文本。 我被卡住了。Adob​​e Indesign腳本更改爲大寫並將文本複製到剪貼板

#target "InDesign" 
 

 
var myDoc = app.activeDocument; 
 

 

 
if(app.documents.length != 0){ 
 
    if(app.selection.length == 1){ 
 
     try{   
 
      //var text = app.selection[0]. 
 
      var frame1 = page.textFrames[0]; 
 
      //var frame2 = page.textFrames[1]; 
 

 
      frame1.texts.everyItem().select(); 
 
      //frame1. 
 
      app.copy(); 
 
     } 
 
     catch(e){ 
 
      alert ("Please select text", "Selection"); 
 
     } 
 
    } 
 
else{ 
 
    alert ("Please select text", "Selection"); 
 
    } 
 
} 
 
else{ 
 
\t alert("Something wrong"); 
 
}

+0

得到錯誤'抓的保持( e){ 警報(「異常發生:」+ e,「例外」);'幫助您排查發生的事情。我在'var frame1 = page.textFrames [0];' –

回答

3
var myDoc = app.activeDocument; 

if(app.documents.length != 0){ 
    if(app.selection.length == 1){ 
     try{   
      //var text = app.selection[0]. 
      //var frame1 = app.selection[0].textBoxes[0].contents; 
      var frame1 = app.documents[0].pages[0].textFrames[0]; 
      frame1.contents = frame1.contents.toUpperCase(); 
     } 
     catch(e){ 
      alert ("Exception : " + e, "Exception"); 
     } 
    } 
else{ 
    alert ("Please select text", "Selection"); 
    } 
} 
else{ 
    alert("Something wrong"); 
} 

這是使用選定的對象:

var myDoc = app.activeDocument; 

if(app.documents.length != 0){ 
    if(app.selection.length == 1){ 
     try{   
      var frame1 = app.selection[0]; 
      frame1.contents = frame1.contents.toUpperCase(); 
     } 
     catch(e){ 
      alert ("Exception : " + e, "Exception"); 
     } 
    } 
else{ 
    alert ("Please select text", "Selection"); 
    } 
} 
else{ 
    alert("Something wrong"); 
} 

複製到clipbaord:

var myDoc = app.activeDocument; 

if(app.documents.length != 0){ 
    if(app.selection.length == 1){ 
     try{   
      var selectedStuff = app.selection[0]; 

      //upperCase the selection right away. 
      //If a textFrame is selected, everything in the TextFrame gets upperCased. 
      //If only part of the text is selected, then only part of the text is upperCased. 
      selectedStuff.contents = selectedStuff.contents.toUpperCase(); 
      /////////////// 

      //app.copy(copies the selected Item, not only Text) so find out what's is selected before you shove it onto the clipboard. 
      if(selectedStuff instanceof TextFrame){ 
       //The selected item was a textFrame, a TextFrame can't be pasted into Notepad, so lets select all the text in that frame instead. 
       app.selection = selectedStuff.texts; 
       } 
      //Now copy the selection. At this point, only TEXT should be selected, so pasting should always work. 
      app.copy(); 
     } 
     catch(e){ 
      alert ("Exception : " + e, "Exception"); 
     } 
    } 
else{ 
    alert ("Please select text", "Selection"); 
    } 
} 
else{ 
    alert("Something wrong"); 
} 
+0

好了,你會介意添加代碼來複制內容到剪貼板嗎? – Kamotho

+1

'app.copy()'將所選**項目**複製到剪貼板。如果**項目**是一個文本框架,那麼textFrame將被複制到剪貼板(此**項目類型**僅爲Indesign所知,因此它只能粘貼到inDesign中)。如果所選的**項目**是textFrame內的文本,則文本將被複制到剪貼板。因爲一串文本只是一串文本,所以**項目類型**對於每一個運行的程序都是衆所周知的,因此,您所選/複製的項目可以粘貼回任何東西。我會盡快添加一些代碼。 –

+0

@KamosKamotho,增加了clipBoard功能 –