Skip to main content
Participating Frequently
December 1, 2023
Question

Script causes AE to Crash - any advice?

  • December 1, 2023
  • 1 reply
  • 272 views

I've been working on a script that would go and replace all media with a user-specified file extention with placeholders. The script works, but not long after the script completes, AE will crash. I'm not seeing what the script is doing that couled cause this. I was hoping someone might have a suggestion?

 

And, the showMsg function is because when I use alert, the message would not appear. But using my custom message box function generally works. (Not always though!)

 

----

 

var w = new Window("palette", "Offline Media", undefined);
w.add ('statictext', undefined, "Enter media file suffix (e.g., mxf):");
var nameWindow = w.add ("edittext");
nameWindow.characters = 30;
var sb = w.add("button", undefined, "Start");

w.show();

sb.onClick = offlineFunction;

function offlineFunction() {
  try {
    var name = nameWindow.text.toLocaleLowerCase();
    var count = 0;

    for (var i = 1; i <= app.project.numItems; i++) {
      var footageItem = app.project.items[i];
      if (footageItem instanceof FootageItem) {
        var filename = footageItem.file.name;
        var idx = filename.lastIndexOf(".");
        if (idx > 0) {
          var suffix = filename.slice(idx+1).toLocaleLowerCase();
          if (suffix.localeCompare(name) == 0) {
            count++;
            fr = footageItem.frameRate;
            if (fr < 1) fr = 1;
            dur = footageItem.duration;
            if (dur < 1) dur = 1;
            footageItem.replaceWithPlaceholder(filename, footageItem.width, footageItem.height, fr, dur); // name, width, height, frameRate, duration)
         }
      }
    }
  } 
  var msg = "Offlined " + count + " media items.";
  showMsg(msg);
  } catch (e) {
    var msg = "Exception! " + e;
    showMsg(msg);
  }
}

 

function showMsg(msg) {
  var myWindow = new Window ("dialog");
  var myMessage = myWindow.add ("statictext");
  myMessage.text = msg;
  myWindow.add("button", undefined, "OK").onClick = function(){myWindow.close();}
  myWindow.show ( );
}

This topic has been closed for replies.

1 reply

Alex White
Legend
December 2, 2023

Try this updated code:

 

var w = new Window("palette", "Offline Media", undefined);
w.add('statictext', undefined, "Enter media file suffix (e.g., mxf):");
var nameWindow = w.add("edittext");
nameWindow.characters = 30;
var sb = w.add("button", undefined, "Start");

w.show();

sb.onClick = offlineFunction;

function offlineFunction() {
    try {
        var name = nameWindow.text.toLowerCase();
        var count = 0;
        var errors = 0;

        for (var i = 1; i <= app.project.numItems; i++) {
            try {
                var footageItem = app.project.items[i];
                if (footageItem instanceof FootageItem && footageItem.file) {
                    var filename = footageItem.file.name;
                    if (filename) {
                        var idx = filename.lastIndexOf(".");
                        if (idx > 0) {
                            var suffix = filename.slice(idx + 1).toLowerCase();
                            if (suffix === name) {
                                count++;
                                var fr = footageItem.frameRate > 0 ? footageItem.frameRate : 1;
                                var dur = footageItem.duration > 0 ? footageItem.duration : 1;
                                footageItem.replaceWithPlaceholder(filename, footageItem.width, footageItem.height, fr, dur);
                            }
                        }
                    }
                }
            } catch (innerError) {
                errors++;
                $.writeln("Error processing item " + i + ": " + innerError.toString());
            }

            // Pause every 10 items to reduce strain on AE
            if (i % 10 === 0) {
                $.sleep(100); // Sleep for 100 milliseconds
            }
        }
        var msg = "Offlined " + count + " media items. Errors: " + errors;
        showMsg(msg);
    } catch (e) {
        showMsg("Exception! " + e.toString());
    }
}

function showMsg(msg) {
    var myWindow = new Window("dialog");
    var myMessage = myWindow.add("statictext");
    myMessage.text = msg;
    myWindow.add("button", undefined, "OK").onClick = function () {
        myWindow.close();
    }
    myWindow.show();
}
Participating Frequently
December 4, 2023

Interesting! That seems to help. Although I'm not sure why. 🙂 So, (given that no exceptions actually occured), you're bacically just adding in a little "breathing room" with a sleep? Do you know why that makes a difference?
Your help is much appreciated! I'd have never thought that would be needed in a script.