Figma Support Forum

Export selected images and save

Hello,

How to export/save files to disk? How to make loop: write files to disk from figma.currentPage.selection?

code.ts:

figma.showUI(__html__);

figma.on('selectionchange', () => {
  if (figma.currentPage.selection.length >= 1) {
    console.log(figma.currentPage.selection.length);
    console.log(figma.currentPage.selection);
  } else {
    console.log('Select at least 1 layers')
  }
})

figma.ui.onmessage = msg => {
  if (msg.type === 'export') {
    console.log(figma.currentPage.selection.length);
    console.log(figma.currentPage.selection);

    const selected = figma.currentPage.selection
    selected.forEach((node, index) => {
      console.log(node);

      // HERE !!!!! HOW TO export/save from node ?

    });


  }

  figma.closePlugin();
};

ui.hmtl:

<h2>Export Images</h2>
<button id="export">Export</button>
<button id="cancel">Cancel</button>

<script>
  document.getElementById('export').onclick = () => {
    parent.postMessage({ pluginMessage: { type: 'export' } }, '*')
  }

  document.getElementById('cancel').onclick = () => {
    parent.postMessage({ pluginMessage: { type: 'cancel' } }, '*')
  }

  window.onmessage = async event => {
    const message = event.data.pluginMessage
    console.log(message)
  }
</script>

I seen:

1. https://github.com/roana0229/figma-export-for-native (https://github.com/roana0229/figma-export-for-native/blob/master/src/code.ts)
2. https://github.com/brianlovin/figma-export-zip

^^^ but not working :frowning:

You are looking for exportAsync(): https://www.figma.com/plugin-docs/api/properties/nodes-exportasync/