« Working with CSV files Adding pictures »
Getting Images from URLs
How to download images from the web and add it afterwards to a document. By Benedikt Groß
Obviously you have to download a resources first, before you can you can make use of it in an indesign document.
For the download part basil.js provides a "b.download()" function. Depending where you want to save the file you have to set the second parameter (optional) eg.:
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
var url = "https://raw.github.com/basiljs/basil.js/master/lib/basil.png";
// download the url to a default location, filename according to url:
// -> "the project folder" + data/download/basil.png
b.download(url);
// download url to a specific location in the project folder:
// -> "the project folder" + data/download_images_files/basil_logo.png
b.download(url, "download_images_files/basil_logo.png");
// download url to a specific location e.g. to your desktop
// -> ~/Desktop/basil_logo.png
var newFile = new File("~/Desktop/basil_logo.png");
b.download(url, newFile);
}
b.go();
If you want to add afterwards the image to a document, you simply have to know where you've saved the file earlier. You can use for instance b.image() to add an image to the current page.
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
function draw() {
b.download("https://raw.github.com/basiljs/basil.js/master/lib/basil.png",
"download/basil.png");
b.image("download/basil.png",0,0);
}
b.go();
