New Background Layer with Clipboard Color

Cavalry
September 29, 2025

This Cavalry script is a small time-saver for a common workflow: adding a solid background that matches a chosen color. Instead of manually creating a shape, setting its size, and picking a fill, the script handles the whole process in one step.

Here’s what it does:

  • Reads a hex value from the clipboard and checks it’s a valid 6-digit color.
  • Converts that value into RGBA channels.
  • Creates a background shape in the active comp.
  • Connects or sets the shape’s resolution so it matches the comp size.
  • Applies the chosen color directly to the material.
  • Sends the layer to the back so it sits neatly behind everything else.

Simple, quick, and keeps your design flow moving.

If you appreciate it and want to support my work – buy me a coffee!

/*
  Fill with clipboard.js
  Reads a hex color from the clipboard and applies it to a Background Shape in the active comp.
  The clipboard contents are used only locally and never shared or exposed.
  Creates the shape, connects or sets resolution, applies color, and sends it to the back.
*/

var compId = null;
try { compId = api.getActiveComp(); } catch (_) {}

if (!compId) {
  console.warn("No active composition found.");
} else {
  // Read & validate hex from clipboard
  var raw = "";
  try { raw = (api.getClipboardText() || "").trim(); } catch (_) { raw = ""; }

  // Accept exactly 6 hex digits, with or without '#'
  var m = raw.match(/^#?([0-9a-fA-F]{6})$/);
  if (!m) {
    console.warn("Clipboard does not contain a 6-digit hex color. Aborting.");
    return;
  }
  var hex = "#" + m[1];

  function parseHex(h) {
    return {
      r: parseInt(h.slice(1, 3), 16),
      g: parseInt(h.slice(3, 5), 16),
      b: parseInt(h.slice(5, 7), 16),
      a: 255
    };
  }
  var col = parseHex(hex);

  // Create Background Shape
  var bgId = null;
  try { bgId = api.create("backgroundShape", "Background Shape"); } catch (e) {
    console.error("Failed to create Background Shape.");
  }
  if (!bgId) return;

  // Parent to comp
  try { api.parent(bgId, compId); } catch (_) {}

  // Try to connect resolution; if not supported, set directly
  var connected = false;
  try { api.connect(compId, "resolution", bgId, "resolution"); connected = true; } catch (_) {}
  if (!connected) {
    try {
      var w = api.get(compId, "resolution.x");
      var h = api.get(compId, "resolution.y");
      if (typeof w === "number" && typeof h === "number") {
        api.set(bgId, { "resolution.x": w, "resolution.y": h });
      }
    } catch (_) {}
  }

  // Set fill color
  try {
    api.set(bgId, {
      "material.materialColor.r": col.r,
      "material.materialColor.g": col.g,
      "material.materialColor.b": col.b,
      "material.materialColor.a": col.a
    });
  } catch (_) {
    console.error("Failed to set background color.");
  }

  // Send to back (bounded)
  try {
    api.select([bgId]);
    api.moveToBack();
  } catch (_) {
    try {
      var parent = api.getParent(bgId) || compId;
      var tries = 0;
      while (tries < 200) {
        var kids = api.getChildren(parent) || [];
        var idx = kids.indexOf(bgId);
        if (idx <= 0) break;
        api.moveBackward();
        tries++;
      }
    } catch (_) {}
  }

  console.log("Background created and sent to back.");
}

How to use the script

Always check code before running it on your computer. Even basic scripts like this one. If you’re not a developer, ask an LLM like ChatGPT, Claude, or Gemini to verify it for you.

Option 1: Install to your AE Folder

Download and install the below .jsx file in your After Effects Script folder. You can then run it using the scripts menu, or by using a launcher like Quick Menu 3.

Option 2: Adapt the script yourself

Alternatively copy the code below and develop your own version. If you’re not a developer you can do this easily with an LLM like ChatGPT.

Research, interviews, and tools for modern motion designers.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.