Basil.js 粘贴图片前检查URL?

Basil.js Check URL before pasting image?

我想最终在我的服务器上创建一个文件夹,其中将充满图像,使用 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();

您需要发出 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