Search code examples
extendscriptafter-effects

I'm stumped, AE Extendscript issue with for each loop


So, I've run into an issue here that has me sort of perplexed. To give you an idea of what I'm trying to accomplish, I'm executing this from After Effects, to get a path for images in a directory, and then write a text file that holds the path for each image as a new line. Currently everything works almost exactly as I want it, and the issue is coming down to how AE lists a file path. I feel like I'm missing something simple, but here's the chunk of code I'm having an issue with:

var saveTextFile = File(savePath + "images.txt");

if(saveTextFile.exists)
    saveTextFile.remove();

saveTextFile.encoding = "UTF8";
saveTextFile.open("e", "TEXT", "????");

var files = Folder (savePath).getFiles("*.PNG");
    if (files.length == 0) return;

        for each (var file in files){
            //var drive = '/x';
            //var fixName = fileName.replace(drive, 'X:');
            //name = fixName.toString();
            //$.writeln(name)
            saveTextFile.writeln(('file ' + "'" + file.toString() + "'"));
            }
saveTextFile.close();

The issue exists in the for each (var file in files) section. If I run it as is, I end up with a path similar to what's listed here:

file '/x/_CURRENT_/sequence_PNG_00000.png'
file '/x/_CURRENT_/sequence_PNG_00001.png'
file '/x/_CURRENT_/sequence_PNG_00002.png'
file '/x/_CURRENT_/sequence_PNG_00003.png'
file '/x/_CURRENT_/sequence_PNG_00004.png'
file '/x/_CURRENT_/sequence_PNG_00005.png'

Now this is great, except for the fact that it's reading the drive letter as "/x". This is problematic, so If I uncomment the variables in the for each loop, I end up with something similar to this:

file 'X:/_CURRENT_/sequence_PNG_00000.png'
file 'X:/_CURRENT_/sequence_PNG_00000.png'
file 'X:/_CURRENT_/sequence_PNG_00000.png'
file 'X:/_CURRENT_/sequence_PNG_00000.png'
file 'X:/_CURRENT_/sequence_PNG_00000.png'
file 'X:/_CURRENT_/sequence_PNG_00000.png'

And so that's great because it formats the X drive properly in the string.... But alas, it renames the incremental number in the string to 00000.png for every image.

Can anyone spot what I might be overlooking?


Solution

  •    for each (var file in files){
    

    does not look like valid JS syntax. Also Extendscript is ES3 so there is no [1,2,3].forEach(function(ele,i,arr){}) either.

    Try it with

    for(var i = 0; i < files.length;i++){
        var file = files[i];
        // ...and so on
    }