Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Copy selection to a new document

Engaged ,
Nov 04, 2014 Nov 04, 2014

Here is a break down of what I am wanting to accomplish....any guidance would be appreciated!

1. User will select items (text frames, compound path items, path items)

2. User will run a script that will do the following....

     1. Open a new document (8.5" x 11") with RGB color

     2. Anything with a fill color should get gray color 100% fill applied

     3. Anything with a stroke color should get gray color 100% stroke applied

     4. Then save a .ai file and a .pdf file in the following format.....

#target illustrator

var doc = app.activeDocument;

var fileName = doc.name;

var jobName = (fileName).substr(0, 11);

aiFile = "D:\\Jobs\\"+jobName+"\\AI\\Cover Page.ai" 

pdfFile = "D:\\Jobs\\"+jobName+"\\PDF\\Cover Page.pdf" 

var newaiFile = new File(aiFile); 

doc.saveAs (newaiFile); 

 

var pdfOpts = new PDFSaveOptions();     

pdfOpts.pDFXStandard=PDFXStandard.PDFXNONE; 

pdfOpts.compatibility = PDFCompatibility.ACROBAT5;     

pdfOpts.preserveEditability = false; 

var newpdfFile = new File(pdfFile);     

doc.saveAs(newpdfFile, pdfOpts);

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Dec 02, 2014 Dec 02, 2014

OK so I got this squared away to where the art board moves to the selected items. Someone else wrote the code to move the art board to the selected items and I modified it to fit my needs. So that being said big thanks to pixxxel schubser for the code for that part (found here... https://forums.adobe.com/thread/1336506 )

Here is the code for anyone to see/modify/use!

#target illustrator

var newDocSet = new DocumentPreset;

newDocSet.title = "Cover"

newDocSet.width = (8.5 * 72);

newDocSet.height

...
Translate
Adobe
Community Expert ,
Nov 04, 2014 Nov 04, 2014

this is how to duplicate the active document, from the documentation...

Duplicating the active document

// Duplicates any selected items from

// the active document into a new document.

var newItem;

var docSelected = app.activeDocument.selection;

if ( docSelected.length > 0 ) {

    // Create a new document and move the selected items to it.

    var newDoc = app.documents.add();

    if ( docSelected.length > 0 ) {

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

            docSelected.selected = false;

            newItem = docSelected.duplicate( newDoc, ElementPlacement.PLACEATEND );

        }

    }

    else {

        docSelected.selected = false;

        newItem = docSelected.parent.duplicate( newDoc, ElementPlacement.PLACEATEND );

    }

}

else {

    alert( "Please select one or more art objects" );

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 04, 2014 Nov 04, 2014

I was putting something together for this.

does anyone know why the second if statement exists in the above code. it seems to work fine with only one.

Also does anyone know how to set the documents units? i can set RulerUnits but cant find documentation on how to set document units.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 05, 2014 Nov 05, 2014

Thanks for the code Carlos!

imageCollection...I also don't understand the need for the extra if statement. So I have removed it from the code I have modified and set it to create a 8.5" x 11" RGB document.

Here is the updated code

#target illustrator

// Duplicates any selected items from the active document into a new document.

var newItem;

var docSelected = app.activeDocument.selection;

if ( docSelected.length > 0 ) {

    // Create a new document and move the selected items to it.

  // RGB Document, 8.5" x 11" (Document size is set in points)

    var newDoc = app.documents.add(DocumentColorSpace.RGB, 612, 792);

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

            docSelected.selected = false;

            newItem = docSelected.duplicate( newDoc, ElementPlacement.PLACEATEND );

        }

    } 

else {

    alert( "Please select one or more art objects" );

}

Now when it duplicates the items it isn't placing them centered on the artboard. Here is a screen shot zoomed out to 12.5% of where it places the items....

Capture.JPG

Any ideas on that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 06, 2014 Nov 06, 2014

This has some user set options at the top which may make it something others can use.

Also should help with your position problem.

Though its not perfect gets it close.

it also does not set the actual document units, Still can't find that...

let me know how it goes for you.

// Puts Selected items in a new document

// New Document Settings

    var newDocSet = new DocumentPreset;

    newDocSet.title = "My New Document"

  var unit = "inch"; // keep to "inch" or "mm"

  var width = "8.5";

  var height = "11";

  var color = "RGB"; // keep to "RGB" or "CMYK"

  var reposition = "true"; // Set to true or false

//

var doc = app.activeDocument;

var art = doc.selection;

var newArt;

  if(art.length > 0){

    if(unit == "inch" || unit == "mm" && color == "RGB" || color == "CMYK"){

       if(unit == "inch"){newDocSet.width = width * 72; newDocSet.height = height * 72; newDocSet.RulerUnits = "Inches";}

       if(unit == "mm"){newDocSet.width = width * 2.834645; newDocSet.height = height * 2.834645; newDocSet.RulerUnits = "Millimeters";}

       if(color == "RGB"){var newDoc = app.documents.addDocument(DocumentColorSpace.RGB,newDocSet);}

       if(color == "CMYK"){var newDoc = app.documents.addDocument(DocumentColorSpace.CMYK,newDocSet);}

       newDoc.rulerOrigin = [0,0];

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

         var pos = art.visibleBounds;

         art.selected = false;

         newArt = art.duplicate(newDoc, ElementPlacement.PLACEATEND);

         if (reposition == "true"){

           var pos2 = newArt.visibleBounds;

           var X = pos[0] - pos2[0];

           var Y = pos[3] - pos2[3];

           newArt.translate(X,Y, true, true, true, true);

         }

       }

    }else{alert("Units or Colorspace not set correctly");}

  }else{alert("nothing selected");}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2014 Nov 07, 2014

Look under DocumentPreset for

unitsRulerUnits:
RulerUnits.Unknown
RulerUnits.Inches
RulerUnits.Centimeters
RulerUnits.Points
RulerUnits.Picas
RulerUnits.Millimeters
RulerUnits.Qs
RulerUnits.Pixels
r/wThe units for the new document. (default: RulerUnits.Points)

Used in:

Document Documents.addDocument (startupPreset: string, presetSettings: DocumentPreset)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 09, 2014 Nov 09, 2014

Thanks Larry G. Schneider,

took another look at my code.

I had the statement as:

newDocSet.RulerUnits = "Inches";

this should be:

newDocSet.units = RulerUnits.Inches;

edited above code to reflect this.

Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 09, 2014 Nov 09, 2014

Fixed Code:

// Puts Selected items in a new document 

// New Document Settings

    var newDocSet = new DocumentPreset; 

    newDocSet.title = "My New Document" 

  var unit = "inch"; // keep to "inch" or "mm" 

  var width = "8.5"; 

  var height = "11"; 

  var color = "RGB"; // keep to "RGB" or "CMYK" 

  var reposition = "true"; // Set to true or false 

// 

var doc = app.activeDocument; 

var art = doc.selection; 

var newArt; 

  if(art.length > 0){ 

    if(unit == "inch" || unit == "mm" && color == "RGB" || color == "CMYK"){ 

       if(unit == "inch"){newDocSet.width = width * 72; newDocSet.height = height * 72; newDocSet.units = RulerUnits.Inches;} 

       if(unit == "mm"){newDocSet.width = width * 2.834645; newDocSet.height = height * 2.834645; newDocSet.units = RulerUnits.Millimeters;} 

       if(color == "RGB"){var newDoc = app.documents.addDocument(DocumentColorSpace.RGB,newDocSet);} 

       if(color == "CMYK"){var newDoc = app.documents.addDocument(DocumentColorSpace.CMYK,newDocSet);} 

       newDoc.rulerOrigin = [0,0]; 

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

         var pos = art.visibleBounds; 

         art.selected = false; 

         newArt = art.duplicate(newDoc, ElementPlacement.PLACEATEND);

         if (reposition == "true"){ 

           var pos2 = newArt.visibleBounds; 

           var X = pos[0] - pos2[0];  

           var Y = pos[3] - pos2[3]; 

           newArt.translate(X,Y, true, true, true, true); 

         } 

       } 

    }else{alert("Units or Colorspace not set correctly");} 

  }else{alert("nothing selected");}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 02, 2014 Dec 02, 2014
LATEST

OK so I got this squared away to where the art board moves to the selected items. Someone else wrote the code to move the art board to the selected items and I modified it to fit my needs. So that being said big thanks to pixxxel schubser for the code for that part (found here... https://forums.adobe.com/thread/1336506 )

Here is the code for anyone to see/modify/use!

#target illustrator

var newDocSet = new DocumentPreset;

newDocSet.title = "Cover"

newDocSet.width = (8.5 * 72);

newDocSet.height = (11 * 72);

newDocSet.units = RulerUnits.Inches;

newDocSet.colorMode = DocumentColorSpace.RGB;

var doc = app.activeDocument;

var selectedItems = doc.selection;

if (selectedItems.length > 0) {

    var newDoc = app.documents.addDocument(DocumentColorSpace.RGB, newDocSet);

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

        var newArt = selectedItems.duplicate(newDoc, ElementPlacement.INSIDE);

    }

} else {

    alert("nothing selected");

}

// to move the new document artboard over to the selected items

var doc = app.activeDocument;

var selectedItems = doc.selection;

 

    var abIdx = doc.artboards.getActiveArtboardIndex(); 

    var actAbBds = doc.artboards[abIdx].artboardRect; 

 

    var vBounds = selectedItems[0].visibleBounds; 

    vBounds_Li = vBounds[0]; 

    vBounds_Ob = vBounds[1]; 

    vBounds_Re = vBounds[2]; 

    vBounds_Un = vBounds[3]; 

 

if (selectedItems.length >1 ) {     

    for (i=1; i<selectedItems.length ; i++) { 

        vBdsI = selectedItems.visibleBounds; 

        if( vBounds_Li > vBdsI[0] ) {vBounds_Li = vBdsI[0]}; 

        if( vBounds_Ob < vBdsI[1] ) {vBounds_Ob = vBdsI[1]}; 

        if( vBounds_Re < vBdsI[2] ) {vBounds_Re = vBdsI[2]}; 

        if( vBounds_Un > vBdsI[3] ) {vBounds_Un = vBdsI[3]}; 

        } 

 

    doc.artboards[abIdx].artboardRect = [vBounds_Li +((vBounds_Re - vBounds_Li)/2-(actAbBds[2]-actAbBds[0])/2), vBounds_Ob -((vBounds_Ob - vBounds_Un)/2+(actAbBds[3]-actAbBds[1])/2), vBounds_Li +((vBounds_Re - vBounds_Li)/2-(actAbBds[2]-actAbBds[0])/2)+(actAbBds[2]-actAbBds[0]), vBounds_Ob -((vBounds_Ob - vBounds_Un)/2+(actAbBds[3]-actAbBds[1])/2)+(actAbBds[3]-actAbBds[1])]; 

    }

// save .ai and .pdf file to correct locations, then close the file

var allDocs = app.documents;

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

var fileName = allDocs[1].name;

var jobName = (fileName).substr(0, 11);

aiFile = "D:\\Job Number\\"+jobName+"\\AI\\Cover.ai" 

pdfFile = "D:\\Job Number\\"+jobName+"\\PDF\\Cover.pdf" 

var newaiFile = new File(aiFile); 

var doc = app.activeDocument; 

doc.saveAs (newaiFile);

saveAI();

 

var pdfOpts = new PDFSaveOptions();     

pdfOpts.pDFXStandard=PDFXStandard.PDFXNONE; 

pdfOpts.compatibility = PDFCompatibility.ACROBAT5;     

pdfOpts.preserveEditability = false; 

var newpdfFile = new File(pdfFile);     

doc.saveAs(newpdfFile, pdfOpts);

}

function saveAI( path, name, id ) {

  var saveOpts = new PDFSaveOptions();

  saveOpts.compatibility = PDFCompatibility.ACROBAT6;

}

doc.close(SaveOptions.SAVECHANGES);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines