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

Add to and replace contents of an array

Engaged ,
Mar 13, 2018 Mar 13, 2018

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]);

}

}

TOPICS
Scripting

Views

1.1K

Translate

Translate

Report

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

Community Expert , Mar 13, 2018 Mar 13, 2018

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

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Mar 13, 2018 Mar 13, 2018

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.

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

Ok I follow. How do I feed my string back into the textFrame.contents?

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

textFrame.contents = "the new contents of the textFrame"

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

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.

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

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, "");

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

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

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

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);

    }

}

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

DOH! I was so close. lol!

Thank you for your help as always! That worked perfect!

Votes

Translate

Translate

Report

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 ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

LATEST

any old time, friend. glad to help. 😃

Votes

Translate

Translate

Report

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