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

How to get correct selection object size

Community Beginner ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

I would like to make a dimension for a selection object. But the selection maybe have clipping with picture, or it is a groupitem,or just a simple item. Etc. I just would to get the selection width and length like it show on transform panel.can someone help me.

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

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

Enthusiast , Sep 12, 2017 Sep 12, 2017

function getSelectionVisibleBounds() { 

    var doc = app.activeDocument;

    var ab = doc.artboards[0];

    var rect = ab.artboardRect; 

    doc.fitArtboardToSelectedArt(0); 

    var vb = ab.artboardRect; 

    ab.artboardRect = rect; 

    return vb 

};

alert(getSelectionVisibleBounds())

Since visibleBounds is OK for you, this is the simplest way. The artboard size will set back to origin, so don't worry about that.

Votes

Translate

Translate
Adobe
Valorous Hero ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

I was just messing with this yet another time, and made a clipboard-observing function.

function getSelectionBounds = function(art) {

  var doc = app.activeDocument;

  var storedActiveArtboardIndex = doc.artboards.getActiveArtboardIndex();

  var activeBoard = doc.artboards[storedActiveArtboardIndex];

  var activeBoardRect = activeBoard.artboardRect;

  var tempSelection = [];

  if (doc.selection != null) {

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

      tempSelection.push(doc.selection);

    }

  }

  doc.selection = null;

  art.selected = true;

  doc.fitArtboardToSelectedArt(storedActiveArtboardIndex);

  var resultRect = activeBoard.artboardRect;

  activeBoard.artboardRect = activeBoardRect;

  doc.selection = null;

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

    tempSelection.selected = true;

  }

  // var newBoundsPoints = [

  //  [resultRect[0], resultRect[1]],

  //  [resultRect[2], resultRect[1]],

  //  [resultRect[0], resultRect[3]],

  //  [resultRect[2], resultRect[3]]

  // ];

  return resultRect;

};

This gets the bounds by temporarily fitting the artboard to the selection dimensions.

Votes

Translate

Translate

Report

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
Enthusiast ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

Well, made a slightly modify to your code.

function getSelectionBounds(art) {

    var doc = app.activeDocument;

    var storedActiveArtboardIndex = doc.artboards.getActiveArtboardIndex();

    var activeBoard = doc.artboards[storedActiveArtboardIndex];

    var activeBoardRect = activeBoard.artboardRect;

    var tempSelection = doc.selection;

    doc.selection = [art];

    doc.fitArtboardToSelectedArt(storedActiveArtboardIndex);

    var resultRect = activeBoard.artboardRect;

    activeBoard.artboardRect = activeBoardRect;

    doc.selection = tempSelection;

    // var newBoundsPoints = [ 

    //  [resultRect[0], resultRect[1]], 

    //  [resultRect[2], resultRect[1]], 

    //  [resultRect[0], resultRect[3]], 

    //  [resultRect[2], resultRect[3]] 

    // ]; 

    return resultRect

};

The problem is, this will always get the visiblebounds, not geometricBounds.

Votes

Translate

Translate

Report

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
Valorous Hero ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

If you are talking about visibleBounds and geometricBounds as used to describe bounds with included or excluded stroke, and if the selection is used to get the rectangle, then is the bounds type not going to be simply determined by the preference setting for "use preview bounds" ?

Votes

Translate

Translate

Report

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 ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

I need visiblebounds.  can we just check the typename, if the selection have clipping, we can just get the clipping size, but if there were more than 2 objects have clipping, I don't know how to get the correct size.and I don't like to change the artboard size.

Votes

Translate

Translate

Report

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
Valorous Hero ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

Oh no! That's too bad. Otherwise, you can check top-level group if it's .clipped property is true. If it's true, you can search the pathItems inside this group to find the one with property .clipping set to true. Now you get the bounds of the clipping path.

The problem is when you have nested groups which may be clipped too. To get all of them without fitting artboard will come down to recursively calculating the bounds of all the art, and if it's many items, changing artboard size is faster.

Votes

Translate

Translate

Report

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
Enthusiast ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

function getSelectionVisibleBounds() { 

    var doc = app.activeDocument;

    var ab = doc.artboards[0];

    var rect = ab.artboardRect; 

    doc.fitArtboardToSelectedArt(0); 

    var vb = ab.artboardRect; 

    ab.artboardRect = rect; 

    return vb 

};

alert(getSelectionVisibleBounds())

Since visibleBounds is OK for you, this is the simplest way. The artboard size will set back to origin, so don't worry about that.

Votes

Translate

Translate

Report

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 ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

LATEST

thank you so much for your help.  It is ok now, I am not sure why the last code make my illustrator down. I read the code and know that the artboard size will back to original.  thanks again.

Votes

Translate

Translate

Report

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
Enthusiast ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

No matter check on "use preview bounds" or not, geometricBounds is always different from visibleBounds. And some times the center of VB is not the center of GB, when scale or rotate items from it's center this will cause problem.cap.PNG

Votes

Translate

Translate

Report

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 ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

thank you for you reply,

but the code is not work, and make my illustrator down. my illustrator version is CC 2017.0.2.

I just need visiblebounds, I would like to make a dimension on the selection. the selection width and length.

Votes

Translate

Translate

Report

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