First, I should mention that I started scripting for Photoshop about a month ago and have had some really motivating successes there. It led me to this one task we have that is just incredibly repetitive and boring in Illustrator that I want to script.
I haven't gotten all the pieces in here that I need so far, and any form of help would be greatly appreciated, whether just suggestion, or really whatever. I don't mind doing research and trying things out myself.
The purpose of this script is to open a template file we have on our network drive, ask for a folder with a collection of files (these are files provided to us that contain the info we need to make labels), place one, save with a specific name, and start over, placing the next file, till they are all on the template. We do this before we can create labels for our products. There are weeks when it has to be done 50+ times and that's pretty mind-numbing.
The steps I think I need are:
- ask where the provided files are that need to be placed
- open template file
- make sure the folder chosen has the right type of stuff in it (PDFs)
- place the first file at specified coordinates
- embed the link
- lock the embedded link
- ask user to proof information on screen (ok to continue, cancel to stop and let user interact with file on their own) (95% of the time, there are no errors)
- use the embedded link's name as the name of the file and save it to a specific network location
- repeat until all the files in the chosen folder have been done
I've been lurking around here for a couple days, reading up on how Illustrator works with scripting and have broken off bits and pieces of different scripts to adapt for my purposes. So far, this is what I've come up with:
function getLabelRequests() {
return Folder.selectDialog('Please select the folder containing label requests:', Folder('~/Desktop'));
}
function placeLabelRequests(selectedFolder) {
var myDoc;
if (selectedFolder) {
var fileRef = File("/Volumes/myNetworkLocation/myTemplateFile.ai")
open(fileRef);
myDoc = app.activeDocument;
var firstImageLayer = true;
var thisPlacedItem;
// create document list from files in selected folder
var fileList = selectedFolder.getFiles();
for (var i = 0; i < fileList.length; i++) {
// open each document in file list
if (fileList[i] instanceof File) {
// get the file name
var fName = fileList[i].name;
// check for supported file formats
if( (fName.indexOf(".pdf") == -1)) {
// skip unsupported formats
continue;
} else {
// Give the file the name of the image file
File.name = fName.substring(0, fName.indexOf(".") );
// Place the label request on the artboard
thisPlacedItem = myDoc.layers['Job Form'].placedItems.add();
thisPlacedItem.file = fileList[i];
thisPlacedItem.position = [15,-45];
thisPlacedItem.embed();
}
}
}
}
if( firstImageLayer ) {
// display error message
alert("Sorry, but the designated folder does not contain any recognized image formats.\n\nPlease choose another folder.");
myDoc.close();
getLabelRequests (placeLabelRequests());
}
else {
// display error message
alert("Rerun the script and choose a folder with label requests, if you please.");
}
}
// Start the script off
placeLabelRequests (getLabelRequests ());
So obviously there's no loop in there, which is one of my problems at this point. I know what each of these chunks of code do, but can't necessarily understand all the syntax. Particularly things like
for (var i = 0; i < fileList.length; i++) {
I know it's saying that the variable i is zero, and while the list of files is greater than i, do whatever that last bit means, but I don't really know why it works or how it was constructed originally.
I haven't specified the save either, which might be why I'm running another of my problems, but I don't know how to get the name of the link to be the name of the file when it's saved. I also haven't given it a confirm to let the user proof either.
Here's my list of problems:
- Running the script returns the image format error, even though the folder selected contains PDFs (I suppose I don't need to confirm the files are PDFs, that could just be incumbent upon the user)
- Running the script places all the files into one iteration of the template (this may be because I haven't gotten the save or the loop in there, but I think it has more to do with the function being set up the way it is)
- Obviously, it doesn't save with the link name as the file name
- I don't seem to be able to figure out how to lock the link after it's embedded.
I also wonder if there's a way to, rather than opening-placing-saving-closing-repeat, to open-place-save, delete-placenext-save, delete-placenext-save.
Phew, sorry for the short novel.
Here is a copy of the template … or not. Can I not embed files in the post that aren't images?
Here's a JPG of the AI file that I use as a template
![Label Job Form-5C-01.jpg]()
This document has two layers in this order normally:
Artwork
Job Form
And here are the blocks I'm using as placement for the information we are provided. These are taking the place of the files that need to be placed, and again, were PDF files, but I'm uploading as JPGs because I either don't know what I'm doing, or you can't upoad those file types.
![PlaceMe.jpg]()
![PlaceMe 2.jpg]()
![PlaceMe 3.jpg]()