

本文内容为AE ExtendScript 脚本编写的开发环境搭建及部分实例。因为平时在制作视频时,会有一些复制模板并替换里面的重复性劳动,所以一直想脚本代工,于是终于找了一天入门了一下,做个笔记分享。 ExtendScript写法类似JavaScript(ES5),运行后自上而下执行 ,有JS基础的话上手难度比较低,学习成本主要为找API的时间。
开发工具有官方的Adobe ExtendScript Toolkit 但是好像不维护了,所以我选择了VS Code作为编辑器的方法。
首先找一个摆放工程的目录在VS Code中打开,新建一个文件,命名为【脚本名.jsx】, 在VS Code的商店里搜索并安装【AE Script Linker】和【ExtendScript Debugger】。
AE Script Linker安装好后在打开.jsx文件时,编辑器右上角出现【▶】按钮【提示链接到After Effect 中运行】,表明安装成功,在打开AE的情况下,点击【▶】会跳转AE运行该脚本。此时可以在.jsx中写:
alert("Hello World"); 点击运行:

HelloWorld
ExtendScript Debugger的主要功能为断点调试,安装好后。在工程目录新建【.vscode】文件夹,在其中新建文件【launch.json】,输入内容:
{
"version": "0.2.0",
"configurations": [
{
"type": "extendscript-debug",
"request": "launch",
"name": "Run Current Script",
"program": "${file}",
"stopOnEntry": false,
"excludes": [
"undefined",
"builtin",
"Function",
"prototype"
],
}
]
} 运行Debug后运行脚本,如有断点会命中。

ExtendScript Debugger
首先是信息输出。
write("Hello World Console");
writeLn("Hello World Console");
alert("Hello World"); 再来是获取节点的代码 可以理解为web页面去获取DOM对象。
item表示的是文件目录下的选择,他可以是文件夹、音频、图片、预合成等。
当item为预合成(CompItem)时 通过.layer()选择里面的图层。
//app.project 为项目文件结构的根 是一种Item类
//他的index是从1开始 并不是从0开始 获取不到的话是返回void 0
var m_item = app.project.item(index); // 等价于 Item类.item(index);
alert(m_item.name);
//如果Item是一个合成(Comp) 可以从这个合成里获取层
var m_layer = m_item.layer(index);
alert(m_layer.name);
//layer肯定包含于某个CompItem中 获取方法为
var sourceItem = m_layer.source;
//item包含的子item数量
alert(m_item.numItems);
//compItem包含的layer数量
alert(m_item.numLayers); Adobe并不提供基于name的选择方法,根据基础API,为了方便使用,可以自行封装查询。例:
//从ParentItem中获取Item
function GetChildItemByName(parentNode, childName, isComp) {
for (var i = 1; i <= parentNode.numItems; i++) {
if ((isComp ? parentNode.item(i) instanceof CompItem : true) && parentNode.item(i).name == childName) {
return parentNode.item(i);
}
}
}
//从ParentItem中获取Layer
function GetChildLayerByName(parentNode, childName, isGetSource) {
for (var i = 1; i <= parentNode.numLayers; i++) {
if (parentNode.layer(i).name == childName) {
if (isGetSource) {
return parentNode.layer(i).source;
}
return parentNode.layer(i);
}
}
} 引用其他jsx脚本
#include "scripts/other.jsx" 读取文件
以选择一个json读取为例:
function ReadJson() {
var m_file = new File().openDlg('选择配置文件', '*json');
if (m_file != null) {
var res = m_file.open('r');
if (res) {
var txt = m_file.read();
m_file.close();
return JSON.parse(txt);
}
writeLn('fail'); // 向信息面板输出
} else {
writeLn('cancel');
}
} //替换Item素材
item.replace(File(filePath));
//替换Layer素材
layer.replaceSource(itemOrLayer, true); var io = new ImportOptions(File(FilePath));
var videoImport = app.project.importFile(io);
videoImport.parentFolder = folderItem;//修改文件夹所属 var copy_item = item.duplicate();
copy_item.parentFolder = ...yourFolder; //缩放
layer.property("Scale").setValue([width,height]);
//文本层的文字
layer.property("Source Text").setValue(value);
//起始时间(秒)
layer.startTime = 20;
//结束时间
alert(layer.outPoint); var wnd = new Window("palette", "Test Window", [0, 0, 250, 150]);
wnd.add("statictext", [30, 10, 220, 30], "输入框 :");
var input = wnd.add("edittext", [30, 30, 220, 50], "AAAAA");
var okBtn = wnd.add("button", [55, 100, 140, 135], "OK");
wnd.center();
wnd.show();
okBtn.onClick = function(){
Main();
} 基于以上API,实现了从json文件读取配置,替换文字、图片、音频、复制预合成、生成并整理时间线的脚本工具。

01.《AE脚本设计参考手册V1.0.0》
02.《After-Effects-CS6-Scripting-Guide》
03.《AEスクリプトでウィンドウ(GUI)をつくる方法》
04.《Adobe ExtendScript Debuggerのセットアップ》