Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to remove all the spaces from a text frame?

Engaged ,
Oct 04, 2024 Oct 04, 2024

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?

TOPICS
How-to , Scripting
289
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Oct 04, 2024 Oct 04, 2024

Salut!

Je verrais plutôt cela:

 

 

 

// JavaScript Document
var doc = app.activeDocument;
var textRef = doc.textFrames[0]; // assume text frame is present

    for (var i = 0; i < textRef.textRanges.length; i++) {
       var Char = textRef.textRanges[i];   //alert(Char.contents)
       if (Char.contents == " ") {
         textRef.textRanges[i].remove();
         i--;
       }
    }

 

 

 

René

 

 

Translate
Adobe
Advocate ,
Oct 04, 2024 Oct 04, 2024

Salut!

Je verrais plutôt cela:

 

 

 

// JavaScript Document
var doc = app.activeDocument;
var textRef = doc.textFrames[0]; // assume text frame is present

    for (var i = 0; i < textRef.textRanges.length; i++) {
       var Char = textRef.textRanges[i];   //alert(Char.contents)
       if (Char.contents == " ") {
         textRef.textRanges[i].remove();
         i--;
       }
    }

 

 

 

René

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 04, 2024 Oct 04, 2024
LATEST
great, thanks for participating
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines