0

I am try to create an Adobe InDesign script that will select text, change the selected text to Uppercase and then copy the same text. I am stuck.

#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{
    alert("Something wrong");
}
Community
  • 1
  • 1
Kamotho
  • 317
  • 1
  • 15
  • Get a hold of the error `catch(e){ alert ("Exception happened: " + e, "Exception");` to help you troubleshoot what's happening. I get "page is undefined" in `var frame1 = page.textFrames[0];` – blaze_125 Aug 21 '17 at 14:05

1 Answers1

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");
}

Here it is using the selected object:

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");
}

Copying to 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");
}
blaze_125
  • 2,262
  • 1
  • 9
  • 19
  • ok, would you mind adding codes to copy contents to the clipboard? – Kamotho Aug 21 '17 at 16:46
  • 1
    `app.copy()` will `copy` the selected **item** to the clipboard. If the **item** is a textframe, then the textFrame is copied to the clipboard(this **item type** is only known to Indesign, so it can only be pasted back into inDesign). If the selected **item** is text within the textFrame, then text is copied to the clipboard. Because a string of text is just a string of text, the **item type** is known to pretty much every single program that runs, therefor, your selected/copied item can be pasted back into pretty much anything. I'll add some code shortly. – blaze_125 Aug 21 '17 at 17:01
  • @KamosKamotho, Added clipBoard functionality – blaze_125 Aug 21 '17 at 17:36