Skip to main content
Participant
March 28, 2024
Answered

Writing a script to select the next artboard.

  • March 28, 2024
  • 1 reply
  • 520 views

I need to write a script that simply selects the next artboard. 

I have tried a few scripts along the lines of: "var doc = app.activeDocument;
var artboards = doc.artboards;
var activeArtboard = doc.activeArtboard;
var nextArtboardIndex = activeArtboard.index + 1;
if (nextArtboardIndex < artboards.length) {
artboards[nextArtboardIndex].activate();
} else {
artboards[0].activate();
}"

But I get an Error 21: undefined is not an object.
Line: 4 -> var nextArtboarIndex = activeArtboard.index + 1;
 
Any help to correct this or if anyone can write a better script?
Much thanks in advance!
This topic has been closed for replies.
Correct answer m1b

Hi @Deny32888020duo3, here is one approach:

var doc = app.activeDocument;
var currentIndex = doc.artboards.getActiveArtboardIndex();
var nextIndex = (currentIndex + 1) % doc.artboards.length;

doc.artboards.setActiveArtboardIndex(nextIndex);

- Mark 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 28, 2024

Hi @Deny32888020duo3, here is one approach:

var doc = app.activeDocument;
var currentIndex = doc.artboards.getActiveArtboardIndex();
var nextIndex = (currentIndex + 1) % doc.artboards.length;

doc.artboards.setActiveArtboardIndex(nextIndex);

- Mark 

Participant
March 28, 2024

Thank you! This seems to sucessfully select the next artboard starting with 1. Though it does not change the selection the the Artboard panel it does select the next artboard on the screen!

m1b
Community Expert
Community Expert
March 28, 2024

Yes I noticed that and even using app.redraw() didn’t update it. Still, it otherwise does the job.