Free Function Friday Ep.14 – importAFile

December 25th 2015 08:00:20am

Welcome to Free Function Friday episode 14. This week we assemble a simple function that condenses the file importing process into one easy function call. To do this with ExtendScript normally you have to deal with a multi-step process, by setting up an import options object then supplying that object to the importFile method itself.

Now this really is a decently simple build, so we are gonna go over other ExtendScript code so you can see how this function can be used. We’ll go over how to access a folder on your harddrive and how to retrieve the files from that folder. Once we are finished building that I will show you how to make a small modification and have it grab only specific files types just by adding a few more lines to our original code. This overview will hopefully help you learn a few other areas of ExtendScript. Once you know the fundamentals of accessing a folder on your computer, it will hopefully help open the door for you for many more scripting options.

There is however one requirement for allowing scripts to access the os, and it is that you have the Read/Write preference turned on in After Effects. I failed to mention this in the video, but this snippet taken from the After Effects Help / Scripts online help explains how to turn that option on manually.

Taken from the After Effects Help
Note:
By default scripts are not allowed to write files or send or receive communication over a network. To allow scripts to write files and communicate over a network, choose Edit > Preferences > General (Windows) or After Effects > Preferences > General (Mac OS), and select the Allow Scripts To Write Files And Access Network option.

Source Code:

var a = new File("~/Desktop/myFile.txt");
alert(importAFile(a));
/*	TESTING ABOVE	*/

function importAFile(fileObj){
	if(fileObj instanceof File){
		app.project.importFile(new ImportOptions(fileObj));
		return true;
	}else{
		return false;
	}
}
Checkout more:
Fast Marker Creator After Effects Script
Free Function Friday Ep.9 – retrievePrimaryComps
Houdini | FFX Geometry Explainer
After Effects ExtendScript Training: Ep. 12 Part 2
Houdini Mardini Escape