4 Replies Latest reply on Jun 25, 2013 12:49 AM by AndyWizz

    Start a new Document with Millimeters

    AndyWizz Level 1

      Hi there,
      I am new to Illustratror Scripting.

      I changed a Script from William Ngan, http://www.metaphorical.net and http://forums.adobe.com/message/3813716?tstart=1

      It saves EPS-files instead of AI-Files. But the Units are still in PT. I want them to be in Millimeters and the artboard isn't focused on the viewport.
      I suppose it's RulerUnits.Millimeters but somehow it doesn't work and when I pick a line it shows pt. I don't know how to write it. Perhaps you find the solution.

      I deleted the Comments of William so it's not as long as the original. I tried at the line:

      var newdoc = app.documents.add ( doc.documentColorSpace, doc.width, doc.height );

       

      I thought this would help:

      newdoc.RulerUnits = doc.RulerUnits;

      But it doesn't work. Hope it shows the idea.

       

      /* Here the Script starts */

       

      var doc = app.activeDocument;

      var docname = (doc.name.split('.'))[0]; // name

      var doc_artboard = doc.artboards[0].artboardRect;

       

      if (app.documents.length > 1) {

              alert( "Nur ein Dokument darf geöffnet sein. Schließen Sie andere Dokumente und führen Sie das Script erneut aus.");

      } else {

       

          var ok = confirm( "Bitte speichern Sie zuerst Ihr Original.\nDie Ebenen werden im gleichen Ordner wie Ihre Datei gespeichert.\nWeiter?" );

       

          if (ok) {

       

              // prepare layers

              for(var i=0; i<doc.layers.length; i++) {

                  doc.layers[i].visible = false;

              }

       

       

              // go through each layers

              for(var i=0; i<doc.layers.length; i++) {

                  app.activeDocument = doc;

       

       

                  if (i>0) doc.layers[i-1].visible = false;

                  doc.layers[i].visible = true;

                  doc.activeLayer = doc.layers[i];

       

                  saveAI( doc.path, doc.activeLayer.name, i );

              }

       

       

              // close original file without saving

              doc.close( SaveOptions.DONOTSAVECHANGES );

       

          }

       

      }

       

       

      function saveAI( path, name, id ) {

       

          var currlayer = doc.layers[id];   

          var g = currlayer.groupItems.add();

          group( g, currlayer.pageItems );   

       

          var t = g.top;

          var l = g.left;

       

          /*

          var myPreset = app.getPresetSettings("Druck");

          app.documents.addDocument("Druck", myPreset );

          */

       

          var newdoc = app.documents.add ( doc.documentColorSpace, doc.width, doc.height );

          newdoc.artboards[0].artboardRect = doc_artboard;

          /* app.preferences.rulerUnits = Units.Millimeters; */

          /* app.activeDocument.rulerUnits = RulerUnits.Millimeters; */

          /* newdoc.Units = RulerUnits.Millimeters; */ 

          var newlayer = newdoc.layers[0];

       

          g.duplicate( newlayer, ElementPlacement.PLACEATBEGINNING );

          newlayer.pageItems[0].top = t;

          newlayer.pageItems[0].left = l;

       

          path.changePath( name+".eps" );

       

          var saveOpts = new EPSSaveOptions();

          /* saveOpts.compatibility = ILLUSTRATOR16; */

          saveOpts.embedLinkedFiles = true;

          saveOpts.includeDocumentThumbnails = true;

          saveOpts.embedAllFonts = true;

          saveOpts.saveMultipleArtboards = false;

          saveOpts.cmykPostScript = true;

          saveOpts.preview = EPSPreview.TRANSPARENTCOLORTIFF;

       

       

       

          newdoc.saveAs( path, saveOpts );

          newdoc.close( SaveOptions.DONOTSAVECHANGES );

       

       

          // wait for the new file to save and close before continue.

          // A callback function (if possible) will be better than a while loop for sure.

          while (app.documents.length > 1) {

              continue;

          }

      }

       

       

      function group( gg, items ) {

       

          var newItem;

          for(var i=items.length-1; i>=0; i--) {

       

       

              if (items[i]!=gg) {

                  newItem = items[i].move (gg, ElementPlacement.PLACEATBEGINNING);

              }

          }

          return newItem;

      }

        • 1. Re: Start a new Document with Millimeters
          Muppet Mark Level 5

          All AI scripting is done in points… There are NO options to change this with either the app or the document…

           

          The choices are use your own math or use the UnitValue() class…

           

          var mm = 2.83464567;
            
          var doc = app.documents.add(
              DocumentColorSpace.CMYK,
              100*mm,
              150*mm,
              1,
              DocumentArtboardLayout.Row,
              72
          );
          
          • 2. Re: Start a new Document with Millimeters
            AndyWizz Level 1

            Thanks for your help!!!

            I think it's much simpler. I wanted to override the presets of the new document (preferences). The objects and the artboard sould have the same size and measurement.

            • 3. Re: Start a new Document with Millimeters
              pixxxel schubser Level 5

              For new documents you can use (or "override") an existing document presets in your script e.g. like this:

               

               

              var w = 210*2.834645;
              var h = 297*2.834645;
              // var w = doc.width;
              // var h = doc.height;
              
              var myPreset = new DocumentPreset;
              myPreset.width = w;
              myPreset.height = h;
              myPreset.units = RulerUnits.Millimeters;
              myPreset.title = 'test document';
              var newdoc = documents.addDocument( "//an_existing_preset//", myPreset);
              

               

              The ruler units are now in Millimeters. And also you can use the doc.width and doc.height.

              • 4. Re: Start a new Document with Millimeters
                AndyWizz Level 1

                Muppet Mark and pixxxel schubser were totally right! I thank both of you!

                Now it's time for me to share the working version. The Preset "Druck" is for the german version. It's "Print" for the international.

                The Script saves layers as individual EPS-Files. The RulerUnits are Millimeters not Point. The viewport is still not focused on the Artboard when opening the document but that's ...

                 

                var doc = app.activeDocument;

                var docname = (doc.name.split('.'))[0]; // name

                var doc_artboard = doc.artboards[0].artboardRect;

                 

                if (app.documents.length > 1) {

                        alert( "Nur ein Dokument darf geöffnet sein. Schließen Sie andere Dokumente und führen Sie das Script erneut aus.");

                } else {

                 

                    var ok = confirm( "Bitte speichern Sie zuerst Ihr Original.\nDie Ebenen werden im gleichen Ordner wie Ihre Datei gespeichert.\nWeiter?" );

                 

                    if (ok) {

                       

                        // prepare layers

                        for(var i=0; i<doc.layers.length; i++) {

                            doc.layers[i].visible = false;

                        }

                 

                        // go through each layers

                        for(var i=0; i<doc.layers.length; i++) {

                            app.activeDocument = doc;

                 

                            if (i>0) doc.layers[i-1].visible = false;

                            doc.layers[i].visible = true;

                            doc.activeLayer = doc.layers[i];

                 

                            saveEPS( doc.path, doc.activeLayer.name, i );

                        }

                 

                        // close original file without saving

                        doc.close( SaveOptions.DONOTSAVECHANGES );

                   

                    }

                 

                }

                 

                function saveEPS( path, name, id ) {

                   

                    var currlayer = doc.layers[id];   

                    var g = currlayer.groupItems.add();

                    group( g, currlayer.pageItems );   

                   

                    var t = g.top;

                    var l = g.left;

                 

                    var w = doc.width;

                    var h = doc.height;

                   

                    /*

                    var w = 31.1*2.834645;

                    var h = 28.15*2.834645;

                    */

                   

                    var myPreset = new DocumentPreset;

                    myPreset.width = w;

                    myPreset.height = h;

                    myPreset.units = RulerUnits.Millimeters;

                    myPreset.title = docname;

                    var newdoc = documents.addDocument( "Druck", myPreset);

                    newdoc.artboards[0].artboardRect = doc_artboard;

                    var newlayer = newdoc.layers[0];

                   

                    g.duplicate( newlayer, ElementPlacement.PLACEATBEGINNING );

                    newlayer.pageItems[0].top = t;

                    newlayer.pageItems[0].left = l;

                 

                    path.changePath( name+".eps" );

                 

                    var saveOpts = new EPSSaveOptions();

                    saveOpts.compatibility = Compatibility.ILLUSTRATOR16;

                    saveOpts.embedLinkedFiles = true;

                    saveOpts.includeDocumentThumbnails = true;

                    saveOpts.embedAllFonts = true;

                    saveOpts.saveMultipleArtboards = false;

                    saveOpts.cmykPostScript = true;

                    saveOpts.preview = EPSPreview.TRANSPARENTCOLORTIFF;   

                               

                    newdoc.saveAs( path, saveOpts );

                    newdoc.close( SaveOptions.DONOTSAVECHANGES );   

                               

                    // wait for the new file to save and close before continue.

                    // A callback function (if possible) will be better than a while loop for sure.

                    while (app.documents.length > 1) {

                        continue;

                    }

                }

                 

                function group( gg, items ) {

                 

                    var newItem;

                    for(var i=items.length-1; i>=0; i--) {

                       

                        if (items[i]!=gg) {

                            newItem = items[i].move (gg, ElementPlacement.PLACEATBEGINNING);

                        }

                    }

                    return newItem;

                }