Skip navigation
WayneLP
Currently Being Moderated

Help with 2 topics: creating a bounding rectangle and selecting particular objects

Aug 23, 2012 4:32 AM

Tags: #javascript #illustrator_cs4 #help_with

Hi All,

 

I'm extremely new to scripting; I managed to create a script to extend the artboard of my documents (which I was extremely proud of given my inexperience!) and another to align a couple of objects alongside each other, but my attempts to script some new code has met with failure.

 

Firstly, I need some Illustrator CS4 Javascript code that will create a bounding rectangle for the selected object(s). The selected object(s) will always be rectangular; no irregularly-shaped objects. I know this should be simple, but I can't work out the variables/attributes involved.

 

Secondly, some code to help selecting the leftmost object in a document. And the equivalent for the rightmost object.

 

If it makes any difference, I'll be stitching these scripts together with an action to achieve the following workflow.

  1. Two EPS files are dragged into an empty document as 'Linked Files'.
  2. The two Linked Files are aligned alongside each other. (Using a script I've already written.)
  3. The leftmost Linked File is selected and has a bounding rectangle created, which is then used to mask the Linked Object.
  4. The rightmost Linked File is then selected and has a bounding rectangle created, which is then used to mask the Linked Object.
  5. Both masked Linked Files are selected, and a bounding rectangle is created to mask them into the same Clipping Mask group.
  6. The artboard is set to the dimensions of this combined masked pair (I've managed to write that script already).
  7. The Linked Files are embedded into the document.

 

The reason for this is because we have EPS map files exported from AutoCAD that are left- and right-hand pages. These need to be combined into one file, but when they are brought in they lose their masks as the BBOX element is not recognised by Illustrator. The double-masking in my process above is needed to ensure that content from either map doesn't overlap the other half of the map.

 

Any assistance would be greatly appreciated, and sorry for the long-winded post!

Thanks,

WayneM@LP

 
Replies
  • Currently Being Moderated
    Aug 23, 2012 9:00 AM   in reply to WayneLP

    Well my first question is are all your CAD *.eps files the same size bounds…? As I would create one doc, place links, mask, save as… Loop the file list placing R&L linked pairs, save as for each…

     

    Not long-winded at all… Scriptable job yes ( if you have lots to do ) Do you always mask by the same amount too… May have more questions…

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 24, 2012 3:59 AM   in reply to WayneLP

    Placing pairs in a document is easy enough… Getting the size after importing too… Just resize the artboard to content at end…

     

    The only piece of logic you can't make on-the-fly here is ( single EPS or paired EPS's )…

     

    If they were named up in a logical manner then you should be able to do the lot with script alone one click and no actions…

     

    Eg:

     

    autoCAD_01.eps

    autoCAD_02_L.eps

    autoCAD_02_R.eps

    autoCAD_03.eps

    and so on…

     

    The masks are always exactly the same dimensions as the LInked Files… Hum what do they mask then or did you just want a group?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 24, 2012 6:42 AM   in reply to WayneLP

    With 2 placed *.eps files… This should give you then general idea… Untidy yes but works?

     

    #target illustrator
     
    joinEPSLinks();
     
    function joinEPSLinks() {
     
              var doc = app.activeDocument;
      
              doc.defaultFilled = doc.defaultStroked = false;
     
              var epsLeft = doc.placedItems[0];
      
              var epsRight = doc.placedItems[1];
     
              epsRight.top = epsLeft.top;
     
              epsRight.left = epsLeft.visibleBounds[2];
     
              doc.artboards[0].artboardRect = doc.visibleBounds;
      
              app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
      
              var leftMask = doc.pathItems.rectangle( 0, 0, epsLeft.visibleBounds[2], doc.height );
      
              var rightMask = doc.pathItems.rectangle( 0, epsRight.visibleBounds[0], epsRight.visibleBounds[2]-epsRight.visibleBounds[0], doc.height );
      
              var docMask = doc.pathItems.rectangle( 0, 0, doc.width, doc.height );
      
              var grpLeft = doc.groupItems.add();
      
              epsLeft.move( grpLeft, ElementPlacement.PLACEATBEGINNING );
      
              leftMask.move( grpLeft, ElementPlacement.PLACEATBEGINNING );
      
              grpLeft.pathItems[0].clipping = true;
      
              var grpRight = doc.groupItems.add();
      
              epsRight.move( grpRight, ElementPlacement.PLACEATBEGINNING );
      
              rightMask.move( grpRight, ElementPlacement.PLACEATBEGINNING );
      
              grpRight.pathItems[0].clipping = true;
      
              var grpDoc = doc.groupItems.add();
      
              grpLeft.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
      
              grpRight.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
      
              docMask.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
      
              grpDoc.pathItems[0].clipping = true;
     
    };
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 25, 2012 4:57 AM   in reply to WayneLP

    Oooops that's my errror ( overlooked you said CS4 ). Artboards were brought in with CS4 but I may have used options that are CS5+ sorry about that I will have to look it up… I don't have CS4 to check in. Yes script can embed the links… This may depend on where your app co-ord is set… Some users move it… The masks end up below the artboard for me now…?

     

    #target illustrator
     
    joinEPSLinks();
     
    function joinEPSLinks() {
     
              var doc = app.activeDocument;
     
              doc.defaultFilled = doc.defaultStroked = false;
     
              var epsLeft = doc.placedItems[0];
     
              var epsRight = doc.placedItems[1];
     
              epsRight.top = epsLeft.top;
     
              epsRight.left = epsLeft.visibleBounds[2];
     
              doc.artboards[0].artboardRect = doc.visibleBounds;
     
              app.redraw();
     
              doc.rulerOrigin = [ 0, 0 ]; // or try doc.rulerOrigin = [ 0, -doc.height ]
     
              var leftMask = doc.pathItems.rectangle( 0, 0, epsLeft.visibleBounds[2], doc.height );
     
              var rightMask = doc.pathItems.rectangle( 0, epsRight.visibleBounds[0], epsRight.visibleBounds[2]-epsRight.visibleBounds[0], doc.height );
     
              var docMask = doc.pathItems.rectangle( 0, 0, doc.width, doc.height );
     
              var grpLeft = doc.groupItems.add();
     
              epsLeft.move( grpLeft, ElementPlacement.PLACEATBEGINNING );
     
              leftMask.move( grpLeft, ElementPlacement.PLACEATBEGINNING );
     
              grpLeft.pathItems[0].clipping = true;
     
              grpLeft.clipped = true;
     
              var grpRight = doc.groupItems.add();
     
              epsRight.move( grpRight, ElementPlacement.PLACEATBEGINNING );
     
              rightMask.move( grpRight, ElementPlacement.PLACEATBEGINNING );
     
              grpRight.pathItems[0].clipping = true;
     
              grpRight.clipped = true;
     
              var grpDoc = doc.groupItems.add();
     
              grpLeft.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
     
              grpRight.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
     
              docMask.move( grpDoc, ElementPlacement.PLACEATBEGINNING );
     
              grpDoc.pathItems[0].clipping = true;
     
              grpDoc.clipped = true;
     
              doc.groupItems[0].groupItems[0].placedItems[0].embed();
     
              doc.groupItems[0].groupItems[1].placedItems[0].embed();
     
              app.redraw();
     
    };
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 5:26 AM   in reply to WayneLP

    Wayne, thanks but no need… as long as you mark posts as helpful or correct that will be fine… It's out here for anyone to find…

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points