Skip to main content
danielj15115761
Inspiring
July 13, 2021
Question

Script: Find EPS/AI files, create & move to a sub-folder

  • July 13, 2021
  • 3 replies
  • 2033 views

Hello once again,

Below you can find a link to my previous script issue. This post will provide some background  information on our situation. 

 

https://community.adobe.com/t5/illustrator/script-export-as-eps-ai-png-all-in-the-same-folder/m-p/12145797#M281622

What we are now looking for, is a script which can find EPS and AI files within a sub-folder and put them into a new sub-sub-folder with a specific name.
The name the sub-sub-folder is similar to the main folder, except that we want to add "_download" at the end of the folder name. 

For example: the folder structure looks like this at the moment:

folder > image folder > EPS/AI files

 

and we want the new folder structure to look like this:

Folder > image folder > image folder_download > EPS/AI files

 

this script needs to be performed on thousands of sub-folders within the main image folder.

 

i hope this makes sense and hope that it's possibility.

 

thanks in advance for any and all your help.

DJ

This topic has been closed for replies.

3 replies

danielj15115761
Inspiring
July 14, 2021

Hi Guys, 

I just thought of a possible solution to this issue. What if we use the previous script and add this extra steps to the end? Could the script create the new folder and export the EPS/AI file into this file? Or is all this too much?

 

function main() {

        var inputFolder = Folder.selectDialog("Select a folder to process")
         if (inputFolder != null){
        var fileandfolderAr = scanSubFolders(inputFolder, /\.(ai|eps)$/i);
        var fileList = fileandfolderAr[0];
        }
        for (var i = fileList.length-1; i >= 0; i--) {
        app.open(fileList[i]);
        var doc = app.activeDocument;
        try {
            var docLayers = doc.layers;
            var n = docLayers.length;
            docLayers[n-1].visible = true;
            docLayers[n-1].locked = false; 
            docLayers[n-1].remove();
            doc.save();
            doc.close(SaveOptions.NO);
        } catch (e) {

        }
    }
}

main()

function scanSubFolders(tFolder, myAI) {
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;

for (var j = 0; j < sFolders.length; j++) {
var procFiles = sFolders[j].getFiles();

for (var i = 0; i < procFiles.length; i++) {
if (procFiles[i] instanceof File) {
if(myAI == undefined) {
allFiles.push(procFiles);
}

if (procFiles[i].fullName.search(myAI) != -1) {
allFiles.push(procFiles[i]);
  }
}

else if (procFiles[i] instanceof Folder) {
sFolders.push(procFiles[i]);
scanSubFolders(procFiles[i], myAI);
    }
  }
}
return [allFiles, sFolders];
}
Inventsable
Braniac
July 13, 2021

I'd definitly use Node or CLI instead but technically you can write and execute BAT and other CLI syntax files from scripting. At the point you're doing this though, you may as well just execute the file from somewhere else, but if it's necessary as some step within a pre-existing script I can understand why there's a need:

var text = """echo You can execute batch files from JSX
pause""";

var file = File(Folder.desktop + "/test.bat");
file.open("w");
file.write(text);
file.close();

var result = file.execute();
alert(result); // Returns true if successful

 

When you run this, a BAT file is written to desktop and then scripting will execute it (or simulate a double-click action, so if it doesn't work via double-click you need to set up file associations), you should see a terminal window pop up and you can run shell commands this way. An issue with doing this is that the terminal will inherit OS focus -- Illustrator loses focus. Even if the last line is "exit" to close the terminal window, you still tend to lose program focus (that may not be an issue for you but it's an important distinction because it means you can't easily sandwich shell commands into normal scripts with seamless transitions).

Inventsable
Braniac
July 13, 2021

Though now that I'm thinking about it, you could probably do this through a combination of Folder.getFiles() and File.changePath() without needing a shell command at all.

Inventsable
Braniac
July 13, 2021

I thought this would be easy but for some reason I can't get the filepath change working. I can't actually find any example of File.changePath() online -- it says a string, but it doesn't say if it needs encodeURI or as a File Object, as plaintext, etc. I've tried all of these and the method returns "true" which it only should if there are no errors (nothing in try/catch either), so the below doesn't work but this would be relatively close once you can figure out the File.changePath part:

 

var ROOT_STR = Folder.desktop + "/image folder"
var TARGET_STR = Folder.desktop + "/image folder/image folder_download";

var ROOT = Folder(ROOT_STR);
var TARGET = Folder(TARGET_STR);
TARGET.create();

var children = ROOT.getFiles(/.(svg|ai)$/);
for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var newPath = decodeURI(child).replace(ROOT_STR, TARGET_STR);
    var result = File(child).changePath(newPath);
    alert(result) // Returns true, but incorrectly! Files never move.
}

 

Disposition_Dev
Braniac
July 13, 2021

this is more of a shell script project than an illustrator script project.

 

In illustrator, scripts don't have access to the file system this way. we can't just move and rearrange files as far as i know (and if we could, it's still probably not the right environment for this task). We can create folders and we can save files into those folders. But as far as i know that's pretty much it.