OverviewLoading and running scripts
6
How to include After Effects scripting in an AppleScript (Mac OS)
Following are three examples of AppleScript scripts that will send an existing JSX file containing an After Effects script to the application without using the After Effects user interface to execute the script.
In the first example, you copy your After Effects script directly into the Script Editor and then run it. The script text appears within quotation marks following the DoScript command, so internal quotes in the script must be escaped using the backslash escape character, as follows:
tell application \
DoScript \end tell
Alternatively, you could display a dialog box asking for the location of the JSX file to be executed, as follows:
set theFile to choose file
tell application \DoScript theFileend tell
Finally, this script is perhaps most useful when you are working directly on editing a JSX script and want to send it to After Effects for testing or to run. To use it effectively you must enter the application that contains the open JSX file (in this example it is TextEdit); if you do not know the proper name of the application, type in your best guess to replace “TextEdit” and AppleScript prompts you to locate it.Simply highlight the script text that you want to run, and then activate this AppleScript:
(*
This script sends the current selection to After Effects as a script.*)
tell application \
set the_script to text of front documentend tell
tell application \activate
DoScript the_scriptend tell
Running scripts automatically during application startup or shutdown
Within the Scripts folder are two folders called Startup and Shutdown. After Effects runs scripts in these folders automatically, in alphabetical order, on starting and quitting, respectively.
In the Startup folder you can place scripts that you wish to execute at startup of the application. They are executed after the application is initialized and all plug-ins are loaded.
Scripting shares a global environment, so any script executed at startup can define variables and functions that are available to all scripts. In all cases, variables and functions, once defined by running a script that contains them, persist in subsequent scripts during a given After Effects session. Once the application is quit, all such globally defined variables and functions are cleared. Be sure to give variables in scripts unique names, so that a script does not inadvertently reassign global variables intended to persist throughout a session.Attributes can also be added to existing objects such as the Application object (see “Application object” on page17) to extend the application for other scripts.
6
OverviewLoading and running scripts
7
The Shutdown folder scripts are executed as the application quits. This occurs after the project is closed but before any other application shutdown occurs.
Running scripts from the Window menu
Scripts in the ScriptUI Panels folder are available from the bottom of the Window menu. If a script has been written to provide a user interface in a dockable panel, the script should be put in the ScriptUI folder. ScriptUI panels work much the same as the default panels in the After Effects user interface.
Instead of creating a Window object and adding controls to it, a ScriptUI Panels script uses the this object that represents the panel. For example, the following code adds a button to a panel:
var myPanel = this;
myPanel.add(\
If your script creates its user interface in a function, you cannot use this as it will refer to the function itself, not the panel. In this case, you should pass the this object as an argument to your function. For example:
function createUI(thisObj) {var myPanel = thisObj;
myPanel.add(\return myPanel;}
var myToolsPanel = createUI(this);
You cannot use the File > Scripts > Run Script File menu command to run a script that refers to this. To make your script work with either a Window object (accessible from the File > Scripts menu) or a native panel (accessible from the Window menu), check whether this is a Panel object. For example:
function createUI(thisObj) {
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window(\[100, 100, 300, 300]);
myPanel.add(\return myPanel;}
var myToolsPanel = createUI(this);
Stopping a running script
A script can be stopped by pressing Esc or Cmd+period (in Mac OS) when the After Effects or the script’s user interface has focus. However, a script that is busy processing a lot of data might not be very responsive.
7
After Effects scripting reference
This chapter lists and describes JavaScript classes, objects, methods, attributes, and global functions defined by After Effects.
The After Effects scripting engine supports ExtendScript, Adobe’s extended version of JavaScript, which imple-ments the 3rd Edition of the ECMA-262 Standard, including its notational and lexical conventions, types, objects, expressions and statements. For a complete listing of the keywords and operators included with
ECMAScript, refer to ECMA-262.pdf, available at www.ecma-international.org/publications/standards/Ecma-262.htm. For an overview of the most common keywords and statements available from ECMA-262, see “JavaScript keywords and statement syntax” on page8.
Elements of basic JavaScript relevant to After Effects scripting
JavaScript variables
Scripting shares a global environment, so any script executed at startup can define variables and functions that are available to all scripts. In all cases, variables and functions, once defined by running a script that contains them, persist in subsequent scripts during a given After Effects session. Once the application is quit, all such globally defined variables and functions are cleared. Scripters should be careful about giving variables in scripts unique names, so that a script does not inadvertently reassign global variables intended to persist throughout a session.
JavaScript keywords and statement syntax
Although it is not possible to provide an exhaustive resource describing usage of JavaScript, the following tables provide an overview of keywords, statements, operators, precedence, and associativity.
The following table lists and describes all keywords and statements recognized by the After Effects scripting engine.
Table1Keywords and Statement Syntax
Description
Standard JavaScript; exit the currently executing loop.
Standard JavaScript; cease execution of the current loop iteration.Label used in a switch statement.
Label used in a switch statement when a case label is not found.
Standard JavaScript construct. Similar to the while loop, except loop condition evaluation occurs at the end of the loop.
Literal representing the Boolean false value.Standard JavaScript loop construct.
Keyword/Statement
breakcontinuecasedefaultdo...whilefalsefor
8
After Effects scripting referenceElements of basic JavaScript relevant to After Effects scripting
9
Keyword/StatementDescription
Standard JavaScript construct. Provides a way to easily loop through the properties of an object.Used to define a function.
Standard JavaScript conditional constructs.Standard JavaScript constructor statement.
Assigned to a variable, array element, or object property to indicate that it does not contain a legal value.
Standard JavaScript way of returning a value from a function or exiting a function.
Standard JavaScript way of evaluating a JavaScript expression and attempting to match the expres-sion’s value to a case label.
Standard JavaScript method of indicating the current object.Literal representing the Boolean true value.
Indicates that the variable, array element, or object property has not yet been assigned a value.Standard JavaScript syntax used to declare a local variable.
Standard JavaScript construct. Similar to the do...while loop, except loop condition evaluation occurs at the beginning of the loop.
Standard JavaScript construct used to specify an object to use in subsequent statements.
for...infunctionif/if...elsenewnullreturnswitchthistrueundefinedvarwhilewith
JavaScript operators
The following tables list and describe all operators recognized by the After Effects scripting engine and show the precedence and associativity for all operators.
Table2
Operators
Description of Operators
DescriptionAllocate object.Deallocate object.Returns data type.Returns undefined value.Structure member.Array element.Function call.
Pre- or post-increment.Pre- or post-decrement.Unary negation or subtraction.Bitwise NOT.Logical NOT.Multiply.Divide.
newdeletetypeofvoid.[]()++–––~!*/
9
After Effects scripting referenceElements of basic JavaScript relevant to After Effects scripting
10
OperatorsDescriptionModulo division.Add.
Bitwise left shift.Bitwise right shift.
Unsigned bitwise right shift.Less than.Less than or equal.Greater than.Greater than or equal.Equal.Not equal.Bitwise AND.Bitwise XOR.Bitwise OR.Logical AND.Logical OR.
Conditional (ternary).Assignment.
Assignment with add operation.Assignment with subtract operation.Assignment with multiply operation.Assignment with divide operation.
Assignment with modulo division operation.Assignment with bitwise left shift operation.Assignment with bitwise right shift operation.
Assignment with unsigned bitwise right shift operation.Assignment with bitwise AND operation.Assignment with bitwise XOR operation.Assignment with bitwise OR operation.Multiple evaluation.
%+<<>>>>><<=>>===!=&^|&&||?:=+=–=*=/=%=<<=>>=>>>=&=^=|=,
10