2016-02-24 27 views
0

我正在尝试编写一个脚本,将字符数组解析为After Effects中文本图层的后续关键帧。一切正常。只有我想更改函数来读取unicode而不是普通文本。Extendscript将unicode字符解析为文本图层

这里是我的脚本代码:

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(); 

而这正是我试图解析字符串:

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

这些都是Unicode字符。

+0

你能不能熬您的代码段下来,不用的字体,没有一个文本文件,有人能执行吗?或者至少提供您正在尝试解析的文件或字符串? – fabianmoronzirfas

+0

@fabiantheblind完成:) – vinni

回答

1

我玩了一下你的代码。我也无法从文件中读取unicode字符。有效的是以JSON格式存储数据并解析它。 (在这里获取解析器https://github.com/douglascrockford/JSON-js) 请参阅下面的修改后的代码。

这是该数据作为JSON

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

这是修改后的脚本

#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(); 
+0

谢谢!完美的作品 – vinni

+0

嗨fabian,剧本完美,但我遇到了日语(utf-16,如果我理解正确)的问题,如“\ u3402 \ ue0101”。 json中的标准编码似乎是utf-8(http://tools.ietf.org/html/rfc4627#section-3)我发现如何改变它。也许你有一个想法?谢谢! – vinni

+0

也许像这样读他们会有帮助吗? 'keyValues [0] .toString(16)' – fabianmoronzirfas