How to require or include scripts in indesign?
Asked Answered
M

2

9

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.

Meaghan answered 6/3, 2013 at 10:2 Comment(0)
P
10

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 located in the root of the C drive, modify script 1 with its true location.

Explanation:

  1. Script 1 creates and executes an anonymous function to avoid polluting the global namespace. If it didn't do this, sFullName would be global.
  2. Script 1 executes Script 2.
  3. Script 2 creates an object and stores it to the global variable g_script2.
  4. Script 1 calls the 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.
  5. The combineName function is run, and returns a string.
  6. Script 1 prints the name, then returns "Success". Since that's the last object on the stack, it is returned as the script result.
Philibeg answered 6/3, 2013 at 21:26 Comment(3)
Thanks, in windows works both the following: "c:/script2.jsx" instead of "/c/script2.jsx". But what about relative paths? relatives to the current script..Meaghan
In windows it would be better to use c:/script2.jsx instead of /c/script2.jsx. The latter will fail if there's a folder named c on the root drive, i.e. c:\c . If script2.jsx is in the same folder as script1.jsx, you could run it with the command $.evalFile(new File((new File($.fileName)).parent.fullName + "\script2.jsx")). Alternatively, ExtendScript does support relative file paths, but I wouldn't recommend using them because they're not relative to the running script.Philibeg
Wow! That works like a charm. For fetching the script's own folder, you can do this: var root = (new File($.fileName)).parent + "/"; - it returns the path like this, however: /c/some%20folder/path - note the lack of trailing slash, by the way. AND, using $.writeln(sFullName); calls up the ESTK on my computer. Things work fine without that line.Betulaceous
M
5

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();
Manhood answered 9/9, 2015 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.