Copy link to clipboard
Copied
I am pulling all of the textFrames from 2 specific layers. It is working fine. I am then pushing them into an array isolatedText[];
My regExp is matching fine. So when I try to replace the word and move it to the front of the string it isn't replacing it in the textFrame.
For example one of my search strings is:
AIR FILTER RESTRICTION SWITCH
I would like it to replace the contents to look like this:
SWITCH - AIR FILTER RESTRICTION
My alert shows it correctly....it is just not replacing the text in the text frame itself.
Here is my code....any help would be much appreciated!!!
var doc = app.activeDocument
var hyperText = doc.layers['Hypertext'].textFrames;
var compText = doc.layers['Component Table'].textFrames;
var isolatedText = [];
for (var i = 0; i < hyperText.length; i++) {
isolatedText.push(hyperText.contents);
}
for (var i = 0; i < compText.length; i++) {
isolatedText.push(compText.contents);
}
for (var ii = 0; ii < isolatedText.length; ii++){
if (isolatedText[ii].match(/SWITCH|SW(?!ING)/) != null) {
isolatedText[ii] = "SWITCH - " + isolatedText[ii].replace(/SWITCH|SW(?!ING)/g, "");
alert(isolatedText[ii]);
}
}
ah, ok. so the problem is, you're pushing the contents of text frames to your arrays, rather than pushing the textFrame objects themselves. therefore, when you try to do:
isolatedText[ii].contents = "some text"
you're attempting to change the .contents property of a string, which doesn't exist. i'm actually surprised it doesn't give an error.
try this:
...var doc = app.activeDocument
var hyperText = doc.layers['Hypertext'].textFrames;
var compText = doc.layers['Component Table'].textFrames;
var isolatedTe
Copy link to clipboard
Copied
You are grabbing the contents of the textFrames, which takes those contents and copies them into a new stand-alone string variable for you. Then when you change those string variables, it does nothing to your textFrame.contents.
So, you will need to ensure that your new changed text is applied back into the textFrame.contents of all your frames somehow.
Copy link to clipboard
Copied
Ok I follow. How do I feed my string back into the textFrame.contents?
Copy link to clipboard
Copied
textFrame.contents = "the new contents of the textFrame"
Copy link to clipboard
Copied
Yes I understand that part. I guess I don't understand how to get THAT specific textFrame since my array isn't defined as a textFrame item.
I tried this...
for (var ii = 0; ii < isolatedText.length; ii++){
if (isolatedText[ii].match(/SWITCH|SW(?!ING)/) != null) {
isolatedText[ii] = "SWITCH - " + isolatedText[ii].replace(/SWITCH|SW(?!ING)/g, "");
isolatedText[ii].contents = isolatedText[ii];
alert(isolatedText[ii]);
}
}
It still doesn't replace the contents.
Copy link to clipboard
Copied
woops. sorry i realize you already said that didn't work. let me look a little closer.
change line 19 in your example above to this:
isolatedText[ii].contents = "SWITCH - " + isolatedText[ii].replace(/SWITCH|SW(?!ING)/g, "");
Copy link to clipboard
Copied
#target illustrator-22
var doc = app.activeDocument
var hyperText = doc.layers['Hypertext'].textFrames;
var compText = doc.layers['Component Table'].textFrames;
var isolatedText = [];
for (var i = 0; i < hyperText.length; i++) {
isolatedText.push(hyperText.contents);
}
for (var i = 0; i < compText.length; i++) {
isolatedText.push(compText.contents);
}
for (var ii = 0; ii < isolatedText.length; ii++){
if (isolatedText[ii].match(/SWITCH|SW(?!ING)/) != null) {
isolatedText[ii].contents = "SWITCH - " + isolatedText[ii].replace(/SWITCH|SW(?!ING)/g, "");
}
}
Still doesn't replace the contents of the textFrame
Copy link to clipboard
Copied
ah, ok. so the problem is, you're pushing the contents of text frames to your arrays, rather than pushing the textFrame objects themselves. therefore, when you try to do:
isolatedText[ii].contents = "some text"
you're attempting to change the .contents property of a string, which doesn't exist. i'm actually surprised it doesn't give an error.
try this:
var doc = app.activeDocument
var hyperText = doc.layers['Hypertext'].textFrames;
var compText = doc.layers['Component Table'].textFrames;
var isolatedText = [];
for (var i = 0; i < hyperText.length; i++)
{
isolatedText.push(hyperText);
}
for (var i = 0; i < compText.length; i++)
{
isolatedText.push(compText);
}
for (var ii = 0; ii < isolatedText.length; ii++)
{
if (isolatedText[ii].contents.match(/SWITCH|SW(?!ING)/) != null)
{
isolatedText[ii].contents = "SWITCH - " + isolatedText[ii].contents.replace(/SWITCH|SW(?!ING)/g, "");
alert(isolatedText[ii].contents);
}
}
Copy link to clipboard
Copied
DOH! I was so close. lol!
Thank you for your help as always! That worked perfect!
Copy link to clipboard
Copied
any old time, friend. glad to help. 😃