Copy link to clipboard
Copied
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);
}
}
}
}
Copy link to clipboard
Copied
Use the global flag for RegExp:
" abc sde ".replace(/\s\s/g, "_")
// "_abc_sde_"
" abc op sde ".replace(/\s\s/g, "_")
// "_abc_op__ sde_"
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You can do:
if ( word.contents.match(/\s\s/) ) {
Copy link to clipboard
Copied
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);
}
}
}
}
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I should have included an apology to @Silly-V for butting in.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now