Skip to main content
Known Participant
August 20, 2024
Question

Error 45: Object is invalid when using JS reading groups

  • August 20, 2024
  • 2 replies
  • 1527 views

We have a .NET application which calls a Javascript function via COM in Adobe Illustrator 2024 V28.6.
Sometimes it works, but sometimes we get an error. Everything worked in the old Adobe Illustrator 2015.

 

Sometimes we get this error: Object is invalid
-> getTextFrames(group.textFrames, textFrames);

See last lines in the JS code below. The group object is invalid!

 

The script errors only occur when the script is called programmatically. When the script is called via the Illustrator interface or via the ExtentedScript Toolkit, the scripts run without any problems.

 

var defaultNumberDecimalSeperator = "##defaultNumberDecimalSeperator##";
var newSeparator = "##newSeparator##";
var layerName = "##layerName##";

var doc = app.activeDocument;
var myLayer = doc.layers.getByName(layerName);

replaceDecimalSeparator(myLayer, newSeparator)

function replaceDecimalSeparator(layer, newSeparator) {
    var textFrames = getAllTextFrames(layer);

    for (var i = 0; i < textFrames.length; i++) {
        var myTextFrame = textFrames[i];
        var myContent = myTextFrame.contents;
        var index;

        // Alle Dezimaltrenner im Textrahmen ersetzen
        do {
            index = myContent.indexOf(defaultNumberDecimalSeperator);
            if (index === -1) {
                break;
            }

            myTextFrame.characters[index].contents = newSeparator;
            myContent = myTextFrame.contents; // Inhalt nach der Änderung neu laden
        } while (index !== -1);
    }
}

// Rekursive Funktion zum Ermitteln aller TextFrames in der Ebene
function getAllTextFrames(myLayer) {
    var myTextFrames = [];
    getTextFrames(myLayer.textFrames, myTextFrames);
    getTextFramesInGroups(myLayer.groupItems, myTextFrames);
    return myTextFrames;
}

function getTextFrames(frames, myTextFrames) {
    for (var i = 0; i < frames.length; i++) {
        myTextFrames.push(frames[i]);
    }
}

function getTextFramesInGroups(groups, mytextFrames) {
    var myGroup;

    for (var i = 0; i < groups.length; i++) {
        myGroup = groups[i];

        getTextFrames(myGroup.textFrames, mytextFrames);
        getTextFramesInGroups(myGroup.groupItems, mytextFrames);
    }
}
 
We tested with sleep() and app.redraw. Unfortunately without success.
 
Best regards
 
Harald
This topic has been closed for replies.

2 replies

Known Participant
August 22, 2024

We have created a small C# project on Github (Program.cs). The script and AI file are also included. https://github.com/danielnierlin/IllustratorScriptExample

 

It was noticeable that if I just open the AI file ​​and run the script on the original layer "MD_2_BEMASSUNG", it always worked. The problem only occurs if I duplicate the layer first. We do this via COM with Duplicate().

 

Thank you very much for your help!

CarlosCanto
Community Expert
Community Expert
August 26, 2024

Hi @haraldw93098100 I finally got a chance to test, I do not do C# so I run the jsx from vbs and I did not get any errors.

 

you mentioned it works good the first time but after duplicating it doesn't. To test, should I duplicate the layer via vbs (c# in your case) then run the jsx on the duplicate? or what's the sequence?

Known Participant
August 27, 2024

Thank you very much for your help! Yes, the file "replaceDecimalSeparator.js" runs without errors. The problem is the Duplicate() function. I think that's too many operations for COM automation.

 

In this loop, about 150 elements are copied into the new layer:

 

for (int i = 1; i <= source.GroupItems.Count; i++) {
   source.GroupItems[i].Duplicate(newLayer);}

 

 

It doesn't run into any errors here, but the AI ​​file seems to be corrupt after this operation. That's why the "replaceDecimalSeparator.js" runs into an error 45 later.

 

If I don't run the Duplicate() function, the program runs without errors. So I think the problem is in the Duplicate() function. How can I code the copying of a layer differently with COM? Or I can replace it with a JavaScript function. Is there a code snippet in JavaScript for copying a layer?

Known Participant
August 21, 2024

I have done further tests. Error 45 occurred in about 50% of all tests. In all other cases the script ran without errors. Why does the script not run stably in Illustrator version 2024? This bug was not present in the older Illustrator version 2015.

 

What kind of bug is this in Illustrator 2024? Sometimes the query goes to the groups, sometimes an error is thrown. Only try-catch helps. And I have tried this in various ways. Repeatedly with waiting times and with app.redraw(). Unfortunately, that didn't improve things either.

 

What can help to fix error 45 (object is invalid)? Does anyone have an idea?

CarlosCanto
Community Expert
Community Expert
August 21, 2024

@haraldw93098100 can you share a sample file where the script fails? save your ai file as pdf and upload it here, or save the ai in Google Drive, DropBox or similar and post the link here

Larry G. Schneider
Community Expert
Community Expert
August 21, 2024

Carlos, sounds like the indexs are getting mixed up. Should he be running the search in reverse?