(function() {
var shotDuration = 30;
try {
app.beginUndoGroup('precompose shots');
forEachSelectedProjectItem(function(item) {
if (!item.file) {
return
}
var shotItems = getShotItems(item.name);
precomposeShots(shotItems);
});
app.endUndoGroup();
} catch (error) {
alert(error)
}
function forEachSelectedProjectItem(callback) {
var item, selection;
selection = app.project.selection;
for (var i = 0, il = selection.length; i < il; i++) {
item = selection;
callback(item)
}
}
function getShotItems(itemName) {
var angles, shotData, shotItem, shotItems, shotToFind;
shotData = parseShotDataFromString(itemName);
shotItems = [];
angles = ['A', 'B'];
for (var i = 0, il = angles.length; i < il; i++) {
shotToFind = angles + shotData.number;
shotItem = findItemByName(shotToFind)
if (!shotItem) {
throw new Error('Could not find shot by name ' + shotToFind);
}
shotItems.push(shotItem);
}
return shotItems;
function findItemByName(name) {
var item;
for (var i = 1, il = app.project.numItems; i <= il; i++) {
item = app.project.item(i);
if (item.name === name) {
return item;
}
}
}
}
function precomposeShots(shots) {
var composition = buildCompForItem(shots[0]);
for (var i = 0, il = shots.length; i < il; i++) {
addItemIntoComposition(shots, composition, i);
}
function buildCompForItem(item) {
var composition, name;
name = parseShotDataFromString(item.name).number;
composition = app.project.items.addComp(name, item.width, item.height, item.pixelAspect, 60, item.frameRate);
composition.openInViewer();
return composition;
}
function addItemIntoComposition(item, composition, index) {
var layer, textLayer;
layer = composition.layers.add(item);
textLayer = addText(composition, item.name);
layer.startTime = index * shotDuration;
textLayer.startTime = layer.startTime;
layer.outPoint = layer.startTime + shotDuration;
textLayer.outPoint = layer.outPoint;
composition.duration = layer.outPoint;
function addText(composition, text) {
var textDocument, textLayer, textValue;
textLayer = composition.layers.addText(text);
textLayer.name = text;
textDocument = textLayer.property("ADBE Text Properties").property("ADBE Text Document");
textValue = textDocument.value;
textValue.font = "Arial-BoldMT";
textValue.fontSize = 80;
textValue.fillColor = [1, 1, 1];
textValue.justification = ParagraphJustification.LEFT_JUSTIFY;
textDocument.setValue(textValue);
textLayer.property('ADBE Transform Group').property('ADBE Position').setValue([
50,
composition.height - 50
]);
return textLayer;
}
}
}
function parseShotDataFromString(string) {
var angle, number;
angle = string.match(/[a-zA-Z]+/g);
if (angle) {
angle = angle[0];
}
number = string.match(/\d+/g);
if (number) {
number = number[0];
}
if (!angle || !number) {
throw new Error('Could not parse angle or number from ' + string);
}
return {
angle: angle,
number: number,
}
}
})();