Hello Vasyl,
is the composition selected in the project is the same as the activeItem?
You can ‘force’ AE to make composition active, by calling:
curComp.openInViewer();
where ‘curComp’ should be comp item. Accordingly, you could grab selection from the project panel and use it for activating the viewer.
curComp = app.project.selection[0];
where selection[0] would be selected item in the project panel, assuming its composition.
Whole scenario below, assuming that your selected comp in the project panel actually matches the one which you want to add item to. If it does not necessarily matches – then slight adjustments could be made.
var curComp = app.project.selection[0];
curComp.openInViewer();
curComp.layers.addNull();
Find out more:
After Effects Tutorials: motion design, expressions, scripting.
Social Media Profiles