如何加载另一个脚本文件并在其上运行方法?
我正在使用 InDesign javascript,但我不知道如何在同一个脚本中包含多个文件。
如何加载另一个脚本文件并在其上运行方法?
我正在使用 InDesign javascript,但我不知道如何在同一个脚本中包含多个文件。
三个选项:import、app.doScript 和 $.evalFile。我更喜欢 $.evalFile。参见app.doScript 与 $.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;
}
};
如果 script2.jsx 不在 C 盘根目录下,请修改脚本 1 的真实位置。
sFullName
将是全球性的。g_script2
中。combineName
脚本 2 的方法。需要注意的是,脚本的所有文件都将共享同一个全局命名空间,这也是脚本 1 可以访问的方式g_script2
。但是,这也意味着对于一个函数或变量,任何两个文件都不应该具有相同的名称,除非它们像本例中那样保存在全局对象中。combineName
函数运行,并返回一个字符串。ExtendScript 提供预处理器指令以包含外部脚本。该指令将目标文件的内容插入到语句所在位置的当前脚本文件中。因此,在语句之后,您将能够调用任何方法,就像它是当前脚本的方法一样:
#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();