Yes — your indentation is deceptive. You are creating pComp and attempting to move it to pMedia inside your loop. Unless the pMedia folder is the very first item in the project, this will fail; creating pComp needs to happen outside the loop, after you’ve identified the pMedia folder.
See this revision. (I’ve also renamed your ‘comp’ variable, because reading ‘comp instanceof FolderItem’ made me do a double-take, I’ve added a little error checking in case the folder isn’t found, and I’ve added comments to explain what’s going on throughout.)
app.beginUndoGroup("Create pComp in pMedia");
// create a variable to store our media folder
var pMedia = null;
// step through the project, looking for the pMedia folder
for (var i = 1; i <= app.project.numItems; i++) {
var projectItem = app.project.item(i);
// check the current project item to see if it's a folder named 'pMedia'
if ((projectItem.name == "pMedia") && (projectItem instanceof FolderItem)) {
// we found it, so let's store it in pMedia, then stop processing the loop
pMedia = projectItem;
break;
};
}
// if we failed to find a pMedia folder, tell the user
// otherwise, create a new comp and put it in the pMedia folder
if (pMedia == null) {
alert("No pMedia folder found!");
} else {
var pComp = app.project.items.addComp("pComp", 1920, 1080, 1, 15, 30); // Create Comp
pComp.parentFolder = pMedia;
}
app.endUndoGroup();