Examples
This section describes some of the sample scripts available from the File > Scripts menu, giving an overview of what they do and a description of how they work.
This set of examples is by no means exhaustive, but it does demonstrate some of scripting’s more complex features in action. It also shows some typical programming constructions from JavaScript that apply to scripting.
For more examples from Adobe and from other After Effects users, visit Adobe Exchange, and choose the Scripts category in the Adobe After Effects section.
New render locations
This script, Change Render Locations.jsx, allows the user to select queued items in the render queue and assign a new render destination for them.
This script does the following:
- Prompts the user for a new folder to use as a render destination.
-
Checks that the user entered a new location (and didn’t cancel), then creates a loop for each selected render queue item, and for each output module in it.
-
If an item is queued, gives the current render location a new name and location, and displays an alert stating the new file path.
Smart import
This script, Smart Import.jsx, allows the user to import the full, nested contents of a folder just by selecting it. It attempts to detect whether each item is a still, moving footage, or an image sequence. The user still has to make other choices in dialog boxes, such as which layer of a multi-layer image (such as a PSD file) to import.
This script does the following:
- Prompts the user for a folder whose contents are to be imported, and checks that the user chooses a folder rather than cancelling.
- Defines a function, processFolder(), to import each of the files in the chosen folder, which uses several helper functions.
- Defines a helper function, testForSequence(), to test whether a given file is part of a sequence. This uses regular expressions, which are a special type of JavaScript designed to reduce the number of steps required to evaluate a string.
The first one tests for the presence of sequential numbers anywhere in the file name, followed by another making certain that the sequential files aren’t of a type that can’t be imported as a sequence (moving image files). The function then checks adjacent files to see if a sequence exists, stopping after we’ve evaluated ten files to save processing time.
If no match is found for a number string, assumes there is no image sequence and checks for an array consisting of the matched string and its location within the file name.
If all files are part of a numbered sequence, assumes a sequence and returns the first file of that sequence.
- Defines a helper function to pop up error dialog boxes if there is a problem with any file we are attempting to import.
- Defines a helper function to actually import any image sequence discovered using testForSequence(). There is an option for forcing alphabetical order in sequences, which is commented out in the script as written. If you want to force alphabetical order, uncomment the line importOptions.forceAlphabetical = true.
- Calls the main function, processFolder().
Render and e-mail
This script, Render and Email.jsx, renders all queued items in an open project and sends an e-mail report to indicate when the render has completed. It makes use of two other scripts that follow, email_methods.jsx (to send the e-mail properly) and Change Email Settings.jsx (which establishes the sender, recipient, and e-mail server); these two scripts are in the (support) subfolder of the Scripts folder on disk.
This script does the following:
- Establishes conditions under which the script will run. An open project with at least one item queued is required.
- Checks whether e-mail settings are already saved in the preferences. If not, run the Change Email Settings.jsx script (via File > Scripts > Run Script File), which prompts the user for the mail gateway and sender and recipient addresses. (If there are saved settings that you need to change, you can always run the script to make new settings that overwrite the existing ones.)
- Render the items in the render queue.
- When rendering is complete, creates a text string for the e-mail message that contains the start time of the render, the render time of each item in the queue, and the total render time.
- E-mails the message, using the settings (such as the server) from the email_methods.jsx script
- Displays an error if for any reason it is unable to send the mail.
A helper script, email_methods.jsx, creates an e-mail object, using the ExtendScript Socket object. For details of that utility, see the Creative Suite 6 JavaScript Tools Guide. Another helper script, email_setup.jsx, prompts the user for the server name, e-mail sender, and e-mail recipient that are saved as Settings. You can run this script as standalone any time you want to change the settings. This script is a good example of how to create settings that are saved in preferences for the sole use of scripting (as opposed to altering existing After Effects preferences settings).
Convert selected properties to markers
This script, Convert Selected Properties to Markers.jsx, goes through the properties in each layer that are currently selected in the Timeline panel, and converts the value of each property at each frame time to a Flash Video event cue point in a marker.
This script adds a layer-time marker on the layer at the same time as each keyframe for each selected property. Each marker is associated with an event-type Flash Video cue point, and the cue point is given a parameter whose name is the name of the property and whose value is the property’s value at that time. If the selected property has an expression, a marker is created for each frame, with the values sampled at each frame.
Note: This script does not convert properties that have complex value types, such as the Path property for a paint stroke, the Curves property of a Curve effect, or a gradient property.
When you render the composition as Flash video, all markers that contain cue-point data are converted to Flash Video cue points.