2

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?

2 Answers 2

4

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.
1
  • hi Joonas, thanks a lot for your precious help! I implemented all your corrections, now the script is executed without any error I tried placing the alert( dirImages.error ); and it comes out blank, so everything looks ok but the script keeps not producing any result I'm already sure there's no capital letters in both documentName and image files, but I will write a couple of lines to convert everything in lowercase just to be sure
    – hoverlol
    Nov 30, 2015 at 20:55
2

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
  • Yup, that would do it :)
    – Joonas
    Dec 1, 2015 at 0:05
  • Using forwardslash / instead of backslash solves the double slash problem. I'm guessing you need to use double slash because the first needs to escape the second.
    – akinuri
    Sep 8, 2016 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.