Ok, I think I have got to the bottom of this, and discovered something interesting about JavaScript in the process...
Firstly, the
Mozilla JavaScript Reference states that AE's version of ECMAScript does
not support
import and
export functions:
Mozilla Reference:
http://developer.mozilla.org/en/docs/Co ... nts:import
Adobe Reference:
"After Effects scripting is based on ECMAScript (or more specifically, the 3rd Edition of the ECMA-262 Standard)."
So, where does that leave us? Fortunately, before despair set in, I came across another method for importing script from other files, detailed here:
http://www.webreference.com/programming/javascript/mk/
Like most JavaScript examples on the web, it's heavily browser-based. The principal idea of using the
eval() function to import another file is a brilliant one though, and easily adapted to an even simpler bit of AE script.
A.jsx
Code: Select all
importJsx("B.jsx");
alert(Bfunction());
function importJsx(fileName)
{
var f = new File(fileName).open('r');
f.open('r');
var toImport = f.read();
f.close();
return eval(toImport);
}
B.jsx
Code: Select all
function Bfunction()
{
return "success";
}
Using this method, you need only need paste one simple, unchanging function into each of your scripts, with a file reference to your library of functions. This will save you time pasting code, and allow older scripts to automatically benefit from improvements you may make to common functions as time goes on. The pitfall of this is that if you accidently change the behaviour of one of your functions, you may break the behaviour of your older scripts. When maintaining a function library file, you should always keep running backups so you can fall back on an earlier version if necessary.
The 'something interesting' that I mentioned earlier, was learning how to
eval() the imported code into the
top level of our script. Originally, I called
eval() from within the body of the
ImportJsx() function, which resulted in the functions loaded from
B.jsx only becoming defined within the scope of the
ImportJsx() function, clearly not what we want. Then it hit me: we want to
return the evaluated code to wherever the function was called. Which works!
Update: For sheer pasteability alone, this somewhat hacky code gets it down to one line which can be pasted at the top of your scripts, without the need to define any functions:
Code: Select all
eval((f=new File("myFunctionLibrary.jsx")).open('r')?f.read()+(f.close()?"":""):alert("Import failed"));
Hope you find this useful... This technique is key to a super useful and secret script due for release soon on AEnhancers.
(clue: Excel) 