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

Error 45: Object is invalid when using JS reading groups

New Here ,
Aug 20, 2024 Aug 20, 2024

Copy link to clipboard

Copied

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
TOPICS
Bug , Scripting

Views

632

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
New Here ,
Aug 21, 2024 Aug 21, 2024

Copy link to clipboard

Copied

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?

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 ,
Aug 21, 2024 Aug 21, 2024

Copy link to clipboard

Copied

@Harald Dev 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

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 ,
Aug 21, 2024 Aug 21, 2024

Copy link to clipboard

Copied

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

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 ,
Aug 26, 2024 Aug 26, 2024

Copy link to clipboard

Copied

Hi Larry, that's what I thought but he says it used to work ok.

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
New Here ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

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!

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 ,
Aug 26, 2024 Aug 26, 2024

Copy link to clipboard

Copied

Hi @Harald Dev 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?

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
New Here ,
Aug 27, 2024 Aug 27, 2024

Copy link to clipboard

Copied

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?

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
New Here ,
Aug 29, 2024 Aug 29, 2024

Copy link to clipboard

Copied

Hello @CarlosCanto  I have done some more tests.

 

After running the Duplicate() function, the AI ​​file is in an unstable state. The elements are not always correctly transferred to the new layer. This is why error 45 sometimes occurs when accessing the elements later.

 

The Duplicate() function still worked in the old version of Illustrator 2015. Now the function only works with a probability of around 50%. It seems that access via COM automation is not stable.

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 ,
Aug 29, 2024 Aug 29, 2024

Copy link to clipboard

Copied

oh, ok @Harald Dev  then it seems the indexes are getting jammed as you add more groups.

 

try start duplicating from the bottom up instead of top down, then move the duplicates below the last original group. That way the original number of groups is not messed up. The duplicate function takes a second argument "ElemenetPlacement" to dup and move the dup at the same time.

 

let me know if that works

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
New Here ,
Sep 02, 2024 Sep 02, 2024

Copy link to clipboard

Copied

LATEST

Hello @CarlosCanto I think the problem is not duplicating the groups.

 

I changed my code. I have same problems copying the PageItems:

        for (int i = 1; i <= source.PageItems.Count; i++)   {
            source.PageItems[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.

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