Skip to main content
djuncheg117156386
Inspiring
October 5, 2022
Answered

Need small script modification for "replacer" script

  • October 5, 2022
  • 2 replies
  • 737 views

Hi

I have a large number (hundreds) of video files where I need to do the same correction so I was looking for a way to automate this process as much as possible. And I found the Replacer script.on site oizys org
Here is its code.

/*
 After effects script
 This will take a template and add multiple compositions after replacing a source.
 It will open a fileselector
 This will replace the source of a layer. The layer should be called 'replace'. Then it will copy the composition and
 do the same for another file picked via the fileselector.
 */
{
    function SmartImport() {
        var scriptName = "Example replace";
        // Ask the user for a folder whose contents are to be imported.
        var targetFolder = Folder.selectDialog("Import items from folder...");
        if (targetFolder != null) {
            // If no project open, create a new project to import the files into.
            if (!app.project) {
                app.newProject();
            }
            function processFile(theFile) {
                try {
                    // Create a variable containing ImportOptions.
                    var importOptions = new ImportOptions(theFile);
                    try {
                        var currentItem = app.project.importFile(importOptions);
                        for (var i = 1; i <= app.project.items.length; i++) {
                            var elm = app.project.items[i];
                            // Look for the template composition
                            if (elm instanceof CompItem && elm.name === 'template') {
                                var netItem = app.project.item(i).duplicate();
                                for (var j = 1; j <= netItem.numLayers; j++) {
                                    // Replace the soure of the item called 'replace'
                                    if (netItem.layer(j).name === 'replace') {
                                        netItem.layer(j).replaceSource(currentItem, true);
                                    }
                                }
                            }
                        }
                    } catch (error) {
                        alert(error.toString() + importOptions.file.fsName, scriptName);
                    }
                } catch (error) {
                    // Ignore errors.
                }
            }
            function processFolder(theFolder) {
                // Get an array of files in the target folder.
                var files = theFolder.getFiles();
                for (index in files) { // Go through the array and set each element to singleFile, then run the following.
                    if (files[index] instanceof File) {
                        processFile(files[index]); // Calls the processFile function above.
                    }
                }
            }
            // Recursively examine that folder.
            processFolder(targetFolder);
        }
    }
    SmartImport();
}


It does what I need - im creates a project in AE with a composition "template" in which all the layers that need to be substituted are called "replace".
The script asks you to specify a folder and for each file in this folder duplicates the composition of "template", replacing the layers "replace" this file. I.e. for 50 files in folder it will create 50 copies of the composition.
What I need to improve - is the automatic renaming  of these compositions by filename of replacing file. For now its make result as composition "template2", "template3"... etc  and im need to rename each composition by hand when im export this composition. 
I want for composition where layer "replace" replaced by file  aaa.mov, create a composition not "template#" but named "aaa" or "aaa.mov" so that when I send it for export the exported file will automatically be called "aaa.mp4" as well. Its looks like easy mod which can get rid of a certain amount of pointless manual work that can be automated.
The problem is that I am a complete zero in coding and scripting. 
I tried to modify a piece of code like this:

if (elm instanceof CompItem && elm.name === 'template') {
                                var netItem = app.project.item(i).duplicate();
				app.project.item(i+1).name = currentItem.name;	
				netItem = app.project.item(i+1);			
                                for (var j = 1; j <= netItem.numLayers; j++) {

There im change its just added one string in first cycle

app.project.item(i+1).name = currentItem.name;

and its even worked...  once for one  composition (i.e. composition was created and named as "file name" instead of 
template#" but its stopped working for next try, the script caused an endless cycle of copyingcomposition. So im do something wrong. 

 I hope someone will look and show me the right line. 
Thanks

This topic has been closed for replies.
Correct answer Dan Ebberts

Ah, OK. After tinkering with the processFile function a little, it seems to work:

		function processFile(theFile) {
			try {
				// Create a variable containing ImportOptions.
				var importOptions = new ImportOptions(theFile);
				try {
					var currentItem = app.project.importFile(importOptions);
					var elm = null;
					for (var i = 1; i <= app.project.items.length; i++) {
						elm = app.project.items[i];
						// Look for the template composition
						if (elm instanceof CompItem && elm.name === 'template') break;
					}
					if (elm == null) return;
					var netItem = elm.duplicate();
					netItem.name = currentItem.name.split(".")[0];
					for (var j = 1; j <= netItem.numLayers; j++) {
						// Replace the soure of the item called 'replace'
						if (netItem.layer(j).name === 'replace') {
							netItem.layer(j).replaceSource(currentItem, true);
						}
					}
				} catch (error) {
					alert(error.toString() + importOptions.file.fsName, scriptName);
				}
			} catch (error) {
				// Ignore errors.
			}
		}

 

2 replies

djuncheg117156386
Inspiring
October 7, 2022

Thanks for Dan Ebberts script now modifed for name composition not "template" but for name of replaced file (its allow to no rename each compostion by hand). 

Full script bellow (originaly downloaded from oizys org )

/*
 After effects script
 This will take a template and add multiple compositions after replacing a source.
 It will open a fileselector
 This will replace the source of a layer. The layer should be called 'replace'. Then it will copy the composition and
 do the same for another file picked via the fileselector.
 */
{
    function SmartImport() {
        var scriptName = "Example replace";
        // Ask the user for a folder whose contents are to be imported.
        var targetFolder = Folder.selectDialog("Import items from folder...");
        if (targetFolder != null) {
            // If no project open, create a new project to import the files into.
            if (!app.project) {
                app.newProject();
            }

	function processFile(theFile) {
			try {
				// Create a variable containing ImportOptions.
				var importOptions = new ImportOptions(theFile);
				try {
					var currentItem = app.project.importFile(importOptions);
					var elm = null;
					for (var i = 1; i <= app.project.items.length; i++) {
						elm = app.project.items[i];
						// Look for the template composition
						if (elm instanceof CompItem && elm.name === 'template') break;
					}
					if (elm == null) return;
					var netItem = elm.duplicate();
					netItem.name = currentItem.name.split(".")[0];
					for (var j = 1; j <= netItem.numLayers; j++) {
						// Replace the soure of the item called 'replace'
						if (netItem.layer(j).name === 'replace') {
							netItem.layer(j).replaceSource(currentItem, true);
						}
					}
				} catch (error) {
					alert(error.toString() + importOptions.file.fsName, scriptName);
				}
			} catch (error) {
				// Ignore errors.
			}
		}

            function processFolder(theFolder) {
                // Get an array of files in the target folder.
                var files = theFolder.getFiles();
                for (index in files) { // Go through the array and set each element to singleFile, then run the following.
                    if (files[index] instanceof File) {
                        processFile(files[index]); // Calls the processFile function above.
                    }
                }
            }
            // Recursively examine that folder.
            processFolder(targetFolder);
        }
    }
    SmartImport();
}

 

Dan Ebberts
Community Expert
Community Expert
October 5, 2022

I haven't tested this, but I think after this line:

var netItem = app.project.item(i).duplicate();

you need to add a line like this:

netItem.name = currentItem.name;

or this:

netItem.name = currentItem.name.split(".")[0];
djuncheg117156386
Inspiring
October 6, 2022

Thanks for reply. 

Unfortunetly its not work. As im remeber im tried this command already and its do same - yes, its change name of composition as im need, but then its do infinty loop of copying its one composition.  

 

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
October 6, 2022

Ah, OK. After tinkering with the processFile function a little, it seems to work:

		function processFile(theFile) {
			try {
				// Create a variable containing ImportOptions.
				var importOptions = new ImportOptions(theFile);
				try {
					var currentItem = app.project.importFile(importOptions);
					var elm = null;
					for (var i = 1; i <= app.project.items.length; i++) {
						elm = app.project.items[i];
						// Look for the template composition
						if (elm instanceof CompItem && elm.name === 'template') break;
					}
					if (elm == null) return;
					var netItem = elm.duplicate();
					netItem.name = currentItem.name.split(".")[0];
					for (var j = 1; j <= netItem.numLayers; j++) {
						// Replace the soure of the item called 'replace'
						if (netItem.layer(j).name === 'replace') {
							netItem.layer(j).replaceSource(currentItem, true);
						}
					}
				} catch (error) {
					alert(error.toString() + importOptions.file.fsName, scriptName);
				}
			} catch (error) {
				// Ignore errors.
			}
		}