Changing `selection.items` without "not permitted to make changes from the background" error

Assigning to selection.items from a panel raises the following error:

“Plugin Error: not permitted to make changes from the background. Return a Promise to continue execution asynchronously.”

Here’s the code:

function selectFirstArtboard(e) {
  const firstArtboard = scenegraph.root.children.at(0)
  console.log("Trying to select the first artboard: " + firstArtboard.name)
  scenegraph.selection.items = [firstArtboard]
}

Here’s the reduced test-case repo.

How should this code be written so that we’re able to change the selection when the user interacts with the panel?

Thanks!

cc: @stevekwak

1 Like

Since panel is persistent while user interacts with scenegraph, you need to let XD know that your plugin is about to make changes. Wrap your code around with:

const { editDocument } = require("application");
editDocument({editLabel: "blah", function(){
      // scenegraph editing code
})
1 Like

Thanks @stevekwak. This solves the issue.