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

Script not running in ID CC2019

New Here ,
Mar 06, 2019 Mar 06, 2019

We had previously had a developer write a script to update text box content with properties from Create Cloud assets. The script had been running fine in CC 2018, but it now throws an error in CC 2019. It's been years since I was working in JavaScript, and I'm a newbie to this forum, so I'm hoping someone might have an easy answer on this that I'm just not seeing.

When the script runs, it's giving me: Error 21 (Error String: undefined is not an object) at line 1

Screen Shot 2019-03-06 at 9.54.26 AM.png

I have that exact same line in other scripts and do not run into the issue. Should I have some other argument listed instead of "undefined"? Any insight would be great.

Here is the script:

Screen Shot 2019-03-06 at 9.49.12 AM.png

TOPICS
Scripting
2.1K
Translate
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

Guide , Mar 06, 2019 Mar 06, 2019

Hi Kevin,

Although there are minor issues in the script, the main reason behind the 'undefined is not an object' error is not related to CC 2019. You would have encountered the same message in CC 2018.

Looking closer, you'll find that your document doesn't fit the specification—and the script is not strong enough to address the problem.

Here is the thing:

FrameOutside.png

Page 1.02 of your sample document, the ID.08 link has a parent group with the expected code frame, but the dims frame is not part of that group, a

...
Translate
Community Expert ,
Mar 06, 2019 Mar 06, 2019

Hi Kevin,

hard to tell what's going on because you did not post all the code.

What does function selectLinks() exactly do and return?

Regards,
Uwe

Translate
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 ,
Mar 06, 2019 Mar 06, 2019

Sorry about that. Newbie move. Here is the entire script:

Screen Shot 2019-03-06 at 11.30.31 AM.png

Translate
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 06, 2019 Mar 06, 2019

Best run your script without doScript() from the ESTK after setting some breakpoints in the code. You could then go step by step through code execution. Hope that you can see where it fails by inspecting values of variables at a given time and you can investigate a workaround.

Also: If someone should test your code copy/paste it to a reply and format it as javascript. For that you have to enable the Use advanced editor of the forum software. The Advanced Editor is not available when doing a reply in the forum's Inbox.

After switching to the Advanced Editor select the code text of your reply and do:

>> Insert > Syntax Highlighting > javascript

Also best provide sample documents via Dropbox or a smiliar service and post the download link.

Regards,
Uwe

Translate
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 ,
Mar 06, 2019 Mar 06, 2019

Thanks, Laubender. Super helpful. Here is the JavaScript, along with a link to the documents in question. Not sure how it will handle links to the CC library.

Source InDesign File:

1_0_ColorTypeVisualHierarchy_L.indd.zip - Box

JavaScript:

app.doScript(Main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Update Sign Grid");

function Main()

{

var LinkNames = ["GridLink","SignCode","Dims"] // [CC Library Link,  Link Name, Dimensions of Link]

var doc = app.activeDocument

var linksArray = doc.links;

var allLinks = selectLinks(LinkNames[0], "label", linksArray);

var gridGroup,signCode,signDims,linkName,vertScale,horScale,visBounds,obWidth,obHeight;

var linksUpdated = 0;

var nonGroup = 0;

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

   

gridGroup = allLinks.parent.parent.parent;

if (gridGroup.constructor.name == "Group"){

signCode = selectLabels(LinkNames[1], "label", gridGroup.pageItems);

signDims = selectLabels(LinkNames[2],"label",gridGroup.pageItems);

linkName = allLinks.name;

linkName = linkName.replace(".ai","");

vertScale = allLinks.parent.parent.allGraphics[0].verticalScale;

horScale = allLinks.parent.parent.allGraphics[0].horizontalScale;

visBounds = allLinks.parent.parent.allGraphics[0].visibleBounds;

obWidth = (Math.round((((visBounds[3]-visBounds[1]))*(100/horScale))*10)/10);

obHeight = (Math.round((((visBounds[2]-visBounds[0]))*(100/vertScale))*10)/10);

signCode[0].contents = linkName;

obDims = obWidth+"\" x "+obHeight+"\"";

signDims[0].contents = obDims;

linksUpdated++

}else{

    nonGroup ++}

}

alert(linksUpdated+" Links Updated & Labeled\r"+nonGroup+" Links incorrectly grouped",10);

}

function selectLinks(value, key, array){

    var i = array.length; var t; var filtered = [];

    while(i--){

            t = array;

            if(t && t.parent.parent[key] == value){

                filtered.push(t);

            }

    }

    return filtered;

}

function selectLabels(value, key, array){

    var i = array.length; var t; var filtered = [];

    while(i--){

            t = array;

            if(t && t[key] == value){

                filtered.push(t);

            }

    }

    return filtered;

}

function toArray(objects){

    var i = objects.length; var array = [];

    while(i--){

            array.push(objects);

    }

    return array;

Translate
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 06, 2019 Mar 06, 2019

Yes. The links to the missing CC Libraries are just that: missing.

Regards,
Uwe

Translate
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
Guide ,
Mar 06, 2019 Mar 06, 2019

Hi Kevin,

Although there are minor issues in the script, the main reason behind the 'undefined is not an object' error is not related to CC 2019. You would have encountered the same message in CC 2018.

Looking closer, you'll find that your document doesn't fit the specification—and the script is not strong enough to address the problem.

Here is the thing:

FrameOutside.png

Page 1.02 of your sample document, the ID.08 link has a parent group with the expected code frame, but the dims frame is not part of that group, as it should.

Unfortunately, your script doesn't check whether its selectLabels() function actually returns a non-empty value. While parsing the above structure, it fails to feed the signDims array, and then the statement (line #34) signDims[0].contents=obDims; meets undefined and causes the well-known error.

There are other vulnerabilities in the code. For example, it should at least check that the objects to be updated (via the contents property) are actual TextFrame instances. And that's easy to do: instead of browsing the PageItems collection of the current group, just browse its TextFrames!

Now, let's rewrite the code as an exercise 😉

app.doScript(Main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Update Sign Grid");

function Main(  doc,a,i,t,upd,err,dims,code,g,w,h,o,s)

//----------------------------------

{

    const GRID_LABEL = 'GridLink';                // CC Library Link

    const CODE_LABEL = 'SignCode';                // Link Name

    const DIMS_LABEL = 'Dims';                    // Dimensions of Link

    const RE_DOT_AI  = /\.ai$/;

    const MR         = Math.round;

   

    if( !(doc=app.properties.activeDocument) ) return;

   

    // Get links whose `parent.parent.label` is GRID_LABEL.

    // ---

    a = doc.links.everyItem().getElements();

    for( i=a.length ; i-- ; GRID_LABEL===a.parent.parent.label || a.splice(i,1) );

    for( upd=0, err=[], i=a.length ; i-- ; )

    {

        s = (t=a).name;

        // Link.parent.parent.parent should be a Group

        // ---

        g = (t=t.parent.parent).parent;

        if( !(g instanceof Group) ){ err.push(s); continue; }

       

        // Metrics => (w,h) (precision: 1/10)

        // ---

        dims = t.allGraphics[0].properties;

        t = dims.visibleBounds;

        w = .1*MR( 1e3*(t[3]-t[1])/dims.horizontalScale );

        h = .1*MR( 1e3*(t[2]-t[0])/dims.verticalScale   );

        // Update the code/dims frames within the group.

        // ---

        code = s.replace(RE_DOT_AI,'');

        dims = w + '" x ' + h + '"';

        for( t=g.textFrames.everyItem().getElements() ; (code||dims)&&(o=t.pop()) ; )

        {

            if( !(s=o.label) ) continue;

            ( code && CODE_LABEL==s && s=[code,code=false][0] ) ||

            ( dims && DIMS_LABEL==s && s=[dims,dims=false][0] ) ||

            ( s=false );

           

            if( !s ) continue;

            o.texts[0].contents = s;

        }

       

        // This is the issue the original code was missing.

        // If `code` or `dims` data remain, then a frame hasn't

        // been reached -- due to bad grouping or missing label.

        // ---

        (code||dims) ? err.push(a.name) : ++upd;

    }

    // Report.

    // ---

    s  = "Links fully updated and labeled: " + upd + ".";

    s += "\r\rLinks incorrectly grouped or labeled: " + err.length + ".";

    if( err.length )

    {

        40 < err.length && (err.length=40) && err.push('..');

        s += "\rDetail: " + err.join(", ") + ".";

    }

    alert( s );

}

This should be a bit safer.

@+,

Marc

Translate
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 ,
Mar 07, 2019 Mar 07, 2019
LATEST

Marc, you are a genius! Thank you so much for the assistance on this. I owe you!

Translate
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