How to require or include scripts in indesign?

How to load another script file and run a method on it?

I am using InDesign javascript and I do not know how to include multiple files in the same script.

+8
javascript include require adobe indesign
source share
2 answers

Three options: import, app.doScript and $ .evalFile. I prefer $ .evalFile. See app.doScript vs. $ .evalFile

Working example:

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 in the root directory of drive C, change script 1 to its true location.

Explanation:

  • Script 1 creates and performs an anonymous function to avoid pollution of the global namespace. If not, sFullName will be global.
  • Script 1 executes script 2.
  • Script 2 creates an object and saves it in the global variable g_script2 .
  • Script 1 calls the combineName script 2 method. It is important to note here that all the files in your script will share the same global namespace, namely, how script 1 can access g_script2 . However, this also means that none of the two files should have the same name for a function or variable if they are not stored inside a global object, as in this example.
  • The combineName function is combineName and returns a string.
  • Script 1 prints the name, and then returns Success. Since this is the last object on the stack, it is returned as the result of the script.
+10
source share

ExtendScript provides preprocessor directives for including external scripts. This directive inserts the contents of the target file into the current script file at the location of the instruction. Therefore, after the statement, you can call any method, for example, its 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(); 
+3
source share

All Articles