S.R. Wild

Scripting Guides in After Effects

Screenshot of guides in an After Effects composition

I wanted an easier way to make guides in After Effects. I also wanted to learn more about scripting in Adobe apps. The best way to learn something is to make something. And, making a tool you’d use is very rewarding.

The script I wrote is similar to Photoshop’s Guide Layout. It creates bleed, margin, and center guides. Most of the time that’s all I need. The bleed and margin positions can be modified by changing bleed and margin variables (units are in pixels).

Once you have have a guide layout it can be exported as a Guide Template and imported to other projects. To export select View > Export Guides… and to import select View > Import Guides…. I find using the script easier because I can assign it to a shortcut.

var canvas = app.project.activeItem;
var height = canvas.height;
var width = canvas.width;
var canvasSize = [height, width];

// Guide positions (in pixels)
var bleed = 20; // Off canvas
var margin = 40; // Inside margin

function makeGuides() {
// For each axis (horizontal then vertical)
for (var axis = 0; axis < canvasSize.length; axis++) {
// Guides from top to bottom then left to right
var guides = [
-bleed, // top or left off canvas bleed
margin, // top or right margin
canvasSize[axis] / 2, // horizontal or vertical center
canvasSize[axis] + bleed, // bottom or right margin
canvasSize[axis] - margin // bottom or right off canvas bleed
];
// Make a guide for each position in the list
for (var guide = 0; guide < guides.length; guide++) {
canvas.addGuide(axis, guides[guide]);
}
}
}

// Wrap in an Undo Group to allow undoing and redoing of guides
app.beginUndoGroup('Canvas Guides'); // Message to show for Undo and Redo
clearOutput();
makeGuides(); // Make the guides
write('Enjoy your guides!'); // Confirmation message to show in Info Window
app.endUndoGroup();

Code is also available as a GitHub Gist.

All Adobe Creative Cloud apps support scripting. A few languages can be used. I used JavaScript because I use it every day. Adobe calls it ExtendScript, their extended implementation of JavaScript. It’s important to note that this is different than using JavaScript for expressions in After Effects, a fun and important part of using the app well. ExtendScript is for programing an app. For example, batch resizing images, creating patterns, or creating new documents with specific settings in Photoshop. Most apps ship with a few examples, usually located in the menu bar under File > Scripts.

Playing around with scripting in Adobe apps is a good way to get started in programming (DrawBot is also good). Don’t worry, you won’t break your computer, although you will make mistakes and break your scripts. I accidentally made an infinite loop when I was refactoring this script. You learn a lot from mistakes.

For more information look at Adobe’s Documentation or consult your favorite search engine.

I hope you found this useful or at least a little interesting. It’s a little tip to get me started writing more. If you need help, contact me. I’m not an expert, but I do like helping people and it also helps me learn. Plus, I don’t know many people who are interested in these things.