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

•  Script that looks for 'double white space' in bodytext…

Enthusiast ,
Sep 02, 2021 Sep 02, 2021

Hello -

I am trying to code a small script that will find the double white spaces that are present in all the texts of an AI document.
NB. Double spaces or even more.

The script does not return any error but no result either.
If I replace the double spaces by a string of some characters it works;

 

Could someone help me?

 

 

 

// Check for 'double white space' in bodytext

if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) { 
	searchWord = "  ";  //  /\s\s/
	for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
		textArt = activeDocument.textFrames[i];
		for (j = 0; j < textArt.words.length; j++) {
			word = textArt.words[j];
			if ( word.contents == searchWord ) {
				alert("YES: "+j);
			}
		}
	}
}

 

 

TOPICS
Scripting
995
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
Adobe
Valorous Hero ,
Sep 02, 2021 Sep 02, 2021

Use the global flag for RegExp:

 

"  abc  sde  ".replace(/\s\s/g, "_")
// "_abc_sde_"
"  abc  op     sde  ".replace(/\s\s/g, "_")
// "_abc_op__ sde_"
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
Enthusiast ,
Sep 02, 2021 Sep 02, 2021

Hey Silly-V -

 

Thanks for that very quick answer 🙂

Now I don't want to "replace", just know if there are "double spacing" (or even more) in  textfield.

Any idea? Thx

 

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
Valorous Hero ,
Sep 02, 2021 Sep 02, 2021

You can do:

if ( word.contents.match(/\s\s/) ) {
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
Enthusiast ,
Sep 02, 2021 Sep 02, 2021

Hello again Silly-V -

 

I adapted the script, bt nothing happen 😞

Again, I sure that I have double spacing or even triple spacing in my text. 

 

// Check for 'double white space' in bodytext

if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) { 
	// searchWord = "  ";
	for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
		textArt = activeDocument.textFrames[i];
		for (j = 0; j < textArt.words.length; j++) {
			word = textArt.words[j];
			if ( word.contents.match(/\s\s/) ) {
			// if ( word.contents.match(/\s\s/) !== null) {
			// if ( word.contents == searchWord ) {
				alert("YES: "+j);
			}
		}
	}
}

 

 

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
Valorous Hero ,
Sep 02, 2021 Sep 02, 2021

OH yea that's cause you are using words. The words property items don't have spaces, because well, words don't have spaces.

Use textFrame.contents to just work on the entire string that still has spaces (the raw text essentially).

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
Enthusiast ,
Sep 02, 2021 Sep 02, 2021

Hey Silly-V -

 

I replaced as suggested the ".words" with "textFrame.contents".

But sems I'm not there yet  😛

Since it doesn't return the number of "double spacing"

 

 

if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) { 
	var counter = 0;
	for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
		textArt = activeDocument.textFrames[i];
		// for (j = 0; j < textArt.words.length; j++) {
			word = textArt.contents;
			if ( word.match(/\s\s/) ) {
				counter++;
			// if ( word.match(/\s\s/) !== null) {
			}
		}
	}
	alert(counter);  // I was expected to see the number of double spacing
}

 

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
Guide ,
Sep 02, 2021 Sep 02, 2021

 

if (app.documents.length > 0 && app.activeDocument.textFrames.length > 0) { 
    var counter = 0;
    for (var i = 0; i < app.activeDocument.textFrames.length; i++ ) {
        var textArt = activeDocument.textFrames[i];
        var word = textArt.contents;
        var x = word.match(/\s\s/g);  // x is array of matches
        counter += x.length;
    }
}
alert(counter);

 

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
Enthusiast ,
Sep 02, 2021 Sep 02, 2021

Hello -

 

Thanks Silly-V

Thanks a lot femkeblanco.

 

This works!

 

Next step… I will try to embed this part of this script into a bigger script.

Thank you again, both enjoy your day

 

 

- Dimitri

 

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
Guide ,
Sep 03, 2021 Sep 03, 2021

I should have included an apology to @Silly-V for butting in.

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
Community Expert ,
Sep 03, 2021 Sep 03, 2021

I have only skimmed your topic. Only as a sidenote:

Depending on what you really want, the wildcard \s is not a good idea!

 

The whitespace special character \s matches any of the specific whitespaces. For example the "normal" space (␣), the tab (\t), the new line (\n), and the carriage return (\r) and also any other kind of white spaces.

 

If that's not your real goal, you should limit the search to the specific space character. And instead of searching for two spaces, search for "two or more" spaces.

 

Replace this line:

var x = word.match(/\s\s/g);  // x is array of matches

 

with this:

var x = word.match(/\u0020{2,}/g);  // x is array of matches

 

And don't use "word" as the variable name for the contents of the text frame. This will extremely confuse you and anyone who wants to read the script.

 

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
Community Expert ,
Oct 08, 2021 Oct 08, 2021
LATEST

Hi @dimitri_cas 

the worst feedback is no feedback at all ...

 

Did you have success?

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