Reputation: 317
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");
}
Upvotes: 0
Views: 2117
Reputation: 61
You're saying that you need a script that 1. "shall select some text", 2. "convert this portion of text completely to uppercase letters", 3. "copy this text to the clipboard".
Without any clue of which text shall even be selected, you won't succeed. Given that you have selected a text frame (with the Selection tool), you can grab the complete contained story using:
myText = app.selection[0].parentStory.contents;
Note that this will bring you a string.
To convert each character of the story to uppercase (if necessary), it might be more efficient to work off an array of the contained characters, like:
allChars = app.selection[0].parentStory.characters.everyItem().getElements();
for ( c = 0; c < allChars.length; ++c ) {
theChar = allChars[c];
theChar.changecase(ChangecaseMode.UPPERCASE);
}
Keep in mind that you need to process characters in tables as well, separately:
allCharsInTables = app.selection[0].parentStory.tables.everyItem().cells.everyItem().characters.everyItem().getElements();
for ( c = 0; c < allCharsInTables.length; ++c ) {
theChar = allCharsInTables[c];
theChar.changecase(ChangecaseMode.UPPERCASE);
}
In order to copy the text as text to the clipboard, you need to select the text as such (given that the text frame is selected using the Selection tool):
myText = app.selection[0].parentStory.texts[0];
app.select(myText);
app.copy();
Upvotes: 0
Reputation: 2317
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");
}
Upvotes: 3