javascriptloopsadobe-illustrator

Running a script on all .ai files in a directory


I'm a graphic designer and a beginner at coding. So please dumb down for me :)

I have a script that I run through Illustrator. It embeds an image and removes a clipping mask on the file. I need this script to run on multiple .ai files in a folder.

The thing is, I cannot create a batch action (in Illustrator) to run the script. Our IT guy doesn't want to add the script to our Illustrator settings on multiple accounts. Is there a way to add code to my script to do this?

I need it to open all the files, run the script and save. The files can be left open on Illustrator, no need to close.

What code do I need to add to my script?

I am on a Mac not PC.


Solution

  • It can be done this way:

    function my_script() {
    
        // copy a full text of your script here
    
        // or include the jsx file this way:
        # include '~/Desktop/script/my_script.jsx'
    
    }
    
    function main() {
    
        var folder = Folder.selectDialog('Please, select the folder');
        if (!(folder instanceof Folder)) return;
    
        var files = folder.getFiles('*.ai');
        if (files.length == 0) {
            alert('Folder doesn\'t content AI files');
            return;
        }
    
        var i = files.length;
        while (i--) {
            var doc = app.open(files[i]);
            my_script();             // <------- here your script is running
            doc.save();
            doc.close();
        }
    
        alert(files.length + ' files were processed');
    }
    
    main();