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

How do you determine the initial reference when controlling a chart using visibleBounds?

Guide ,
Jul 31, 2025 Jul 31, 2025

I used:

var sel = app.documents[0].selection;
sel[j].visibleBounds = [sb[0], lcb, sb[2], rcb];

to control two charts.


I noticed that sometimes 00001 is placed at the front, and its height is used as the standard for changes.

 

But sometimes 00002 is placed at the front, and its height is used as the reference.

These are not based on which one was originally at the front, but seem to be based on the order of the filenames 00001 and 00002.

 

Is this random? It doesn't seem so.

How to ensure that the initial top-ranked item remains the standard reference and continues to rank first.

 That is, make it sb[0].

NO1.png

 

TOPICS
Bug , How to , Scripting
318
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

Community Expert , Jul 31, 2025 Jul 31, 2025

Hi @dublove the selection is an Array, which you can sort.

/**
* Example of sorting frames by position on the page.
*
* @author m1b
* @version 2025-07-31
* @discussion https://community.adobe.com/t5/indesign-discussions/how-do-you-determine-the-initial-reference-when-controlling-a-chart-using-visiblebounds/m-p/15437423
*/
(function () {

    var doc = app.activeDocument;

    // now we have an Array of selected page items
    var frames = doc.selection;

    // we have an array
    alert('frames
...
Translate
Community Expert ,
Jul 31, 2025 Jul 31, 2025

Again you are sharing incomplete code with undefined variables (ie. sel[j], sb[0], sb[2]). We can't help you if you don't show us exactly what you are trying to achieve. 

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 ,
Jul 31, 2025 Jul 31, 2025

Sorry,  there's a bit too much code here.
I'll look into it some more. The question is, if multiple options are selected, which one is the first?

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 ,
Jul 31, 2025 Jul 31, 2025

Hi @dublove I think you need to sort the array of graphics (or frames) based on their position on the page. Here is an example showing the Array.sort method at work:

/**
* Example of sorting graphics by position on the page.
*
* @author m1b
* @version 2025-07-31
* @discussion https://community.adobe.com/t5/indesign-discussions/how-do-you-determine-the-initial-reference-when-controlling-a-chart-using-visiblebounds/m-p/15437423
*/
(function () {

    var doc = app.activeDocument;
    var graphics = [doc.allGraphics[0], doc.allGraphics[1]];

    // sort the graphics by their frame's position on the page
    graphics.sort(function (a, b) {

        // we want the graphic's parent frame's bounds
        var aBounds = a.parent.visibleBounds;
        var bBounds = b.parent.visibleBounds;

        // compare the left edges
        if (aBounds[1] < bBounds[1])
            // a is left of b
            return -1;

        else if (aBounds[1] > bBounds[1])
            // a is right of b
            return 1;

        else
            // at the same horizontal position, so compare top edges
            return aBounds[0] - bBounds[0];

    });

    // so now we know that graphics[0] is to the left of graphics[1]
    
    // just for demonstration!
    graphics[0].remove();

})();

 

After sorting, the graphics[0] will have the left-most frame.

- Mark

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 ,
Jul 31, 2025 Jul 31, 2025

Hi m1b.

Thank you very much.

 

My operation object is
var sel = app.documents[0].selection;
How can I send sel[j] to the function () function for sorting?

Like this?

var graphics = [sel[j], sel[j+1]];
.....
Push sel[j] again?
var newSel;
newSel.push(sel[j])
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 ,
Jul 31, 2025 Jul 31, 2025

Hi @dublove the selection is an Array, which you can sort.

/**
* Example of sorting frames by position on the page.
*
* @author m1b
* @version 2025-07-31
* @discussion https://community.adobe.com/t5/indesign-discussions/how-do-you-determine-the-initial-reference-when-controlling-a-chart-using-visiblebounds/m-p/15437423
*/
(function () {

    var doc = app.activeDocument;

    // now we have an Array of selected page items
    var frames = doc.selection;

    // we have an array
    alert('frames constructor name is "' + frames.constructor.name + '".');

    if (0 === frames.length)
        return alert('Please select some frames and try again.');

    // sort the frame by left-right/top-bottom on the page
    frames.sort(function (a, b) {

        var aBounds = a.visibleBounds;
        var bBounds = b.visibleBounds;

        // compare the left edges
        if (aBounds[1] < bBounds[1])
            // a is left of b
            return -1;

        else if (aBounds[1] > bBounds[1])
            // a is right of b
            return 1;

        else
            // at the same horizontal position, so compare top edges
            return aBounds[0] - bBounds[0];

    });

    // so now we know that frames[0] is above frames[1]

    // just for demonstration!
    frames[0].remove();

})();
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 ,
Jul 31, 2025 Jul 31, 2025

Thank you very much.

I understand a little bit now. It's a cycle that exchanges positions within the array.

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 ,
Aug 01, 2025 Aug 01, 2025
LATEST

Hi m1b.

It's strange.
When aBounds[1] == bBounds[1],

the following code judgment is confusing. 

 

Is there an alignment error?
If aBounds[1] == bBounds[1],

I want the top one to be number 1.
But I can't seem to determine that.

 

It is not necessarily the case that the object above is deleted.

else
// At the same horizontal position, so compare the top edges.
return aBounds[0] - bBounds[0];

wc.png

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