Illustrator script will not print on the correct printer.

I call an Illustrator using BridgeTalk script within a script InDesign, but I doubt it would make a difference.  Here's the question: we have four printers on the network that are named 'Printer 1', ' printer 2 "," printer 3 ", and"printer 4"on each Mac on the network." "  I want to print a document to a specific printer, based on the computer that is used to run the script.  Pretty simple idea and I have illustrated it in my script in progress below:

function illustratorDie(artFile, dieFolder, companyName, orderNum, shipDate, dieInks)
{ 
          // Before anything else, find out to which printer we'll be sending our document.
          var machine = $.getenv("USER");
          $.writeln("(IL) Environment variable 'USER' is " + machine);
          var localPrinter;
          var shipPrinter;
          switch (machine)
          {
                    case "aerostar":
                              $.writeln("Machine is aerostar.");
                              localPrinter = "Printer 4";
                              shipPrinter = "Shipping";
                              break;
                    case "apache":
                              $.writeln("Machine is apache.");
                              localPrinter = "Printer 3";
                              shipPrinter = "Shipping";
                              break;
                    case "navajo":
                              $.writeln("Machine is navajo.");
                              localPrinter = "Printer 3";
                              shipPrinter = "Shipping";
                              break;
                    case "seneca":
                              $.writeln("Machine is seneca.");
                              localPrinter = "Printer 2";
                              shipPrinter = "Shipping";
                              break;
                    case "warrior":
                              $.writeln("Machine is warrior.");
                              localPrinter = "Printer 2";
                              shipPrinter = "Shipping";
                              break;
                    case "tomahawk":
                              $.writeln("Machine is tomahawk.");
                              localPrinter = "Printer 1";
                              shipPrinter = "Shipping";
                              break;
                    case "Marketing":
                              $.writeln("Machine is Marketing.");
                              localPrinter = "Printer 1";
                              shipPrinter = "Shipping";
                              break;
                    default:
                              $.writeln("Machine has defaulted.");
                              localPrinter = "Printer 3";
                              shipPrinter = "Shipping";
          }
          $.writeln("(IL) Thus, the local printer is " + localPrinter);


          // First, create a new document for the die.
          var dieDoc = app.documents.add();


          // Then open the document that contains the artwork.
          var artDoc = app.open(artFile);


          // Group the art from the document with the artwork,
          // just in case it hasn't already been grouped.
          artDoc.layers[1].locked = true;
          artDoc.selectObjectsOnActiveArtboard();
          var sel = artDoc.selection;
          var newGroup = artDoc.groupItems.add();
          newGroup.name = "ArtGroup";
          newGroup.move(artDoc, ElementPlacement.PLACEATEND);
          for (var i = 0; i < sel.length; i++)
          {
                    sel[i].moveToEnd(newGroup);
          }


          // And copy the artwork from the art document to this new die document.
          var copiedArt = newGroup.duplicate(dieDoc.layers[0],ElementPlacement.PLACEATEND);


          // Now we can close the art document as it will no longer be needed.
          artDoc.close(SaveOptions.DONOTSAVECHANGES);


          // Back to our die document, let's fix up the art now.
          // We do so by first centering it on the page.
          copiedArt.left = ((dieDoc.width - copiedArt.width) / 2);
          copiedArt.top = (dieDoc.height + copiedArt.height) / 2;


          // Then we change all paths to black & white.
          // For this, we will assume that there are no gradients nor raster art.
          var pathsToRemove = new Array;
          var blackColor = new CMYKColor();
          blackColor.cyan = 0;
          blackColor.magenta = 0;
          blackColor.yellow = 0;
          blackColor.black = 100;


          convertBW(copiedArt);


          $.writeln("Removable paths: " + pathsToRemove.length);
          for (var v = 0; v < pathsToRemove.length; v++)
          {
                    pathsToRemove[v].remove();
          }


          // Now specify the contents of that text block.
          var newLine = String.fromCharCode(13);
          var textContents = companyName + newLine;
          textContents += orderNum + newLine;
          textContents += shipDate + newLine;
          textContents += newLine;
          textContents += "W: " + Math.round((copiedArt.width / 72) * 100) / 100 + newLine;
          textContents += "H: " + Math.round((copiedArt.height / 72) * 100) / 100;


          // And finally actually place the text block while formatting it.
          var textDesc = dieDoc.textFrames.add();
          textDesc.position = [36, dieDoc.height - 50];
          textDesc.contents = textContents;
          textDesc.textRange.characterAttributes.size = 36;


          // Let's set print options for the two printers.
          var localPrintOptions = new PrintOptions();
          localPrintOptions.printerName = localPrinter;
          var shipPrintOptions = new PrintOptions();
          shipPrintOptions.printerName = shipPrinter;


          var myJobOptions = new PrintJobOptions();
          myJobOptions.copies = 1;
          shipPrintOptions.jobOptions = localPrintOptions.jobOptions = myJobOptions;


          $.writeln("Printing to " + localPrintOptions.printerName);


          // Now we need to print this text block to the local printer as well as the Shipping printer.
          $.sleep(2000);
          dieDoc.print(localPrintOptions);
          $.sleep(2000);
          dieDoc.print(shipPrintOptions);


          // Before saving the file to an .EPS, we need to hide (or delete) the text block.
          textDesc.remove();


          // Lastly, we need to save this document as an .EPS file in the proper location.
          // Set the SaveOptions first, just like the PrintOptions earlier.
          var dieOptions = new EPSSaveOptions
          {
                    compatibility = Compatibility.ILLUSTRATOR16;
                    preview = EPSPreview.TRANSPARENTCOLORTIFF;
                    embedAllFonts = true;
                    cmykPostScript = true;
                    postScript = EPSPostScriptLevelEnum.LEVEL2;
          }


          // And then actually save the document.
          var savedFile = unescape(dieFolder.toString()) + "/" + artFile.name.slice(0,-4) + "_Die.eps";
          $.writeln("Inside Illustrator script, savedFile is " + savedFile);
          dieDoc.saveAs(File(savedFile), dieOptions);


          // Finally, close the document.
          dieDoc.close();
          return savedFile;


          // END OF MAIN.

        function blackIt (currentItem)
        {
          ...
         }
}

I at first thought that the script was running too fast, so I added the two "$.sleep (2000)" orders near the end to see if it would help - it didn't.

The bits of important, after defining the variables localPrinter and shipPrinter, come after the line / / Let's define the printing options for both printers.

Instead of print to the local printer, sometimes it will print to the other.  For example, I am the machine called "apache".  The script must localPrinter on "Printer 3".  And, according to the statement of $. writeln() , he does.  However, despite this setting, almost always displays "Printer 2"!  From time to time he actually does it print "Printer 3".

This happens on other systems, too.  It almost seems as if it prints to a printer that is numerically the one less than that to which it is supposed to print.  "aerostar" prints often to "3", "warrior" often it prints for "printer 1", and then the 'marketing' impressions, we do not yet all printers output a page!

What is the cause to happen and, more importantly, what can I do about it?  Help, please!

Of course, I get another set of printers listed.  Nevertheless, I found a solution for this.  Since it seems that the printerName of PrintOptions property is buggy, I decided to try the printPreset property instead.  I created of the presets on each computer, one for each printer and the accordingly modified script.  Works like a charm now.  Now the switch statement:

switch (machine)
{
          case "aerostar":
                    $.writeln("Machine is aerostar.");
                    localPrinter = "Printer 4";
                    localPrintPreset = "Color Proof Printer 4";
                    shipPrinter = "Shipping";
                    break;
          case "apache":
                    $.writeln("Machine is apache.");
                    localPrinter = "Printer 3";
                    localPrintPreset = "Color Proof Printer 3";
                    shipPrinter = "Shipping";
                    break;
          case "navajo":
                    $.writeln("Machine is navajo.");
                    localPrinter = "Printer 3";
                    localPrintPreset = "Color Proof Printer 3";
                    shipPrinter = "Shipping";
                    break;
          case "seneca":
                    $.writeln("Machine is seneca.");
                    localPrinter = "Printer 2";
                    localPrintPreset = "Color Proof Printer 2";
                    shipPrinter = "Shipping";
                    break;
          case "warrior":
                    $.writeln("Machine is warrior.");
                    localPrinter = "Printer 2";
                    localPrintPreset = "Color Proof Printer 2";
                    shipPrinter = "Shipping";
                    break;
          case "tomahawk":
                    $.writeln("Machine is tomahawk.");
                    localPrinter = "Printer 1";
                    localPrintPreset = "Color Proof Printer 1";
                    shipPrinter = "Shipping";
                    break;
          case "Marketing":
                    $.writeln("Machine is Marketing.");
                    localPrinter = "Printer 1";
                    localPrintPreset = "Color Proof Printer 1";
                    shipPrinter = "Shipping";
                    break;
          default:
                    $.writeln("Machine has defaulted.");
                    localPrinter = "Printer 3";
                    localPrintPreset = "Color Proof Printer 3";
                    shipPrinter = "Shipping";
}

and the transfer block that accompanies it, which uses the new variable:

// Let's set print options for the two printers.
var localPrintOptions = new PrintOptions();
localPrintOptions.printPreset = localPrintPreset;
var shipPrintOptions = new PrintOptions();
shipPrintOptions.printerName = shipPrinter;

I hope this will help someone else who is faced with this problem!

Tags: Illustrator

Similar Questions

Maybe you are looking for