Calling the app (ExtendScript)
Call ExtendScript functions in After Effects or Premiere Pro from a panel with AP.host.eval, write jsx/ functions in ES3 that return JSON, keep app-only code in jsx/aeft/ and jsx/ppro/, and handle host errors.
Panel code runs in a browser. Anything that touches the app (the project, its
compositions or sequences, layers or clips, files through File/Folder) runs
in ExtendScript, in jsx/. AP.host.eval connects the two. It works the same
in After Effects and Premiere Pro. What you can do in each app:
- host-after-effects.md: After Effects
- host-premiere.md: Premiere Pro
Where JSX lives
Section titled “Where JSX lives”Put ExtendScript in jsx/, one file per feature (for example
jsx/layers.jsx; subfolders are fine). Every .jsx file there is loaded into
the app once, before your app starts, and its functions are shared by
all panels. Call one from panel code with ctx.host.eval("fnName", [args], cb).
Two folders are special (afterpresets.json hosts lists the apps):
| Folder | Loaded in |
|---|---|
jsx/aeft/ |
After Effects only |
jsx/ppro/ |
Premiere Pro only |
everything else in jsx/ |
every app the extension targets |
An extension for both apps can define the same function in jsx/aeft/ and
jsx/ppro/ (for example myTool_activeItemName) and call it from shared panel
code; each app loads its own version. ap check warns about a folder for an
app the project does not target.
AP.host.app (also ctx.host.app) is the app running the panel: "AEFT"
(After Effects) or "PPRO" (Premiere Pro). Use it for wording that differs
between the apps, or to hide a feature that only one app has:
var noun = ctx.host.app === "PPRO" ? "sequence" : "composition";- There is no list to keep: the CLI joins every
.jsxfile in alphabetical path order, with JSON support (json2) first, embeds the result in every panel’smain.js(readable inap dev, compiled to JSXBIN for releases), and the runtime evaluates it as soon asAP.bootruns, beforestart. EveryAP.host.evalwaits for that load. - Order only matters for code that runs at the top level of a file while it loads (not inside a function). Keep files to function declarations.
Writing a JSX function
Section titled “Writing a JSX function”ExtendScript is ES3: var only, no let/const, no arrow functions, no
Array.prototype.forEach/map/indexOf, no trim(), no template strings.
JSON is always available (the CLI adds json2).
Rules:
- Declare a named top-level function. Avoid generic names; prefix them
(
myTool_addNull), because every extension shares one ExtendScript engine. - Take JSON-compatible arguments (strings, numbers, booleans, arrays, plain objects).
- Return
JSON.stringify(...). Return a result object such as{ ok: true, ... }or{ ok: false, message: "..." }for expected problems. - Undo: in After Effects wrap changes in one undo group; Premiere Pro has none (every change is its own undo step). See the app pages.
/* jsx/aeft/layers.jsx (After Effects) */function myTool_addNull(name) { var comp = app.project.activeItem; if (!comp || !(comp instanceof CompItem)) { return JSON.stringify({ ok: false, message: "Select a composition first." }); } app.beginUndoGroup("Add null"); try { var layer = comp.layers.addNull(); layer.name = name; return JSON.stringify({ ok: true, index: layer.index }); } finally { app.endUndoGroup(); }}Calling it from the panel
Section titled “Calling it from the panel”AP.host.eval("myTool_addNull", ["Controller"], function (result) { if (!result || result.error) { showError(result ? result.error : "No answer from the app."); return; } if (!result.ok) { showMessage(result.message); return; } showMessage("Added layer " + result.index);});AP.host.eval(fnName, args, cb):
fnNameis a function name or dotted path ("myTool.addNull"). Anything else is refused with{ error }.argsis an array. Each value is passed as a JSON literal, so quotes, newlines, and code in strings are safe.undefinedbecomesnull.cb(result)receives the parsed JSON your function returned,nullwhen it returned nothing, or{ error: "message" }when anything failed.
Never build ExtendScript source by joining strings with user input. Always pass
values through args.
Errors
Section titled “Errors”result |
Cause |
|---|---|
{ error: "<message>" } |
Your function threw; the message is the thrown error’s message. |
{ error: "...is undefined" } or similar |
The function does not exist: check the name, that its file is in jsx/ and ends in .jsx, and that it is not in another app’s folder (jsx/aeft/, jsx/ppro/). |
{ error: "The host returned a non-JSON result.", raw } |
Your function returned a plain string. Return JSON.stringify(...). |
{ error: "The host script failed to run." } |
ExtendScript syntax error or an ES5+ feature in jsx/. |
{ error: "Could not load host script: ..." } |
The embedded JSX failed to load. Read the message; usually a syntax error. “This extension has no host script for X” means the build has no code for the running app (afterpresets.json hosts). |
The JSX loads once per page. During ap dev every save rebuilds and reloads
the open panels, which loads the new JSX too; after an update,
AP.updates.reload() does the same. The new functions replace the old ones.
Functions removed from the new code stay defined in the app until it
restarts, so never rely on a function being absent.
Testing
Section titled “Testing”Call the function from the live panel and check the result and the project
state. Use a disposable project (ap host scratch) for
anything that changes the project. ap host eval runs in the
app that runs the connected panel.