We created one plugin where we export selected ArtBoard but each time we need to check the user has selected or not (selection object). We want to select ArtBorad programmatically if the user has not selected it.
Is there any API to select ArtBoard programmatically in XD plugin?.
Yes, you can select artboards. To do so, set the selection.items property to one node or an array of nodes to be selected:
To get your document’s artboards, examine the children of the root node:
Here is a function that selects all of the artboards:
const { editDocument } = require("application");
const { selection, Artboard } = require("scenegraph");
const scenegraph = require("scenegraph");
function selectAllArtboards() {
editDocument({ editLabel: "Selecting artboards" }, function () {
var artboardsToSelect = new Array();
for (let i = 0; i < scenegraph.root.children.length; i++) {
let childNode = scenegraph.root.children.at(i);
if (childNode.constructor.name === "Artboard") { // Only get artboards
console.log("Artboard " + i + " " + childNode.name + " id: " + childNode.guid);
artboardsToSelect.push(childNode);
}
}
if (artboardsToSelect.length > 0) {
console.log("Selecting " + artboardsToSelect.length + " artboard(s)");
selection.items = artboardsToSelect; // Select the artboards
}
});
}
Or to only select the first artboard:
function selectFirstArtboard() {
editDocument({ editLabel: "Selecting artboard" }, function () {
let artboardFound = false;
let i = 0;
while (!artboardFound && (i < scenegraph.root.children.length)) {
let childNode = scenegraph.root.children.at(i);
if (childNode.constructor.name === "Artboard") {
selection.items = childNode;
artboardFound = true;
}
i++;
}
});
}
Keep in mind that changing the selection must be done within application.editDocument() and application.editDocument() can be called only once in response to a user gesture, such as pressing a button.
Hi David,
Thanks For response. Currently if I have 5 Artboards and I selected only 2 ArtBoards using Shift key.
How i can create rendition of that two artboards?.
scenegraph.root.children are not modifiable. and selection.items is not accepted for node in renderOptions.
One more question is How to create single PDF but Multipage pdf when we selected multiple ArtBoards?
The application.createRenditions() function does require referencing a single node. If you want to export all artboards, then for the “renditions” parameter set “node” to “scenegraph.root”. However I understand that you want to export only the selected artboards to one PDF file. Unfortunately I do not see a way of doing that. The best I can advise is to export each selected artboard to its own PDF file by iterating through “selection.items”. Unfortunately the application.createRenditions() API does not support exporting multiple renditions to different pages in a single PDF file at this time.
@DavidXD I tried the same function you shared to select Artboards programmatically but it gives me the following error when the current selection is inside a group.
Error: Cannot select node outside the current edit context’s scope: