Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Can’t add a file to timeline via scripting

  • Can’t add a file to timeline via scripting

    Posted by Vincenzo Imbimbo on March 18, 2024 at 4:59 pm

    Hello guys, i’m trying to add a file (mp3) wich i have imported in the after effects items panel. I want the button to add a specific file to the timeline in the current time, but everytime i click the button it says that the file is not imported in the project (but it is). Can you guys help me? Wha am i doing wrong? Here is the code:

    function addFileToTimeline(fileName) {

    var comp = app.project.activeItem; // Get the active composition

    if (!comp || !(comp instanceof CompItem)) {

    alert("Please select a composition in the project.");

    return;

    }

    var importedFile = null;

    // Loop through all items in the project to find the file by name

    for (var i = 1; i <= app.project.items.length; i++) {

    var item = app.project.items[i];

    if (item.name.indexOf(fileName) !== -1 && item instanceof FootageItem) {

    importedFile = item;

    break;

    }

    }

    if (!importedFile) {

    alert("The file '" + fileName + "' is not imported in the project.");

    return;

    }

    var layer = comp.layers.add(importedFile);

    layer.startTime = comp.time; // Set the start time of the layer to the current time

    }

    // When the "Add Woosh to Timeline" button is clicked

    panelOneButton1.onClick = function() {

    addFileToTimeline("Woosh.mp3");

    };

    Dan Ebberts
    replied 3 weeks, 5 days ago
    2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    March 18, 2024 at 7:11 pm

    Your function works for me, although I would structure the loop a little differently:

        for (var i = 1; i <= app.project.numItems; i++) {
    var item = app.project.item(i);
    if (item.name.indexOf(fileName) > -1 && item instanceof FootageItem) {
    importedFile = item;
    break;
    }
    }
  • Vincenzo Imbimbo

    March 19, 2024 at 9:42 am

    Thanks Dan, now it works!! But there is something weird tho, when i undo (ctrl+z) the file go back to the start of the timeline and i need to undo again to delete it, how can i modify the code to use just one undo?

  • Dan Ebberts

    March 19, 2024 at 3:15 pm

    You just need to enclose everything in an undo group:

    app.beginUndoGroup("Add File");
    // your code that changes stuff
    app.endUndoGroup();

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy