This is a bit tricky because activeItem can either represent the comp that is currently active, or an item in the project panel if that is active and selected. In the second case, activeItem returns null if multiple items are selected in the project panel, so then you would have to loop through app.project.items looking for any that are .selected and seeing if they were instanceof CompItem.
Here is some example code but it really depends on what exactly you're trying to do. I guess you could assume that if a comp is active (either one comp only selected in the project panel, or an actually active comp) and that no layers are selected in that comp, then that's the name you're after, otherwise if a layer is selected and its source is a CompItem then that's the name you're after.
Code: Select all
var activeItem = app.project.activeItem;
if (activeItem != null && activeItem instanceof CompItem) {
alert(activeItem.name + " is active comp");
for (var x = 0; x < activeItem.selectedLayers.length; x++) {
if (activeItem.selectedLayers[x].source instanceof CompItem) {
alert("Precomp layer: " + activeItem.selectedLayers[x].source.name + " is selected");
}
}
}