How to check if a property value is readonly using extendscript?
Asked Answered
B

3

8

I'm writing a script for After Effects that collects all properties from a layer and write them into an XML file. When I retrieve the values from the XML, some values are readOnly and the toolkit throws an error.

Is there any way to check it, like readonly attribute of File object? ie: layer.property().(readonly||readOnly)

If not, someone can tell me wich aproach can I take to go in the right direction?

Burgoo answered 2/8, 2015 at 13:1 Comment(3)
I guess I'm a bit confused because if you are writing the values successfully to the XML file, then that should just be a plain text file, and you should be able to read any property there. If you are retrieving the properties, then all you should need to do is read them.Adim
@ariestav: OP doesn't mention it but it seems he wants to set them on reading.Spay
Bummer. I was going to suggest Object Reflection and its 'properties' array "ReflectionInfo" (which promised to tell me if it's "one of unknown, readonly, readwrite, createonly, method or parameter"), but testing on InDesign everything except reflect seems to be tagged Read/Write – even for properties clearly marked Read Only in the object's properties!Spay
C
4

Given that the first item in the project is a comp with a solid in it, this works but it is arguably kludgey, and you'd need to be able to build the (each) string in order to do this -- but maybe you are already set up to do that:

var r;
r = testForReadability("app.project.items[1].layers[1].enabled");
alert(r);
r = testForReadability("app.project.items[1].layers[1].width");//a solid's width is NOT writable
alert(r);

function testForReadability(thisProperty) {
    var x;
    try {
        x = eval(thisProperty);
        eval(thisProperty + " = x;");
        return true;
    } catch(e) {
        return false;
    }
}

However, there is a small can of worms opening up here, in that "false"s will not work if the "Enable Script Debugger" option is set. So you need to do a workaround in order to check for this setting and temporarily reset it (see http://aenhancers.com/viewtopic.php?f=8&t=189&p=554&hilit=debugger#p554 )

Cutoff answered 4/8, 2015 at 15:4 Comment(6)
That was what I looked for. I will check it when come back from hollydays. Thanks!Burgoo
Be aware that in some cases, your code will be broken in some cases. e.x.: "app.project.items[1].layers[1].property('ADBE Text Properties').property('Source Text').value.justification" - when the text layer have two rows and have two different justification.Leupold
An attempted edit on this code changed the " = x;' to "=" + x + ";" but this is not correct. x should be eval-ed as x (the script understands x as a variable and will resolve it correctly in eval), not resolved to its value, concatenated and eval-ed.Cutoff
BTW, regarding that aenhancers link: Which preferences section name and key has changed through a couple of versions of AE (since CS6). You may need to experiment with these section names: "Main Pref Section", "Main Pref Section v2", "Extendscript"; and these keys: "Pref_JAVASCRIPT_DEBUGGER", "EnableExpressionsDebuggingAtYourOwnRisk". The latter key would only work with the "Extendscript" section.Cutoff
It works fine. For my purposes I have simplified it without using eval try{ thisProperty = xmlProperty; return true; } catch(e) { return false; } Thanks!Burgoo
Incidentally, the js debugger workaround seems further complicated by the latest version of CC. I temporarily made AE un-launchable by changing the settings by script. Probably best (or certainly safest) to just warn and have user change setting.Cutoff
L
2

I don't think you can get this information from the ESTK.

You can use the 'After Effects Scripting Guide Book' to check and create an object that contains all the 'readonly' properties, and then to check if the object includes this property.

Here's a link for the scripting guide: After-Effects-CS6-Scripting-Guide

Leupold answered 3/8, 2015 at 6:11 Comment(1)
I thought about this aproach but it seems to be very cumbersome. I reserve this way as tha last. Thanks!Burgoo
D
1

Just try to override it, and revert it back, like this:

function isReadOnly(value, container) {
  var tmp = container[value];
  var tmp2;
  var coolString = "cool";
  try {
    container[value] = "cool";
  } catch (e) {
    return true
  }
  tmp2 = container[value];
  container[value] = tmp;
  return coolString != tmp2;
}

// true, navigator.platform is read only
console.log(isReadOnly("platform", navigator))

// false, window.parent is not read only
console.log(isReadOnly("parent", window))
Devastate answered 13/4, 2017 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.