/*
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.");
}