Illustrator scripting: place image file if it has same name of illustrator file


Question

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?

1
2
7/19/2016 2:11:00 PM

Accepted Answer

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;

        }
 }

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

2
5/1/2018 9:11:00 PM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow


Website under construction!!!
^