Various things that are wrong here. There are various examples of this already posted on here including:
https://community.adobe.com/t5/after-effects-discussions/create-a-script-for-quot-install-scriptui-panel-quot-without-having-2-windows/m-p/13282374
- when launched as a panel, AE supplys the panel object and you have to pass 'this' into the scope of your outer function.
- you should be asking if thisObj is an instanceof a 'panel' type, not 'myPanel' which is your variable.
- you don't really want a user having a comp selected as a basis for whether the UI gets created. just create it regardless then test for the active comp when the button is clicked.
function main(thisObj) {
function build_UI(thisObj) {
var myPanel = (thisObj instanceof Panel) ? (thisObj) : (new Window("palette", "Responsive UI", undefined, { resizeable: true}));
var createButton = myPanel.add("button", undefined, "Create Random Solid");
createButton.onClick = function () {
var comp = app.project.activeItem;
if (comp != null && comp instanceof CompItem) {
var solidLayers = [];
var newSolidLayer = comp.layers.addSolid([Math.random(), Math.random(), Math.random()], "Random Solid", comp.width, comp.height, comp.pixelAspect);
newSolidLayer.moveToBeginning();
solidLayers.push(newSolidLayer);
} else {
alert("Please select a composition.");
}
};
if (myPanel instanceof Window) {
myPanel.center();
myPanel.show();
} else {
myPanel.layout.layout(true);
}
}
build_UI(thisObj);
}
main(this);