Blue Moose
Blue Moose

Reputation: 125

grab the count of selected textframe indesign

I have a text frame that I want to run some code on. This code will remove the double spaces... as a designer, I need clean text, not how a writer would have double spaces after a period. I have a code that goes through every frame on the current document. what I do need is to select only certain text frames to run that code.

here is what I have.

var myDoc = app.activeDocument;
var textFrame = myDoc.textFrames.selection[0];

 //make sure selected text frame
if(!(app.selection[0] instanceof TextFrame)) {
    alert("Please select a text frame");
    exit();
} else {
for(var i = 0; i < textFrame.length; i++) {
    var frameCount = textFrame[i];
}

alert("text frame amount:" + frameCount);

I am sure this is an easy fix. I just cant figure what I am doing wrong here.

Any ideas?

Upvotes: 0

Views: 387

Answers (1)

mdomino
mdomino

Reputation: 1235

If you want to allow a selection of multiple text frames, you first need to cycle through the selection to collect all selected items which are actually text frames and then in a second step loop over all these collected text frames.

This should do, what you need:

var sel = app.selection;
var selectedTextFrames = [];

for (var i = 0; i < sel.length; i++) {
  if(sel[i] instanceof TextFrame) {
    selectedTextFrames.push(sel[i])
  }
}

if(!selectedTextFrames.length) {
  alert("Please select one or multiple text frames");
  exit();
}

for (var i = 0; i < selectedTextFrames.length; i++) {
  // do something with each selected text frame
}

alert(i + " text frame(s) have been processed.");

Upvotes: 2

Related Questions