AE拓展脚本自动更新csxs
csxs是adobe拓展脚本的配置文件,决定了当前拓展可以在哪些产品上使用,这个拓展有多少个窗口,如果在开发过程中,有动态修改csxs的需求,可以使用下面的代码,一键更新csxs
/**定义窗口 */
const extobj = {
"com.loyoi3.0": { name: "AE6工具箱", size: [800, 300] },
"com.loyoi.search": { name: "AE6搜索", size: [800, 300] },
"com.loyoi.buts": { name: "AE6按钮", size: [800, 300] },
"com.loyoi.library": { name: "AE6在线库", size: [800, 500] },
"com.loyoi.setting": { size: [450, 300], type: "ModalDialog" },
}
/**初始化窗口 */
function initWin() {
const arr = Object.keys(extobj);
/**获取窗口数据 */
const exts = window.__adobe_cep__.getExtensions(JSON.stringify(arr))
const requireArr = JSON.parse(exts);
// 这里对窗口数量做匹配
if (requireArr.length !== arr.length) {
const str = (csxsStr(
arr.map(e => `<Extension Id="${e}" Version="3.0"/>`).join(" "),
arr.map(e => getExtStr(e,
extobj[e]["name"],
extobj[e]["size"],
extobj[e]["type"]),
).join(" "),
));
updataCSXS(str)
}
// console.log(JSON.parse(exts));
}
/**
* 传入完整的csxs字符串,写入文件后使用命令行替换
* @param {*} str
*/
function updataCSXS(str) {
// console.log(str);
const loyoi = window.loyoi;
loyoi.evalScript(`confirm('配置文件需要更新,现在更新吗?\\n>>>重要!>>>\\n更新后请重启ae!\\n更新后请重启ae!\\n更新后请重启ae!',false,'AE6提示')`, (v) => {
if (v <span style="font-weight: bold;" class="mark"> "true") {
// if (v </span> "true") {
const oldPath = loyoi.ext + `/CSXS/manifest.xml`
const newPath = `${loyoi.user}/LoYoi/manifest.xml`
loyoi.writeFile(newPath, str)
let cmd = null
if (loyoi.isMac) {
cmd = JSON.stringify(["mv", "-f", newPath, oldPath])
} else {
cmd = 'move /Y "' + newPath.split("/").join("\\") + '" "' + oldPath.split("/").join("\\") + '"'
}
// console.log(cmd);
loyoi.admin("更新CSXS", cmd, (code, err) => {
if (code <span style="font-weight: bold;" class="mark"> 0) {
loyoi.msg({ text: "更新成功,重启[AE]生效!", type: "success", duration: 2500 })
} else if (code </span>= 1) {
loyoi.msg({ text: "更新失败,退出代码" + code, type: "error", duration: 2500 })
} else if (code === "err") {
loyoi.msg({ text: `出错,更新失败:${err}`, duration: 2500 })
}
window.cep.fs.deleteFile(newPath)
})
}
})
}
/**
* 构建完整的csxs数据
* @param {*} id
* @param {*} extList
* @returns
*/
function csxsStr(id, extList) {
return `<?xml version="1.0" encoding="UTF-8" standalone="no"?><ExtensionManifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ExtensionBundleId="com.loyoi.ae6" ExtensionBundleVersion="3.5" Version="7.0"> <ExtensionList> ${id} </ExtensionList> <ExecutionEnvironment> <HostList> <Host Name="ILST" Version="[13.0,99.9]"/> <Host Name="PHSP" Version="[13.0,99.9]"/> <Host Name="PHXS" Version="[13.0,99.9]"/> <Host Name="PPRO" Version="[13.0,99.9]"/> <Host Name="AEFT" Version="[13.0,99.9]"/> </HostList> <LocaleList> <Locale Code="All"/> </LocaleList> <RequiredRuntimeList> <RequiredRuntime Name="CSXS" Version="7.0"/> </RequiredRuntimeList> </ExecutionEnvironment> <DispatchInfoList> ${extList} </DispatchInfoList></ExtensionManifest>`
}
/**
* 构建面板配置文件
* @param {*} id
* @param {*} name 面板名字
* @param {*} size 默认大小
* @param {*} type 面板类型
* @returns
*/
function getExtStr(id, name, size, type) {
// 类型默认为可停靠面板 ModalDialog
type = type || "Panel"
name = name ? `<Menu>${name}</Menu>` : ""
return `<Extension Id="${id}"> <DispatchInfo> <Resources> <MainPath>./dist/index.html</MainPath> <CEFCommandLine> <Parameter>--allow-file-access-from-files</Parameter> <Parameter>--allow-file-access</Parameter> <Parameter>--enable-nodejs</Parameter> <Parameter>--mixed-content</Parameter> </CEFCommandLine> </Resources> <Lifecycle> <AutoVisible>true</AutoVisible> </Lifecycle> <UI> <Type>${type}</Type> ${name} <Geometry> <Size> <Height>${size[0]}</Height> <Width>${size[1]}</Width> </Size> <MaxSize> <Height>800</Height> <Width>1200</Width> </MaxSize> <MinSize> <Height>50</Height> <Width>210</Width> </MinSize> </Geometry> </UI> </DispatchInfo> </Extension>`
}
简单对上面的代码做一个解释
-
extobj
定义窗口的参数,如果增删,改这里就行 -
initWin
函数会检查ae中,是否存在我需要的窗口,如果不存在,就会构建完整的csxs数据,然后写入文件