function getPropPath(prop) {
    var layerRoot = false;
    var propPath = "";

    while (prop.parentProperty) {
        propPath = "('" + prop.name + "')" + propPath;
        prop = prop.parentProperty;
    }
    return propPath;
}


function searchProperties(curItem, checkName, nestingLevel, fxName, propertyPath) {

    var totalProps = curItem.numProperties;

    for (var k = 1; k <= totalProps; k++) {

        curProp = curItem.property(k);

        if (fxName == null) { // It's not an FX
            if (curProp.matchName == checkName) {
                if (getPropPath(curProp) == propertyPath && !curProp.lock) curProp.selected = true;
                return;
            }

            if (curProp.numProperties > 0) {
                searchProperties(curProp, checkName, nestingLevel + 1, fxName, propertyPath)
            }
        } else { // Check also the Effects names
            if (curProp.matchName == checkName && curProp.parentProperty.name == fxName) {
                curProp.selected = true;
                return;
            }

            if (curProp.numProperties > 0) {
                searchProperties(curProp, checkName, nestingLevel + 1, fxName, propertyPath)
            }
        }

    }

}


(function () {
    try {
        app.beginUndoGroup("Select Matching Property");
        var comp = app.project.activeItem;
        var layers = comp.selectedLayers;
        var selProperties = comp.selectedProperties;
        var fxName = null;

        if (!comp || !(comp instanceof CompItem)) {
            alert("Please select a composition");
            return;
        }

        if (layers.length < 2 || selProperties.length < 1) {
            alert("First, select a property that you want to search, then select the other layers that contain the same property.");
            return;
        }


        var searchName = selProperties[0].matchName;
        propertyPath = getPropPath(selProperties[0]);
        if (selProperties.length > 1) {

            //runGrab(comp, layers);


            var searchName = selProperties[0].matchName;
            if (selProperties.length == 2 && selProperties[0].isEffect) {
                // If it's an effect check the effect names
                fxName = selProperties[0].name;
                searchName = selProperties[1].matchName; // If it's a effect select the second one
            } else {
                alert("Only one property can be selected at a time");
                return;
            }
        }


        if (layers.length <= 1) {
            //return;
        }

        for (var k = 0; k < layers.length; k++) {
            layer = layers[k];          
            searchProperties(layer, searchName, 0, fxName, propertyPath)
        }


    } catch (err) {
        alert(err);
    } finally {

        app.endUndoGroup();
    }
})();