1

我想最终在我的服务器上创建一个文件夹,其中将填充图像,使用 basil.js 我想提取它们并将它们放入 indesign 中。

该脚本有效,但我需要一个 IF 语句,该语句将错误检查 URL 是否实际存在......因为目前即使该 URL 上没有实际图像,它也会放置一个黑色占位符。

if != url .....所以我不必使用 FOR 循环...我会继续添加图像,直到检查整个目录。

可能没有你如何做到这一点......对不起,但由于我是一个新手,我一直在修补它。

#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";

var count = 1;
var x = 100;
var y = 100;

function draw() {

    for (i = 0; i < 15; i++) {

   var url = "http://www.minceandcheesepie.com/spaceinvaders/image" +  count + ".png";

   var newFile = new File("~/Documents/basiljs/user/data/image" + count + ".png");

  b.download(url, newFile);
  b.image('image' + count +'.png', x, y);

    x += 200; 
    y += 200;
    count++;

    app.select(NothingEnum.nothing, undefined);
    }

}
b.go();
4

1 回答 1

1

您需要发出 HTTP HEAD 请求以检查 URL 的结果:

reply = "";
conn = new Socket;

if (conn.open ("www.minceandcheesepie.com:80")) {
    // send a HTTP HEAD request
    conn.writeln("HEAD /spaceinvaders/image" + counter + ".png HTTP/1.0\r\nConnection: close\r\nHost: www.minceandcheesepie.com\r\n\r\n");
    // and read the server's reply
    reply = conn.read(999999);
    conn.close();
}

然后在reply你从服务器(服务器响应)中得到一个字符串,它告诉你页面是否存在,那么你只需要通过页面的结果来解析它(如果它是 200 - 页面存在):

var serverStatusCode = parseInt(reply.split('\n\n')[0].split('\n')[0].split(' ')[1]);
if (serverStatusCode === 200) {
    alert('exists');
} else {
    alert('not exists');
}

存在 URL 的服务器响应示例:

HTTP/1.1 200 OK
Date: Wed, 16 Dec 2015 06:37:20 GMT
Server: Apache
Last-Modified: Wed, 16 Dec 2015 02:41:08 GMT
ETag: "67d-526fad6ab0af2"
Accept-Ranges: bytes
Content-Length: 1661
Connection: close
Content-Type: image/png

不存在 URL 的服务器响应示例:

HTTP/1.1 404 Not Found
Date: Wed, 16 Dec 2015 06:47:33 GMT
Server: Apache
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1
于 2015-12-16T06:47:52.903 回答