2016-02-24 30 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