Answered
How to remove all the spaces from a text frame?
There is a text frame in a document. Whete each world is colored by a seprate color.
I try to remove all the spaces in the frame but there is an error "No such element"
on the line "var wordRange = textFrame.textRange.characters[charIndex];"
var doc = app.activeDocument;
var textFrame = doc.textFrames[0]; // assume text frame is present
var contents = textFrame.contents;
var words = contents.split(" ");
var colors = [];
var charIndex = 0;
for (var i = 0; i < words.length; i++) {
var wordLength = words[i].length;
var wordRange = textFrame.textRange.characters[charIndex];
wordRange.length = wordLength;
colors.push(wordRange.fillColor);
charIndex += wordLength + 1;
}
var newContents = contents.replace(/\s+/g, '');
var newTextFrame = doc.textFrames.add();
newTextFrame.position = textFrame.position;
newTextFrame.contents = newContents;
charIndex = 0;
for (var i = 0; i < words.length; i++) {
var wordLength = words[i].length;
var textRange = newTextFrame.textRange.characters[charIndex];
textRange.length = wordLength;
textRange.fillColor = colors[i];
charIndex += wordLength;
}How to fix it?
