Mobile app version of vmapp.org
Login or Join
Heady304

: Illustrator scripting: place image file if it has same name of illustrator file I'm -slowly- learning how to code using JavaScript in Illustrator, and I'm trying to write a script that automatically

@Heady304

Posted in: #AdobeIllustrator #IllustratorScripting

I'm -slowly- learning how to code using JavaScript in Illustrator, and I'm trying to write a script that automatically picks an image file from a given fixed folder in my hard drive and places it in a fixed position inside the Illustrator file. All of this should happen if and only if the image has the very same name of the Illustrator file itself (both without the ".something" extension)

Here's what I have so far:

doc = app.activeDocument;

//images are in this folder
var dirImages = new Folder("C:FolderWithImages");
var imagesList = dirImages.getFiles();

var itemToPlace;


for (var i = 0; i < imagesList.length; i++) {

var imgName = imagesList[i].name;
var documentName = activeDocument.name;

//compare image filename to current document name (both without extensions)

var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
var docuNameNoExt = documentName.slice(0, documentName.indexOf("."));

// check identical names
if( imgNameNoExt == docuNameNoExt ) {

itemToPlace.file = imagesList[i];
itemToPlace.top = myDocument.height;
itemToPlace.left = 0;
}
}


When I run it I don't get any error code, but I don't get any result either. What am I doing wrong?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Heady304

2 Comments

Sorted by latest first Latest Oldest Best

 

@Heady304

It came out I wasn't using the most important function to place an item in the canvas :)
here I copy the corrected version of the script (for sure it can be implemented, but right now is already working as a charm)

doc = app.activeDocument;

//images are in this folder -yes, you need double "" for some reason I still don't know-
var dirImages = new Folder("C:FolderWithImages");
var imagesList = dirImages.getFiles();

var itemToPlace = {};

for (var i = 0; i < imagesList.length; i++) {

var imgName = imagesList[i].name;
var documentName = doc.name;

//compare image filename to current document name (both without extensions)

var imgNameNoExt = imgName.slice(0, imgName.indexOf("."));
var docuNameNoExt = documentName.slice(0, documentName.indexOf("."));

// check identical names
if( imgNameNoExt == docuNameNoExt ) {


// this I was missing!!!!!!!!!!!!
var itemToPlace = doc.placedItems.add();

itemToPlace.file = imagesList[i];
itemToPlace.layer = doc.currentLayer;
itemToPlace.top = doc.height;
itemToPlace.left = 0;

}
}


Thaks again Joonas for helping me out on this, without your advices I wouldn't have had the strenght to dig more into this and make it work!!

10% popularity Vote Up Vote Down


 

@Ogunnowo857

Your dirImages path has one extra backslash. Should probably be: C:FolderWithImages?

I tend to just throw alerts in places to figure out where things are going wrong. You might want to try sticking alert( dirImages.error ); somewhere after it to see if that says there's an error in your path.

Also:


itemToPlace variable doesn't contain an object.


Change it to var itemToPlace = {};

In itemToPlace.top, you are referencing myDocument, which doesn't exist in this code.


It should probably be itemToPlace.top = doc.height;

This one isn't an error, but a missed opportunity. In variable documentName, you are using activeDocument.name, when you could be using doc.name.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme