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.
dln385
source share