2017-08-21 68 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功能 –