Skip to main content
brian_p_dts
Community Expert
Community Expert
July 17, 2022
Question

[Scripting] Any ways to get Illy not to freeze when executing a complex script on PC?

  • July 17, 2022
  • 2 replies
  • 699 views

When I run certain scripts where I'm performing batch operations, Illustrator always goes nonresponsive on me. Script UI progress bars don't work. I include a target illustrator and targetengine directive at the top. This doesn't happen on Mac. Is there a way around it? 

This topic has been closed for replies.

2 replies

Anubhav M
Community Manager
Community Manager
July 19, 2022

Hello @brian_p_dts,

 

Sorry to hear about this experience. As pointed out by Mylenium, more information is required to diagnose the exact reason for the freezing issue. Kindly share your computer's specs, the exact version of Illustrator and the OS, and what exactly the script does.

 

Also, try resetting Illustrator's preferences by following the steps shared in this article (https://helpx.adobe.com/illustrator/kb/troubleshoot-preference-issues.html) and sharing your observations.
Disclaimer: Please note that resetting preferences will remove all custom settings, and Illustrator will launch with the defaults. You may take a backup of these settings from the location mentioned in the article.

Looking forward to hearing from you.

 

Thanks,
Anubhav

brian_p_dts
Community Expert
Community Expert
July 20, 2022

Here is the code. Freezing occurs on both my computer and client's computer. Our respective specs are attached. This happens with many other Illustrator scripts I write, and only on PC. I feel like I asked about this before and knew this to be a commonly understood problem, but perhaps I am misremembering.  I will try resetting preferences too. 

The code opens a series of selected AI files, parses the file name, outputs them as jpgs. 

#target illustrator
#targetengine "exporterDTS"

var gHasError = false;

//Background Technology
//Following font finding functions adapted from:
//https://community.adobe.com/t5/illustrator-discussions/how-can-i-find-existence-of-missing-fonts-using-java-script/td-p/10872126
var hasMissingFont = function(doc, sFontNames) {
    var fonts = getFntsLst(doc); 
    var i = fonts.length;// array
    while (i--) {
        if (sFontNames.indexOf(";"+fonts[i]+";") == -1) {
            return true;
        }
    }
}

//----------------------------------------------------------------
var getFntsLst = function(doc){
// return list array fonts doc
    var docFontList = [];
    var tf;
    for ( i = 0; i < doc.textFrames.length; i++ ) {
        var charLen = doc.textFrames[i].story.textRange.contents.length;
        for ( var j = 0; j < charLen; j++ ) {
            tf = doc.textFrames[i].story.textRange.characters[j].textFont;
            docFontList.push(tf.name+"-"+tf.style);
        }
    }
    return docFontList;
}

//----------------------------------------------------------------
var getSysFonts = function(){
// string list fonts system (sep space))
    var sep = ";";
    var sFontNames = sep;
    var i = app.textFonts.length;
    while(i--) {
        sFontNames += textFonts[i].name+"-"+textFonts[i].style+sep;
    }
    return sFontNames;
}

//END FONT CHECK ROUTINES

//----------------------------------------------------------------
var getTemplateFol = function() {
    var prefsFile = File(File($.fileName).path + "/ExporterPrefs.txt");
    prefsFile.encoding = "UTF-8";
    prefsFile.open("r");
    var inputPath = prefsFile.readln();
    var outPath = prefsFile.readln();
    prefsFile.close();
    if (!inputPath || /^( +?)$/gi.test(inputPath)) {
        inputPath = inputPath;
    }
    if (!outPath || /^( +?)$/gi.test(outPath)) {
        outPath = outPath;
    }
    var ui = control(inputPath, outPath);
    prefsFile.encoding = "UTF-8";
    prefsFile.open("w");
    prefsFile.writeln(ui.inFolPath);
    prefsFile.writeln(ui.outFolPath);
    prefsFile.close();
    return ui;

}

//----------------------------------------------------------------

var control = function(inFolPath/*str*/, outFolPath/*str*/) {
    var tmpIn;
    var tmpOut;
    // EXPORTERUI
    // ==========
    var exporterUI = new Window("dialog"); 
        exporterUI.text = "Exporter"; 
        exporterUI.preferredSize.width = 400; 
        exporterUI.orientation = "column"; 
        exporterUI.alignChildren = ["center","top"]; 
        exporterUI.spacing = 10; 
        exporterUI.margins = 16; 
    // PANEL1
    // ======
    var panel1 = exporterUI.add("panel", undefined, undefined, {name: "panel1"}); 
        panel1.orientation = "row"; 
        panel1.alignChildren = ["left","top"]; 
        panel1.spacing = 10; 
        panel1.margins = 10; 

    var statictext1 = panel1.add("statictext", undefined, undefined, {name: "statictext1"}); 
        statictext1.text = "Input"; 
        statictext1.alignment = ["right","center"]; 
    
    var iconbutton1 = panel1.add("button", undefined, undefined, {name: "iconbutton1"}); 
        iconbutton1.text = inFolPath; 
        iconbutton1.preferredSize.width = 350;

        //input folder
        iconbutton1.onClick = function() {
            if (inFolPath && Folder(inFolPath).exists) {
                tmpIn = Folder(inFolPath).selectDlg("Choose the new path for the AI files");
                if (tmpIn && tmpIn.exists) {
                    inFolPath = decodeURI(tmpIn.fsName);
                    iconbutton1.text = inFolPath; 
                } else {
                    inFolPath = "";
                    iconbutton1.text = "No folder selected"; 
                }
            }
            else {
                tmpIn = Folder.desktop.selectDlg("Choose the new path for the AI files");
                if (tmpIn && tmpIn.exists) {
                    inFolPath = decodeURI(tmpIn.fsName);
                    iconbutton1.text = inFolPath; 
                }
                else {
                    inFolPath = "";
                    iconbutton1.text = "No folder selected"; 
                }
            }
        }
    // PANEL2-OUTPUT
    // ======
    var panel2 = exporterUI.add("panel", undefined, undefined, {name: "panel2"}); 
        panel2.orientation = "row"; 
        panel2.alignChildren = ["left","top"]; 
        panel2.spacing = 10; 
        panel2.margins = 10; 

    var statictext1 = panel2.add("statictext", undefined, undefined, {name: "statictext2"}); 
        statictext1.text = "Output"; 
        statictext1.alignment = ["right","center"]; 
    
    var iconbutton2 = panel2.add("button", undefined, undefined, {name: "iconbutton2"}); 
        iconbutton2.text = outFolPath; 
        iconbutton2.preferredSize.width = 350;


        iconbutton2.onClick = function() {
            if (outFolPath && Folder(outFolPath).exists) {
                tmpOut = Folder(outFolPath).selectDlg("Choose the new path for the output files");
                if (tmpOut && tmpOut.exists) {
                    outFolPath = decodeURI(tmpOut.fsName);
                    iconbutton2.text = outFolPath; 
                } else {
                    outFolPath = "";
                    iconbutton2.text = "No folder selected"; 
                }
            }
            else {
                tmpOut = Folder.desktop.selectDlg("Choose the new path for the output files");
                if (tmpOut && tmpOut.exists) {
                    outFolPath = decodeURI(tmpOut.fsName);
                    iconbutton2.text = outFolPath; 
                }
                else {
                    outFolPath = "";
                    iconbutton2.text = "No folder selected"; 
                }
            }
        }

    // PANEL1
    // ======
    var panel1 = exporterUI.add("panel", undefined, undefined, {name: "panel1"}); 
        panel1.orientation = "row"; 
        panel1.alignChildren = ["left","top"]; 
        panel1.spacing = 10; 
        panel1.margins = 10; 

    var cancel = panel1.add("button", undefined, undefined, {name: "cancel"}); 
        cancel.text = "Cancel"; 

    var ok = panel1.add("button", undefined, undefined, {name: "ok"}); 
        ok.text = "OK"; 

    exporterUI.show();
    return {
        inFolPath: inFolPath,
        outFolPath: outFolPath,
    }
}


//----------------------
var main = function() {
    var fst = getTemplateFol();
    //if (!fst || !fst.exists) return;
    var fs = File(fst.inFolPath).openDlg("Choose the AI files", "*.ai*", true);
    if (!fs || fs.length == 0) {
        alert("No files selected in " + fst.inFolPath);
        return;
    }
    var outfol = Folder(fst.outFolPath);
    if (!outfol || !outfol.exists) {
        alert(outfol.fsName + " not found. Please run the script and reselect a new folder.");
        return;
    }
    gOutFolPath = outfol.fsName;
    var sFontNames = getSysFonts(); // string

    var exportOptions = new ExportForScreensOptionsJPEG();
        exportOptions.scaleType = ExportForScreensScaleType.SCALEBYRESOLUTION;
        exportOptions.scaleTypeValue = 300;
        exportOptions.antiAliasing = AntiAliasingMethod.None;
        exportOptions.embedICCProfile = false;
   
    var i = fs.length;
    var docNumRx = /\$\$\$/gi;
    var fabRx = /!!!/gi;
    var sizeRx = /###/gi;
    var badFiles = [];
    var wi = new Window('palette');
        wi.location = [100, 100];
        wi.text = "Processing orders";
        wi.pbar = wi.add('progressbar', undefined, 0, i);
        wi.pbar.preferredSize.width = 300;

        wi.show();

    var success = 0;
    var doc, allTfs, n, o, fn, tf;
    while (i--) {
        fn = decodeURI(fs[i].name);
        try {
            doc = app.open(fs[i]);
        } catch(e) {
            gHasError = true;
            $.writeln("DOCUMENT ERROR. Could not open document " + fn + ". Skipping. " + e);
            badFiles.push(fs[i]);
            wi.pbar.value++;
            continue;
        }
        if (hasMissingFont(doc, sFontNames)) {
            gHasError = true;
            $.writeln("MISSING FONT ERROR: Document " + fn + ". Skipping");
            badFiles.push(fs[i]);
            doc.close(SaveOptions.DONOTSAVECHANGES);
            wi.pbar.value++;
            continue;
        }
        o = parseFileName(fn);
        //skip if could not successfully parse name
        if (o === null) {
            doc.close(SaveOptions.DONOTSAVECHANGES);
            badFiles.push(fs[i]);
            wi.pbar.value++;
            continue; 
        }
        allTfs = doc.textFrames;
        n = allTfs.length;
        while (n--) {
            tf = allTfs[n];
            if (docNumRx.test(tf.contents)) {
                tf.contents = tf.contents.replace(docNumRx, o.docNum);
            }
            if (fabRx.test(tf.contents)) {
                tf.contents = tf.contents.replace(fabRx, o.fabType);
            }
            if (sizeRx.test(tf.contents)) {
                tf.contents = tf.contents.replace(sizeRx, o.size);
            }
        }
        try {
            output(exportOptions, outfol);
        } catch(e) {
            gHasError = true;
            $.writeln("OUTPUT ERROR: " + fn + ". " + e);
            if (doc) {
                doc.close(SaveOptions.DONOTSAVECHANGES);
            }
        }
        success++;
        wi.pbar.value++;
    }
    if (badFiles.length > 0) {
        moveBadFiles(badFiles, outfol);
    }
    Folder(outfol + "/300ppi").rename("processed-" + new Date().toLocaleDateString());

    if (gHasError) {
        alert("Done, with errors. " + success +" files successful. Check error folder for exporter-log.txt");
    }
    else {
        alert("Done, with no errors. " + success + " files successful.");
    }
}

//-------------------------------
var moveBadFiles = function(badFiles, outfol) {
    var errfol = Folder(outfol +"/error-" + new Date().toLocaleDateString());
    if (!errfol.exists) errfol.create();
    var i = badFiles.length;
    while (i--) {
        badFiles[i].copy(errfol.fsName + "/" + badFiles[i].name);
        badFiles[i].remove();
    }
}


//-------------------------------

var output = function(exportOptions, outfol) {
    var doc = activeDocument;
    var abs = doc.artboards;
    var n = abs.length;
    var i = 0;
    var a = [];

    for (i; i < n; i++) {
        abs[i].name = ("0" + (i+1)).slice(-2);
        a.push((i+1).toString());
    }

    var whatToExport = new ExportForScreensItemToExport();
        whatToExport.artboards = a.join(",");
        whatToExport.document = false;
    
    var dn = decodeURI(doc.name);
    var dnrx = /\.ai$/gi;
    
    doc.exportForScreens(File(outfol), ExportForScreensType.SE_JPEG100, exportOptions, whatToExport, dn.replace(dnrx,"") + "-");

    doc.close(SaveOptions.DONOTSAVECHANGES);
}

//-------------------------------

String.prototype.ftrim = function() {
    return this.replace(/^ +/gi,"").replace(/ +$/gi,"");
}


//-------------------------------

var parseFileName = function(str) {

    var hyphenSplit = str.split("-");
    try {
        var docNumRx = /^(\d|[A-Za-z])+/gi;
        var docNumStr = str.match(docNumRx);
        if (docNumStr === "") {
            $.writeln("DOCUMENT NUMBER PARSING ERROR: " + str);
            docNumStr = null;
        }
    } catch(e) {
        $.writeln("DOCUMENT NUMBER PARSING ERROR: " + str);
        docNumStr = null;
    }

    try {
        fabTypeStr = hyphenSplit[hyphenSplit.length - 2].ftrim();
        //include a try here incase match == 0, in which case it's fine
        try {
            if (fabTypeStr.match(/ +/gi).length > 1) {
                $.writeln("FAB TYPE PARSING ERROR: " + str);
                fabTypeStr = null;
            }
        } catch(e) {}
        
    } catch(e) {
        $.writeln("FAB TYPE PARSING ERROR: " + str);
        fabTypeStr = null;
    }
    //size string should be fourth from last set in hyphen chain
    var sizeRx1 = /LARGE/gi;
    var sizeRx2 = /\d{1,2}/gi;
    var sizeStr = hyphenSplit[hyphenSplit.length - 4].ftrim();
    try {
        if (!sizeRx1.test(sizeStr) && !sizeRx2.test(sizeStr)) {
            sizeStr = null;
            $.writeln("SIZE PARSING ERROR: " + str);
        }
    } catch(e) {
        sizeStr = null;
        $.writeln("SIZE PARSING ERROR: " + str);
    }
    
    if (docNumStr === null || sizeStr === null || fabTypeStr === null) {
        gHasError = true;
        return null;
    }

    return {
        docNum: docNumStr,
        size : sizeStr,
        fabTypeStr : fabTypeStr
    }
};

main();

 

Disposition_Dev
Legend
July 20, 2022

opening files (especially from a mounted location) and exporting jpgs is just time consuming. How many files are you attempting to open, process, and export? If that number is more than 20... i'd say that's your issue. illustrator just isn't very fast at doing stuff like this. 

 

does it do any of the files properly before it freezes? or is it freezing completely upon opening/processing/exporting the very first file?

 

If you just wait a bit... does it finish? Try launching the script right before you go to lunch or something so you can just let it sit. it could be that it isn't necessarily freezing, but illustrator is just busy so you can't interact with it.. (this also causes the app to show up as "not responding" in task managers) but that doesn't necessarily mean it's totally frozen and needs to be restarted.

Mylenium
Legend
July 18, 2022

What does complex even mean? What exactly are you doing? What are the specs of the system? What does the code look like? You need to be more specific.

 

Mylenium