Skip to main content
Known Participant
January 8, 2019
Question

Error 21: null is not an object.

  • January 8, 2019
  • 1 reply
  • 2848 views

I'm trying to edit an old script made in 2013 by someone.

I'm getting the following error.

Error 21: null is not an object.

->  var matchedName = doc.artLayers[artLayerIndex].name.match(/^.+(\..+)\.+/)[1];

whats wrong, is it incompatible with PS 2017

This topic has been closed for replies.

1 reply

pixxxelschubser
Community Expert
Community Expert
January 9, 2019

It seems wrong copied or composed by yourself.

At first:

It is hard to check a simple line with variables - nobody knows how they are defined!

Second:

The regex is looking for a filename as layer name with one dot followed by some characters and followed by one or multiple dots afterwards. This is not really a plausible combination.

Third:

If your layer name not followed the rule: characters dot characters and one dot as minimum at the end the regex not find [1] and the regex returns null.

Finally: What do you want to do?

Where the script comes from?

How the variables are defined?

What is your layer structure?

What layer names do you have in your documents?

At the end:

If you have a layer name eg Image123new_pic.jpg then you need

activeDocument.artLayers[0].name.match(/^.+(\..+)/)[1]

If not - read this posting again and again please.

Known Participant
January 9, 2019

Its a script from somewhere else. I believe it was written in 2013.

the error is line 7 although there may be more, from what i read it did work.

var doc = app.activeDocument;

var layerNameMap = {};

// find the layer's full names

// document must have layers with these string in the middle of the layer name i.e. teapot.lighting.tga

// case matters as does having two, and only two,  'dot' chars in the name

for(var artLayerIndex =0; artLayerIndex < doc.artLayers.length;artLayerIndex++){

    var matchedName = doc.artLayers[artLayerIndex].name.match(/^.+(\..+)\.+/)[1];

    switch(matchedName){

        case '.lighting': layerNameMap['lighting'] = doc.artLayers[artLayerIndex].name; break;

        case '.GI': layerNameMap['GI'] = doc.artLayers[artLayerIndex].name; break;

        case '.reflect': layerNameMap['reflect'] = doc.artLayers[artLayerIndex].name; break;

        case '.refract': layerNameMap['refract'] = doc.artLayers[artLayerIndex].name; break;

        case '.specular': layerNameMap['specular'] = doc.artLayers[artLayerIndex].name; break;

        case '.AO': layerNameMap['AO'] = doc.artLayers[artLayerIndex].name; break;

        default: doc.artLayers[artLayerIndex].visible = false;

    }

}

// now sort layers. if layers with required names are not in document these next lines will throw an error

doc.artLayers.getByName(layerNameMap['lighting']).move(doc.artLayers[doc.artLayers.length-1], ElementPlacement.PLACEAFTER);

doc.artLayers.getByName(layerNameMap['GI']).move(doc.artLayers[doc.artLayers.length-2], ElementPlacement.PLACEAFTER);

doc.artLayers.getByName(layerNameMap['reflect']).move(doc.artLayers[doc.artLayers.length-3], ElementPlacement.PLACEAFTER);

doc.artLayers.getByName(layerNameMap['refract']).move(doc.artLayers[doc.artLayers.length-4], ElementPlacement.PLACEAFTER);

doc.artLayers.getByName(layerNameMap['specular']).move(doc.artLayers[doc.artLayers.length-5], ElementPlacement.PLACEAFTER);

doc.artLayers.getByName(layerNameMap['AO']).move(doc.artLayers[doc.artLayers.length-6], ElementPlacement.PLACEAFTER);

// select the top layer

doc.activeLayer = doc.artLayers.getByName(layerNameMap['AO']);

// add a layerSet

var IDLayerSet = doc.layerSets.add();

IDLayerSet.name = 'ID';

makeCurveAdjustmentLayer();

selectLayerBelow();

AOLevels();

doc.activeLayer.blendMode = BlendMode.OVERLAY;

selectLayerBelow();

doc.activeLayer.blendMode = BlendMode.SCREEN;

selectLayerBelow();

doc.activeLayer.blendMode = BlendMode.SCREEN;

selectLayerBelow();

doc.activeLayer.blendMode = BlendMode.SCREEN;

selectLayerBelow();

doc.activeLayer.blendMode = BlendMode.SCREEN;

function makeCurveAdjustmentLayer(){

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

     ref.putClass( charIDToTypeID( "AdjL" ) );

   desc.putReference( charIDToTypeID( "null" ), ref );

   var desc1 = new ActionDescriptor();

     desc1.putClass( charIDToTypeID( "Type" ), charIDToTypeID("Crvs" ) );

   desc.putObject( charIDToTypeID( "Usng" ), charIDToTypeID( "AdjL" ), desc1 );

   executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );

};

function AOLevels() {

    var desc = new ActionDescriptor();

    desc.putEnumerated( stringIDToTypeID('presetKind'), stringIDToTypeID('presetKindType'), stringIDToTypeID('presetKindCustom') );

        var list = new ActionList();

            var desc1 = new ActionDescriptor();

                var ref = new ActionReference();

                ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Cmps') );

            desc1.putReference( charIDToTypeID('Chnl'), ref );

                var list1 = new ActionList();

                list1.putInteger( 0 );

                list1.putInteger( 128 );

            desc1.putList( charIDToTypeID('Otpt'), list1 );

        list.putObject( charIDToTypeID('LvlA'), desc1 );

    desc.putList( charIDToTypeID('Adjs'), list );

    executeAction( charIDToTypeID('Lvls'), desc, DialogModes.NO );

};

function selectLayerBelow(){

    var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Bckw" ) );

    desc.putReference( charIDToTypeID( "null" ), ref );

    desc.putBoolean( charIDToTypeID( "MkVs" ), false );

    executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

};

pixxxelschubser
Community Expert
Community Expert
January 9, 2019

I am still of the same opinion. The regex cannot match anything!

Give me please same examples of your layer names.

Aktualisiert

Or better give an example file please.