javascript InDesign脚本编写:在脚本文件中包含远程.js-file(并使用其变量)

oyjwcjzk  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(133)

我试图访问InDesign脚本中的远程.jsfile以使用它的变量。我找到了本地包含js-files的函数,但还没有找到包含的好方法。
http://remote-site.com/test.js

var testVar = "it works!";

myscript.js,包括本地(工作):

app.doScript(new File("/Users/Popmouth/test.js"));
alert(testVar);

myscript.js,包括本地和远程(不工作):

app.doScript(new File("http://remote-site.com/test.js"));
alert(testVar);

我还发现了这个片段,这个警报工作(警报文件的内容,即"var testVar = "it works!;"),但我不知道如何使用我的警报函数下面的变量:

var HTTPFile = function (url,port) {
    if (arguments.length == 1) {
            url = arguments[0];
            port = 80;
    };

    this.url = url;
    this.port = port;
    this.httpPrefix = this.url.match(/http:\/\//);
    this.domain = this.httpPrefix == null ? this.url.split("/")[0]+":"+this.port :this.url.split("/")[2]+":"+this.port;
    this.call = "GET "+ (this.httpPrefix == null ? "http://"+this.url : this.url)+" HTTP/1.0\r\nHost:" +(this.httpPrefix == null ? this.url.split("/")[0] :this.url.split("/")[2])+"\r\nConnection: close\r\n\r\n";
    this.reply = new String();
    this.conn = new Socket();
    this.conn.encoding = "binary";

    HTTPFile.prototype.getFile = function(f) {
        var typeMatch = this.url.match(/(\.)(\w{3,4}\b)/g);
        if (this.conn.open(this.domain,"binary")) {
                  this.conn.write(this.call);
                  this.reply = this.conn.read(9999999999);
                  this.conn.close();
        } else {
                  this.reply = "";
        }
        return this.reply.substr(this.reply.indexOf("\r\n\r\n")+4);;
    };
}

var remoteFile = new HTTPFile("http://remote-site.com/test.js");
alert(.getFile());

此功能

jvlzgdj9

jvlzgdj91#

好的,我去了adobe-forums,得到了下面的字符串,它替换了app.doScript(new File("/Users/Popmouth/test.js"));

var remoteCode = 'http://remote-site.com/test.js';  //location of your remote file
var script = app.doScript("do shell script \"curl 'remoteCode'\"".replace("remoteCode", remoteCode), ScriptLanguage.APPLESCRIPT_LANGUAGE);  

// Set script args  
app.scriptArgs.setValue("scriptArg1", "Hello");  
// Set environmental vars  
$.setenv("envVar1", "World");  
// Run the "remote" script  
app.doScript(script, ScriptLanguage.JAVASCRIPT);

相关问题