Skip to content

Instantly share code, notes, and snippets.

@zlovatt
Last active September 20, 2022 03:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zlovatt/24dfed5a894c1066f905c3d0325bdd2b to your computer and use it in GitHub Desktop.
Save zlovatt/24dfed5a894c1066f905c3d0325bdd2b to your computer and use it in GitHub Desktop.
Extendscript: Gets currently open comp from timeline, as opposed to selected comp in project panel
(function () {
/**
* Gets currently open comp from timeline, as opposed to
* selected comp in project panel
*
* @returns {CompItem | null} Active comp, or null if none
*/
function getFocusedComp() {
var activeItem = app.project.activeItem;
// If there is no active item, or it's not a comp, return null.
if (!(activeItem && activeItem instanceof CompItem)) {
return null;
}
// If there are multiple items selected in the project panel,
// activeItem will always be the active comp -- so we found it.
if (app.project.selection.length !== 1) {
return activeItem;
}
// If there's only one, let's add a null to the layer and see
// whether the null was added to the same comp as activeItem
// If so, then we know that the focused comp is the activeItem
var numLayers = activeItem.numLayers;
app.executeCommand(2767); // 'add null object'
if (numLayers !== activeItem.numLayers) {
app.executeCommand(16); // undo (removes 'null object')
return activeItem;
}
}
var focusedComp = getFocusedComp();
if (focusedComp === null) {
$.writeln("No comp in focus! Assume focus is project panel.");
} else {
$.writeln("Focused comp: " + focusedComp.name);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment