Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

remove active comp

Explorer ,
May 21, 2020 May 21, 2020

Hi,

I am trying to remove the active comp from my project using JavaScript but it doesn't really work. It executes the script but instead of removing it, it's just switching the order....Not sure what I am doing wrong. Can anyone share what is the correct way to delete a comp from a project?

app.project.activeItem.remove.CompItem;

Thank you very much! 

TOPICS
Scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 21, 2020 May 21, 2020

I figured something that works in the meantime. If anyone has a better solution, please share. My working code below:

function deleteComp(){
for (var i = 1; i <= app.project.numItems; i ++) {
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === 'myComp')) {
        app.project.item(i).remove();
        break;
    }
}
}

 Thanks

Translate
Explorer ,
May 21, 2020 May 21, 2020

I figured something that works in the meantime. If anyone has a better solution, please share. My working code below:

function deleteComp(){
for (var i = 1; i <= app.project.numItems; i ++) {
    if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === 'myComp')) {
        app.project.item(i).remove();
        break;
    }
}
}

 Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

Try this:

 

var myItem = app.project.activeItem;
if (myItem && myItem instanceof CompItem ) myItem.remove();

 

Dan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 21, 2020 May 21, 2020

Thank you Dan!

Would this work if I want to actually target a specific comp name?

I know my initial question was about removing the active comp but I realized that I actually need to select which comp I want to remove....sorry for any confusion.

Thank you again!

A.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

You can't directly access a comp by name--you have to loop through the items looking for a comp with that name. So what you came up with looks pretty good..

 

Dan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 22, 2020 May 22, 2020
LATEST

I think you should pass the comp name as parameter to the function, so you don't need to change the function every time you need another composition deleted.

 

function deleteComp(compName) {
    for (var i = 1; i <= app.project.numItems; i++) {
        if (app.project.item(i) instanceof CompItem && app.project.item(i).name === compName) {
            app.project.item(i).remove();
            break;
        }
    }
}

 This way, you can use this function multiple times in your project, to delete different comps. Like this:

deleteComp("First one");

deleteComp("Second one");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines