How do I load another script file and run a method on it?
I'm using InDesign javascript and I don't know how to include multiple files in the same script.
Three options: import, app.doScript, and $.evalFile. I prefer $.evalFile. See app.doScript vs $.evalFile
C:\script1.jsx
(function() {
$.evalFile(new File("/c/script2.jsx"));
var sFullName = g_script2.combineName("John", "Doe");
$.writeln(sFullName);
return "Success";
})();
C:\script2.jsx
g_script2 = {
combineName: function(sFirstName, sLastName) {
return sFirstName + " " + sLastName;
}
};
If script2.jsx is not located in the root of the C drive, modify script 1 with its true location.
sFullName
would be global.g_script2
.combineName
method of script 2. It is important to note here that all of the files of your script will share the same global namespace, which is how script 1 can access g_script2
. However, this also means that no two files should ever have the same name for a function or variable, unless they're kept inside a global object like in this example.combineName
function is run, and returns a string.ExtendScript provides preprocessor directives for including external scripts. This directive inserts the contents of target file into the current script file at the location of the statement. So after the statement you will be able to call any method like it’s a method of the current script:
#target InDesign;
// Include other script file that includes a function otherScriptFileMethod
#include "otherScriptFile.jsx"
// Now you can call the method as it was written in this script file
otherScriptFileMethod();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With