As a basic example, I’m trying to create a Cover page and set that page as the Figma.currentPage. However, in the console.log of the currentPage, it still show as “Page 1” instead of “Cover”. The actual Figma app does switch to the “Cover” page, but I don’t understand why console.log is showing “Page 1” instead of “Cover” as the currentPage. This is causing frames I want to create on the “Cover” page to be created on “Page 1” instead.
What am I missing to ensure that Figma has successfully set currentPage to “Cover” before I create frames? Or is there a way to create frames directly on the “Cover” page without having to set it as the currentPage?
I’m struggling with this as well. figma.currentPage doesn’t seem to be updated instantly.
figma.on("currentpagechange", () => {
console.log("currentpagechange:", figma.currentPage.name); // never triggered
});
const page = figma.createPage();
page.name = "New page";
console.log("BEFORE:", figma.currentPage.name); // name of the previously open page
figma.currentPage = page;
console.log("AFTER:", figma.currentPage.name); // name of the previously open page
figma.closePlugin(figma.currentPage.name); // name of the previously open page
There’s also some weird behavior: after closing the plugin, the newly created page becomes the current page (as expected within plugin runtime). Additionally, without calling figma.closePlugin(), the currentpagechange event is triggered.
Note: There is no documentAccess?: 'dynamic-page' field in the manifest file.
However, as you might guess, the plugin’s logic is much more complex, and this is just an initial step. I’ll probably need to consider something asynchronous.
I assumed figma.currentPage isn’t asynchronous, which is why I thought setCurrentPageAsync() made sense, but I’m not entirely sure that assumption is correct.
I’ll give dynamic-page a try, though I’ve run into some issues with it for now.
OK, I tried using documentAccess?: 'dynamic-page' field, but I’m still seeing the same results.
figma.on("currentpagechange", () => {
console.log("currentpagechange:", figma.currentPage.name); // never triggered
});
async function setPage() {
const page = figma.createPage();
page.name = "New page";
console.log("BEFORE:", figma.currentPage.name); // name of the previously open page
await figma.setCurrentPageAsync(page);
console.log("AFTER:", figma.currentPage.name); // name of the previously open page
figma.closePlugin(figma.currentPage.name); // name of the previously open page
}
setPage();
I was under the impression that await ensures the Promise resolves before moving on, but it seems like the Promise doesn’t reflect when the page is actually set as the current page. Could it be that I need to call loadAllPagesAsync()? Any suggestions?