2 Replies Latest reply on Oct 17, 2017 12:29 PM by joshuas1975

    Illustrator CS6 stops responding during script and crashes at end...

    joshuas1975

      Hello Everyone,

       

      I'm having a serious problem. I need to optimize about 170+ Illustrator files for export to *.svg for the UI elements of our Tech Demo. Each of these Illustrator files has in excess of 500 paths. (They're vegetation, so tons of leaves and such.) In order to fully optimize these files, I need to basically take the black outline filled paths and cut them out of all of the non-black filled paths beneath them and then merge that "cutter" path with the bottom-most black outline filled path.

       

      I've created a script to automate this process. The script is working fine. The only problem is that once I get about 15 - 20 paths in, Illustrator locks up to the "Not Responding" stage and then, once the script is done executing, it crashes. Now, I've run the script on a much simple version of one of the most complex files (640 paths). My test file has 10 paths in it. On the 10 path version, no problems. On the 640 path version, the crashing behavior.

       

      Can anyone take a look at my script to see if there is anything I can do to rectify this situation? I've tried putting in sleep and app.redraw() calls to see if that could force Illustrator to keep up, but that just exacerbates the situation. I think because Illustrator is waiting for the script to execute. I'm really stumped and frustrated as to what I can do. I'm Including the script at the end.

       

      Thanks In Advance!

       

      var debugLevel = $.level;

      var uiDisplayAlerts = false;

      var actionFile = new File("E:/Adobe/Adobe Illustrator CS6 (64 Bit)/Presets/en_US/Actions" + '/optimizationActions.aia');

      var originalPathItems = [];

      var cutterName = "Cutter Path";

      var cutterRed, cutterGreen, cutterBlue;

       

      $.level = 2;

       

      if (app.userInteractionLevel == UserInteractionLevel.DISPLAYALERTS)

      {

          uiDisplayAlerts = true;

      } else {

          uiDisplayAlerts = false;

      }

       

      // Populate our array with all of the path items before modification.

      for (i = 0; i < app.activeDocument.layers[0].pathItems.length; i++) {

          originalPathItems.push (app.activeDocument.layers[0].pathItems[i]);

      }

       

      app.loadAction(actionFile);

       

      if ( app.documents.length > 0 ) {

          if (!app.activeDocument.saved) {

              Window.alert ("This script needs to modify your document. Please save it before running this script.", "Please Save Your Document");

          } else {

              if (app.activeDocument.selection.length < 1) {

                  Window.alert ("You need to have at least one cutter path selected.", "Select A Single Path");

              } else if (app.activeDocument.selection.length > 1) {

                  Window.alert ("You need to have only one cutter path selected.", "Select A Single Path");

              } else {

                  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

                  cutterIndex = app.activeDocument.layers[0].pathItems.length - app.activeDocument.selection[0].zOrderPosition;

                  cutterRed = app.activeDocument.selection[0].fillColor.red;

                  cutterGreen = app.activeDocument.selection[0].fillColor.green;

                  cutterBlue = app.activeDocument.selection[0].fillColor.blue;

                  app.cut();

                  for (i = 0 ; i < originalPathItems.length ; i++) {

                      //$.bp(i > originalPathItems.length - 2 && i < originalPathItems.length);

                      // If this is the bottom path in the stack, we need to do a Unite.

                      if (i == originalPathItems.length - 1) {

                          originalPathItems[i].selected = true;

                          app.doScript('oaPasteInFront', 'optimizationActions', false);

                          originalPathItems[i].selected = true;

                          app.doScript('oaUnite', 'optimizationActions', false);

                          app.selection = null;

                      } else {

                          // If this isn't the bottom path in the stack, we need to do a minus front.

                          if (!originalPathItems[i].hidden && i > cutterIndex &&  originalPathItems[i].fillColor.red != cutterRed  &&  originalPathItems[i].fillColor.green != cutterGreen &&  originalPathItems[i].fillColor.Blue != cutterBlue) {

                              originalPathItems[i].selected = true;

                              app.doScript('oaPasteInFront', 'optimizationActions', false);

                              app.activeDocument.selection[0].name = cutterName;

                              originalPathItems[i].selected = true;

                              app.doScript('oaMinusFront', 'optimizationActions', false);

                              // Now we need to handle the outcomes for a minus front

                              if (app.activeDocument.selection.length == 2 && app.activeDocument.selection[0].name == cutterName) {

                                  // If we've got two selected paths and the first one is the same color as the cutter, it means

                                  // that the cutter completely overlaps the path to be cut and we need to delete them both.

                                  for (i = app.activeDocument.selection.length - 1 ; i >= 0 ; i--) {

                                      app.activeDocument.selection[i].remove();

                                  }

                              } else if (app.activeDocument.selection[0].typename == "GroupItem") {

                                  // If we've got  a group selected, it means the path cut and split the other path into multiple

                                  // paths, so we've got to ungroup them and deselect. them.

                                  var groupToUngroup = app.activeDocument.selection[0];

                                  var pathsToUngroup = [];

                                  var compoundPathsToUngroup = [];

                                  var placementGroup;

                                  var placementPath;

                                  var placementCompoundPath;

                                 

                                  app.selection = null;

                                 

                                  if (groupToUngroup.pathItems.length >= 1) {

                                      for (j = 0 ; j < groupToUngroup.pathItems.length ; j++) {

                                          pathsToUngroup[j] = groupToUngroup.pathItems[j];

                                      }

                                  }

                             

                                  if (groupToUngroup.compoundPathItems.length >= 1) {

                                      for (j = 0 ; j < groupToUngroup.compoundPathItems.length ; j++) {

                                          compoundPathsToUngroup[j] = groupToUngroup.compoundPathItems[j];

                                      }

                                  }

                                 

                                  placementGroup = groupToUngroup;

                                 

                                  if (pathsToUngroup.length >= 1) {

                                      for (j = 0 ; j < pathsToUngroup.length ; j++) {

                                          if (j < 1) {

                                              pathsToUngroup[j].move(placementGroup, ElementPlacement.PLACEBEFORE);

                                              placementPath = pathsToUngroup[j];

                                          } else {

                                              pathsToUngroup[j].move(placementPath, ElementPlacement.PLACEAFTER);

                                              placementPath = pathsToUngroup[j];

                                          }

                                      }

                                  }

                             

                                  if (compoundPathsToUngroup.length >= 1) {

                                      for (j = 0 ; j < compoundPathsToUngroup.length ; j++) {

                                          if (j < 1) {

                                              compoundPathsToUngroup[j].move(placementPath, ElementPlacement.PLACEAFTER);

                                              placementCompoundPath = compoundPathsToUngroup[j];

                                          } else {

                                              compoundPathsToUngroup[j].move(placementCompoundPath, ElementPlacement.PLACEAFTER);

                                              placementCompoundPath = compoundPathsToUngroup[j];

                                          }

                                      }

                                  }

                             

                                  app.selection = null;

                              } else {

                                  // If it's anything else, it either means that the two paths don't overlap at all or the cutter

                                  // overlaps the other path, but doesn't completely chop it into pieces, so we just need to

                                  // deselect the remaining path.

                                  app.selection = null;

                              }

                          }

                      }

                  }

              }

          }

      } else {

          Window.alert("You must open at least one document.", "No Document Open");

      }

       

      app.unloadAction('optimizationActions','');

       

      if (uiDisplayAlerts) {

          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

      } else {

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

      }

       

      $.level = debugLevel;