Skip to main content
Inspiring
February 19, 2025
Answered

How to Allow Overwriting When Adding a File to the Render Queue via Script?

  • February 19, 2025
  • 1 reply
  • 447 views

Hi everyone,

I’m writing a script to automate rendering in Adobe and using addDLToBatch to add files to the render queue. However, if the output file already exists, rendering does not proceed, and I need to manually change the filename or remove the existing file.

Here’s the code snippet I’m using:

var frontend = app.getFrontend();
if (frontend) {
  frontend.addEventListener('onItemAddedToBatch', function (eventObj) {
    $.writeln('frontend.onItemAddedToBatch: success');
  });

  var batchItemSuccess = frontend.addDLToBatch(
    projFile.fsName,
    format,
    preset,
    comp.dynamicLinkGUID,
    file.fsName
  );

  if (batchItemSuccess) {
    $.writeln('Successfully added: ' + myFile);
    var encoderHost = app.getEncoderHost();
    if (encoderHost) {
      encoderHost.addEventListener('onItemEncodeComplete', function (eventObj) {
        $.writeln('Result: ' + eventObj.result);
        $.writeln('Source: ' + eventObj.sourceFilePath);
        $.writeln('Output: ' + eventObj.outputFilePath);
      });
    } else {
      alert('encoderHost not found');
    }
  } else {
    alert('Error adding to render queue');
  }
} else {
  alert('frontend not found');
}

 

 

Correct answer SükriyeLudwig

Hi again in that case, I would recommend to have some utils methods to do this. 

// The projectPath can be a path to an AfterEffects, Premiere Pro or Character Animator project
var format = "H.264";
var projectPath = "/Users/Shared/testdata/addDLTest.prproj";
var preset = "/Users/Shared/testdata/HighQuality1080HD.epr";
var destination = "/Users/Shared/testdata/file.mp4";

function fileExists(path) {
    var file = new File(path);
    if (!file) {
        return false;
    }

    if (file.open("r")) {
        file.close();
        return true;
    }
    return false;
}

function removeFile(path) {
    var file = new File(path);
    if (file.exists) {
        file.remove();
    }
}

var frontend = app.getFrontend();
if (frontend) {
    // first we need the guid of the e.g. ae comps or ppro sequences
    var result = frontend.getDLItemsAtRoot(projectPath);
    $.writeln(result.length + " comps / sequences found.");

    // import e.g. the first comp / sequence
    if (result.length > 0) {
        // listen for batch item added / creation failed event
        frontend.addEventListener("onItemAddedToBatch", function (eventObj) {
            $.writeln("frontend.onItemAddedToBatch: success");
        });

        frontend.addEventListener(
            "onBatchItemCreationFailed",
            function (eventObj) {
                $.writeln("frontend.onBatchItemCreationFailed: failed");
                $.writeln("srcFilePath: " + eventObj.srcFilePath);
                $.writeln("outputFilePath: " + eventObj.outputFilePath);
                $.writeln("error: " + eventObj.error);
            }
        );

        if (fileExists(destination)) {
            $.writeln("File found:");
            removeFile(destination);
        }

        // add the comp / sequence to the batch
        var encoderWrapper = frontend.addDLToBatch(
            projectPath,
            format,
            preset,
            result[0],
            destination
        );

        // get encoder host to run batch
        var encoderHost = app.getEncoderHost();
        if (encoderHost) {
            encoderHost.runBatch();
        } else {
            $.writeln("encoderHost not valid");
        }
    } else {
        $.writeln("the project doesn't have any comps / sequences");
    }
} else {
    $.writeln("frontend not valid");
}

1 reply

Community Manager
February 19, 2025

Hi,
you can setup AME to increment output file name if file name already exists.

 

Other that than you can setup your destination:

var destination = "/Users/Shared/testdata/file2";  or 
var destination = "/Users/Shared/testdata/file2.mp4";

if the checkbox is enabled ME will take care of it and will not overwrite it. 
 
Best, 
-Sue
 
HarchenkoAuthor
Inspiring
February 19, 2025

Hi.
Thank you for your response.

But I don't need to change the filename; I need the file to be overwritten.

I want to overwrite files. However AME, it gives a rendering error because the files already exist.

I have to manually click on each file and allow overwriting for it to work.

 



SükriyeLudwigCommunity ManagerCorrect answer
Community Manager
February 20, 2025

Hi again in that case, I would recommend to have some utils methods to do this. 

// The projectPath can be a path to an AfterEffects, Premiere Pro or Character Animator project
var format = "H.264";
var projectPath = "/Users/Shared/testdata/addDLTest.prproj";
var preset = "/Users/Shared/testdata/HighQuality1080HD.epr";
var destination = "/Users/Shared/testdata/file.mp4";

function fileExists(path) {
    var file = new File(path);
    if (!file) {
        return false;
    }

    if (file.open("r")) {
        file.close();
        return true;
    }
    return false;
}

function removeFile(path) {
    var file = new File(path);
    if (file.exists) {
        file.remove();
    }
}

var frontend = app.getFrontend();
if (frontend) {
    // first we need the guid of the e.g. ae comps or ppro sequences
    var result = frontend.getDLItemsAtRoot(projectPath);
    $.writeln(result.length + " comps / sequences found.");

    // import e.g. the first comp / sequence
    if (result.length > 0) {
        // listen for batch item added / creation failed event
        frontend.addEventListener("onItemAddedToBatch", function (eventObj) {
            $.writeln("frontend.onItemAddedToBatch: success");
        });

        frontend.addEventListener(
            "onBatchItemCreationFailed",
            function (eventObj) {
                $.writeln("frontend.onBatchItemCreationFailed: failed");
                $.writeln("srcFilePath: " + eventObj.srcFilePath);
                $.writeln("outputFilePath: " + eventObj.outputFilePath);
                $.writeln("error: " + eventObj.error);
            }
        );

        if (fileExists(destination)) {
            $.writeln("File found:");
            removeFile(destination);
        }

        // add the comp / sequence to the batch
        var encoderWrapper = frontend.addDLToBatch(
            projectPath,
            format,
            preset,
            result[0],
            destination
        );

        // get encoder host to run batch
        var encoderHost = app.getEncoderHost();
        if (encoderHost) {
            encoderHost.runBatch();
        } else {
            $.writeln("encoderHost not valid");
        }
    } else {
        $.writeln("the project doesn't have any comps / sequences");
    }
} else {
    $.writeln("frontend not valid");
}