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

remove active comp

Explorer ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

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

Views

1.2K

Translate

Translate

Report

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

Votes

Translate

Translate
Explorer ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Try this:

 

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

 

Dan

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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");

Votes

Translate

Translate

Report

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