Search code examples
unicodeextendscriptafter-effects

Extendscript parse unicode characters into text layer


I'm trying to write a script that parses an array of characters into subsequent keyframes of a text layer in After Effects. Everything works. Only I'd like to change the function to read unicode instead of normal text.

Here's my Code for the Script:

var textLayer = currentComp.layers.addText("test");
        textLayer.name = "score";
        textLayer.position.setValue([50,500]);

        //Chose the txt with the array
        var myFile = File.openDialog("Navigate to keyframe file.");
        myFile.open("r");

        var myLine = myFile.readln();
        var keyValues = myLine.split(",")

        var prop1 = app.project.item(1).layer(1).property("ADBE Text Properties").property("ADBE Text Document");

        var arrayLength = keyValues.length;

    //Keyframe Loop
    app.beginUndoGroup("Keys");

        for(var i=0; i<arrayLength; i++){
        prop1.setValueAtTime([i]/25,keyValues[i]);
        }
    app.endUndoGroup();

And this is the String that i'm trying to parse:

\u5c07,\u63a2,\u8a0e,\u53ca,\u5176,\u4ed6

These are all unicode characters.


Solution

  • I played a bit with your code. I also was not able to read the unicode characters from the file. What worked is storing the data in a JSON format and parsing it. (get the parser here https://github.com/douglascrockford/JSON-js) See my modified code below.

    This is the data as JSON

    ["\u5c07","\u63a2","\u8a0e","\u53ca","\u5176","\u4ed6"]
    

    This is the modified script

    #include "json2.js"
    // use json parsers
    // get it here https://github.com/douglascrockford/JSON-js
    var main = function() {
      var currentComp = app.project.items.addComp("test", 800, 600, 1, 10, 25); // add a comp
      var textLayer = currentComp.layers.addText("test");
      textLayer.name = "score";
      textLayer.position.setValue([50, 500]);
    
      //Chose the JSON with the array
      var myFile = File.openDialog("Navigate to keyframe file.");
      if (myFile === null) return;  // stop if user cancels
      myFile.open("r"); 
      var content = myFile.read();// read the whole file
      var keyValues = JSON.parse(content);// parse its content, dont use eval
      // rest is as before
      var prop1 = app.project.item(1).layer(1).property("ADBE Text Properties").property("ADBE Text Document");
      // var arrayLength = keyValues.length; // dont need this
      //Keyframe Loop
      app.beginUndoGroup("Keys");
      for (var i = 0; i < keyValues.length; i++) {
        prop1.setValueAtTime([i] / 25, keyValues[i]);
      }
      app.endUndoGroup();
    }
    main();