Replacing Text MRAP

Participant ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Any reason I would be getting an MRAP error on this snipit?  I have used this forever and just recently its been kicking back MRAP.

var Number = "123456";

function AddTextToFrame(num){

    var idoc = activeDocument;

    var text = idoc.textFrames;

    for(var i=0; i<text.length; i++){

        if(text.contents == "99999"){

            text.contents = num;

        }

    }

};     

AddTextToFrame(Number);

TOPICS
Scripting

Views

617

Likes

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

Adobe Community Professional , Apr 06, 2021 Apr 06, 2021
so i know it's 3 years later... but i just happened upon this post again while searching for something else, and i realize what was wrong with your code originally. so just in case anybody here in the future finds this post and can't figure out why Jeremy's code wasn't working (because maybe you're having the same problem...) i'll go over it with you. It's a very easy mistake to make, but with a little practice you can learn to recognize it quickly. Consider these lines: var text = idoc.textFr...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Traversing the document items is kind of like walking on egg-shells in our world, but also in your case "Number" could be causing issues as it may be a reserved JS keyword. Maybe they were OK with using this word before, but now they aren't ?

alert(new Number("123") instanceof Number);

Likes

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
Participant ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Thanks V.  The Actual input for number is var aNumber = prompt('Please enter a number','');  I just used that as a place holder. You think this would still cause the issue?

aNumber = prompt('Please enter a number','');

function AddTextToFrame(num){

    var idoc = activeDocument;

    var text = idoc.textFrames;

    for(var i=0; i<text.length; i++){

        if(text.contents == "99999"){

            text.contents = num;

        }

    }

};    

AddTextToFrame(aNumber);

Like (0) Reply

Likes

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
Adobe Community Professional ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

are you getting the MRAP error on this line:

text.contents = num;

???

is it possible that it's attempting to change the contents of a textFrame that is either locked, or resides on a layer that's locked?

try this here little snippet and see whether it sheds some light on where it's going wrong.

function test()

{

    aNumber = prompt('Please enter a number', '');

    function AddTextToFrame(num)

    {

        var idoc = activeDocument;

        var text = idoc.textFrames;

        for (var i = 0; i < text.length; i++)

        {

            if (text.contents == "99999")

            {

                $.writeln("processing textFrame[" + i + "]");

                $.writeln("Attempting to set the contents to: " + num);

                try

                {

                    text.contents = num;

                }

                catch(e)

                {

                    alert("Failed to set the contents. System error: " + e);

                    $.writeln("script failed on the " + (i + 1) + "th textFrame");

                    if(text.parent.locked || !text.parent.visible || text.parent.hidden)

                    {

                        alert("the parent layer or object ( " + text.parent + ") for the desired text frame is locked or hidden.");

                    }

                }

            }

        }

    };

    AddTextToFrame(aNumber);

}

test();

Likes

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
Participant ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Thanks for the snippit Will.

I tried your solution and it works just fine.  I think like V said, walking on eggshells.  Sometimes it works, sometimes it doesn't.

For the time being before your reply I put a small line in there that unlocks everything before the operation starts.  Solved the problem so far.

app.executeMenuCommand('unlockAll');

I'm not sure how reliable these MenuCommands are, but they have proven to work well for global things.

Likes

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
Adobe Community Professional ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Hey my bad, I did not notice "a" in "aNumber".

Hopefully it fixes for you, I've actually noticed that as far as text is concerned, the text contents can be changed even when the textframe is locked, or its parent layer is locked. Same thing with being invisible.

#target illustrator

function test(){

var doc =app.activeDocument;

    var t = doc.textFrames[0];

    t.contents = "HELLO!";

};

test();

Screen Shot 2018-02-20 at 2.11.21 PM.png

Screen Shot 2018-02-20 at 2.11.34 PM.png

So, it changes the text without any hitches.

#target illustrator

function test(){

    var doc =app.activeDocument;

    var mySwatch = doc.swatches.getByName("CMYK Yellow");

    var t = doc.textFrames[0];

    t.textRange.characterAttributes.fillColor = mySwatch.color;

};

test();

I also grouped the textframe in a group and tried to change the fillColor, which also worked.

I'd want to know if this text-replacement routine is happening in the middle of a bunch of other processing where documents are being closed - this can produce an ambiguous scenario that we are still trying to pin-point, where the 'doc' variable reference is still somehow attached to the old 'doc' that's been already closed.

Likes

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
Participant ,
Feb 20, 2018 Feb 20, 2018

Copy link to clipboard

Copied

Thanks V.  It is happening when I open document.  This snip runs to change text on all parts of my product. About 50 different text fields. I try to make a point of including activeDoc variables in most functions as it doesn't always pull it from outside of the function if processing or the doc being closed.

Likes

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
Adobe Community Professional ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

Are you batching several files with this script? If so, are you opening a file, updating the textFrames, closing the file, open next file.... etc.???

If you're running a batch, it's a really good idea to open all of your files first, then process each open document, then when processing is complete, save/close the files.

Likes

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
Participant ,
Feb 21, 2018 Feb 21, 2018

Copy link to clipboard

Copied

At most its only 2 files and both stay open until the process is complete.  Batch processing gets tricky with illustrator.  I do most of my processing via javascript loop and hot folder not using the batch tool that illustrator has.

Likes

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
Adobe Community Professional ,
Apr 06, 2021 Apr 06, 2021

Copy link to clipboard

Copied

so i know it's 3 years later... but i just happened upon this post again while searching for something else, and i realize what was wrong with your code originally.

 

so just in case anybody here in the future finds this post and can't figure out why Jeremy's code wasn't working (because maybe you're having the same problem...) i'll go over it with you. It's a very easy mistake to make, but with a little practice you can learn to recognize it quickly. Consider these lines:

var text = idoc.textFrames;

for(var i=0; i<text.length; i++){
    if(text.contents == "99999"){

 

  1. The variable "text" is defined as the textFrames collection of idoc. 
  2. loop defined and set to iterate over each element in the collection "text".
  3. condition checks to see if the "contents" property of "text" is equal to "99999" // ERROR

 

The textFrames collection does not have a "contents" property, which is what was causing the error. Since line 3 exists inside a loop body, we need to make sure we're referencing some element of "text", and not "text" itself. Using the variable "i" we get a specific textFrame which does have a "contents" property that can be checked.

 

Here's the fixed snippet from the original post.

var aNumber = "123456";
function AddTextToFrame(num){
    var idoc = activeDocument;
    var text = idoc.textFrames;
    for(var i=0; i<text.length; i++){
        if(text[i].contents == "99999"){
            text[i].contents = num;
        }
    }
};     

AddTextToFrame(aNumber);

 

Likes

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
Advocate ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

"we need to make sure we're referencing some element of "text", and not "text" itself"

 

A way to remind onerself of this is to make the name of a collection plural.  

Likes

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
Contributor ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

I suspect you meant this:

 

function AddTextToFrame(num){
    var idoc = activeDocument;
    for(var i=0; i<idoc.textFrames.length; i++){
        var textFrame = idoc.textFrames[i];
        if(textFrame.contents == "99999"){
            textFrame.contents = num;
        }
    }
}   

Likes

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
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Yes that does make it easier to read. But I was trying to keep the original code and just fix the bug so that it would be easy for a beginner to compare to the code in the OP. 

Likes

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
Contributor ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

It wasn’t that. It was the fact OP’s code obviously intended to iterate over the text frames manipulating each one in turn, but it forgot to ask for `app.activeDocument.textFrames[i].contents` so was actually attempting to manipulate `app.activeDocument.textFrames.contents` multiple times instead. i.e. A bug in OP’s code which might well explain the [admittedly cryptic and unhelpful] error message.

Likes

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
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

yes. that's what i pointed out in my response last night. my fix did the same thing as yours, i just left the rest of their code unchanged instead of changing the variables up.

 

I think maybe you saw this post at the top of the list and thought OP was asking for an answer. this post is 3 years old and I just happened to find it again and realize what was wrong with it (because i didn't notice properly at the time). So I posted the answer i should have posted back then and that brought the topic back to the main page.

Likes

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
Contributor ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Yeah, I did. Teaches me not to read closer. Damn zombie posts with tiny dates. Really wish Adobe forums’d indicate old posts brought back to life after X months/years. It can be useful to do so, but I’m not the first to be suckered by that.

Likes

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
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

LATEST

And you won't be the last. 😉 

Likes

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