-
Alternatives to eval() in creating property paths (Extendscript)
I am using and older post to get the full paths from a list of properties.
https://creativecow.net/forums/thread/get-path-to-property-of-a-layer/
This works great, but it is using eval, which I’m wary of (I’ve experienced issues with it in other languages, and have seen folks talk about it being prone to errors.) One of the posters in this thread mentioned that you could store the indexes of the properties in an array and then loop through them. I’ve done this by stitching a string together, but am running into an issue of trying to use the string contents of a variable, and a property name.
var compItem = app.project.activeItem;
var arrayOfPropIndexes = [2, 2, 2];
var propIndexPath = "layer(1)";
for (var i = 0; i <= arrayOfPropIndexes.length - 1; i++) {
propIndexPath = (propIndexPath + "(" + arrayOfPropIndexes[i] + ")");
}
//This returns layer(1)(2)(2)(2)
$.writeln(propIndexPath);
//This returns the name of the property at indexes (2)(2)(2) of layer(1)
$.writeln(compItem.layer(1)(2)(2)(2).name);
//This returns the name of the property at indexes (2)(2)(2) of layer(1)
$.writeln(eval("compItem." + propIndexPath + ".name"));
//This errors out as "undefined is not an object"
$.writeln(compItem.propIndexPath.name);I feel like stitching together a string of the compItem and property path isn’t the right way to go about this, and I’m hoping to avoid using eval if I can. Is there a way to break out the contents of a variable to be used as a property address, or is this actually a good candidate for using eval? (Or is there another way all together that I could be going about building the address out of an array of indexes?) I’m still working my way through understanding how these properties are accessed, so any help would be appreciated.
Thanks!
Sorry, there were no replies found.