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

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

Community Beginner ,
Aug 23, 2012 Aug 23, 2012

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

TOPICS
Scripting
3.2K
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

Guru , Aug 25, 2012 Aug 25, 2012

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

...
Translate
Adobe
Guru ,
Aug 23, 2012 Aug 23, 2012

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…

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 Beginner ,
Aug 23, 2012 Aug 23, 2012

Hi Muppet Mark; thanks for the reply.

To answer your questions:

  • The EPS files are not all the same size. Each RHS and LHS pair has the same height, but may vary in width. (For example the LHS may be a page wide, while the RHS may be half a page wide.) Thus my need for a script that creates a bounding rectangle that uses the size of the selected object rather than a fixed size each time. I actually already have a script that does this, but it's extremely over-complex and cumbersome for what I need and it involves a dialog box with options that ruins my hopes for integrating it into an action. I simply need a way to generate a rectangle that's the same size and coordinates as an existing selected rectangular object.
  • These scripts are needed for a whole process that's converting map files created for printed books into use in eBooks. Some are from AutoCAD, some are from ArcMap, and the layer structure differs across the maps. Some are LHS/RHS pairs that need to be rejoined, some aren't. So running through a folder and masking the files, then running through to rejoin them isn't the best option as different tasks need to happen to the files depending on the map type. Ideally I want to have a single action for the masking/joining that does some steps in the action and calls some small scripts for other parts.
  • The masks are always exactly the same dimensions as the LInked Files.

However, I was doing some research just now, and I've learned that it's impossible to create an action that calls a script, as the action constantly 'forgets' the script. So my plan might be all for naught unless I can work out a way to put together all my steps into a single action. Blerg.

Thanks,

WayneM@LP

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
Guru ,
Aug 24, 2012 Aug 24, 2012

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?

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 Beginner ,
Aug 24, 2012 Aug 24, 2012

The pairs always have -lhs and -rhs in their filename, and it's only the pairs that need to have this joining done. All the files have other changes to be made (removing page references, adding titles, and cosmetic changes such as that), but it's only a way to join up the RHS and LHS pairs that I'm looking for here. Batching a solution to join a mass of files at the same time isn't really the issue here, it's just speeding up the joining of particular pairs one pair at a time, and ensuring it's very precisely done with no human error.

When the maps are dropped in as Linked Files, they appear as if the BBOX is still recognised, and the content outside the page area can't be seen. But when they're embedded (or simply opened in Illustrator), the external content can be seen. So I'd like to bring them in as Linked Files, create rectangles to mask them, and then embed at the end once they're masked in a way that Illustrator recognises.

The final mask around the paired EPS files is needed as sometimes the maps will have keys underneath that need to be masked out after the joining is done. I was looking for a way to add it in exactly snapped to the visible bounds of the individually masked maps so that it was ready to mask out the docked keys if needed.

To be honest, the process I have  is simple enough in my opinion. Drag in the linked files, run my script to align then, run another script once for each Linked File to add bounding rectangles, then mask them off individually, then use the same script again to add a top-level bounding rectangle and mask the two together before embedding them. It literally takes me less than a minute to do, but my cartographers were complaining about it. They want to just use totally familiar practises like copying & pasting maps together and aligning them by hand, but the copying/pasting slows their PCs down (these are very complex maps), and I don't want to risk them not snapping the halves together exactly as there are other scripts later for eBook conversion that require precise artboard measurements to be preserved. Also, working with the maps as embedded files until the very end keeps the process quicker.

Thus my desire to find some kind of a one-click script that runs the files through the process I've outlined above.

Here's my plain English version of what I think needs to happen.

  1. Cartographer creates new blank document
  2. Cartographer drags LHS and RHS documents into blank document, and ensures LHS is on top (so Illustrator always knows which Linked File is which).
  3. Script is run, which
    1. Sets top-right corner of LHS Linked File to match the top-left corner of RHS Linked File
    2. Creates myRectangle1, whose dimensions/location match those of LHS Linked File
    3. Masks LHS Linked File with myRectangle1
    4. Creates myRectangle2, whose dimensions/location match those of RHS Linked File
    5. Masks RHS Linked File with myRectangle2
    6. Creates myRectangle3, whose dimensions/location match those of the LHS & RHS LInked Files alongside each other
    7. Masks LHS & RHS Linked Files with myRectangle3
    8. Embeds Image for all Linked Files
  4. Cartographer uses existing script to set artboard to the dimensions of the top-level mask (myRectangle3), then saves document and performs other manual conversion tasks.
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
Guru ,
Aug 24, 2012 Aug 24, 2012

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;

};

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 Beginner ,
Aug 24, 2012 Aug 24, 2012

Wow - that's so close I can almost smell it!

However, when I run it, there's a few problems...

  1. I'm running it through the ExtendScript Toolkit directly into Adobe Illustrator CS4, and it stalls at this line: "app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;"
    When I comment this line out, it manages to get through the entire action. Not sure what it doesn't like about that line?
  2. The end result doesn't leave the Linked Files in Clipping Path masked groups, just regular groups.
  3. The mask objects don't end up aligned to the Linked Files.
    You can see a screenshot that demonstrates the problems of #2 and #3 here:
    combo-test-error1.jpg
    (http://i.imgur.com/V25vH.jpg)
  4. Is there a way to Embed Image with the two Linked Files at the end of the script? If not we can do that step manually using the Links palette flyout menu.

Thanks so much for your help so far!

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
Guru ,
Aug 25, 2012 Aug 25, 2012

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();

};

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 Beginner ,
Aug 26, 2012 Aug 26, 2012

Yes! You're a guru.

Worked perfectly when I changed "doc.rulerOrigin = [ 0, 0 ]" to "doc.ruler.Origin = [" 0, doc.height ]" (because Illustrator counts up from the bottom left-hand corner, presumably?)

Would you mind if I credit you in the comments lines of my new script? Is there a better way to do so other than Muppet Mark @ forums.adobe.com?

Thanks so much for all your help.

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
Guru ,
Aug 27, 2012 Aug 27, 2012
LATEST

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…

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