Skip to main content
Question

How access other file by Figma API


Nic_Cheung

I created a small plugin for creating Tables. I hope to create and access a small data Figma file in the community space to save the Table’s data styles that the table needs to use, and then how can I read the file?

In fact, this is a problem with reading files across files in Figma API. I wish any expert can enlight us, or give some clues and hints.

for example, can access it and clone it, but how to clone it?😂 Which function I can use?

20 replies

Nic_Cheung
  • Author
  • 11 replies
  • May 5, 2022


Here is the Screenshot, it is ugly because this plugin only can work but bounding one static Figma file, I hope it can work in any Figma file without any limitation


Gleb
  • Power Member
  • 4706 replies
  • May 6, 2022

Plugins can only work within the limits of one file. The most you can do is import components from other files (libraries), and even then you have to know the component keys first, which you can’t get without opening the plugin in the library file first.


Nic_Cheung
  • Author
  • 11 replies
  • May 6, 2022

Hi, Master Gleb, Thank you for coming,

Could I know which function should I use to get the component keys? I have already hung up a Figma file with full edit permissions in a URL, and already put all resource frames into it.

Could you let me know where is the introduction of this function?

Thanks 😊🙏


Gleb
  • Power Member
  • 4706 replies
  • May 7, 2022

Edit permissions and URLs have nothing to do with plugins. Open the file, open the plugin in that file, scan the file for the component you need (using something like root.findAll for example) and then get the key via component.key property.


Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Hi, Dear Master Bleb, follow the advice your enlighted me, I create them correctly this Sunday! Like the GIF below here:
getComKey

Thank you for your comments!

Nic


Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Next step: I prefer to import the component by the key, but can you give me a tip: on how to import the component by JS,
I am failed when I import it,



tank666
  • 4857 replies
  • May 29, 2022

Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Hi, Tank, Thank you for reminding me of the “figma.” mistake I made, I fixed it, and it worked, but … there is some 404
So could you tell me some clue about the console error report?


tank666
  • 4857 replies
  • May 29, 2022

For ComponentSetNode you need to use figma.importComponentSetByKeyAsync(key).


Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Tank, Thank you reply me so fast​:blush:👍


I switch to:
importComponentSetByKeyAsync(“key’s string”);
But still display 404, something I did wrong?
importError01


tank666
  • 4857 replies
  • May 29, 2022

Is the component with this key published in the library file?


Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

I use a button event to get the component.key similar to what Gleb told me.

Here is how I copy the key below​:blush:😭:
importErrorCopyKey


tank666
  • 4857 replies
  • May 29, 2022

I asked about something else.

The component whose key you got is in the library file? Is this file published as a library?

The above API methods only allow you to import components from libraries.

Figma Help Center

Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Oh, yes yes😊, the resource library file has been published,

and the test file already imports library in as green framed:

I will check my publish status, maybe somewhere is wrong, Thank you! Tank!😊


Nic_Cheung
  • Author
  • 11 replies
  • May 29, 2022

Very helpful, I chose another component, it is working, and I get a [promise]

so how do I copy the component into the current page?


tank666
  • 4857 replies
  • May 29, 2022

Wait for the Promise to succeed and then create an instance of the imported component.

For example:

let importComponent = await figma.importComponentByKeyAsync(key);
importComponent.createInstance()

Nic_Cheung
  • Author
  • 11 replies
  • June 19, 2022

Hi, Tank,
I continue with your suggestion as below here: but it is doesn’t work, help please😂


Gleb
  • Power Member
  • 4706 replies
  • June 19, 2022

Note that you are receiving a syntax error, not the plugin API error, which means your code is written incorrectly. Most likely you are trying to use await outside of an async function (or outside of any function for that matter). Make sure you are using async/await correctly.


Nic_Cheung
  • Author
  • 11 replies
  • June 19, 2022

Hi, Gleb, It is the first time using await, the situation is:
I can get promise as the image below here:

but I copy code(Tank66’s), and it logged error,

Could you help me with more complete/Entire code, sorry I’m a noob designer and I’m really stupid at coding😂
I feel like I’m at the gate, but I just “can’t get in”: I just can’t import this component from the library.


tank666
  • 4857 replies
  • June 19, 2022

As you were told above, you are using await outside of an async function. Just create an async function and call it:

async function importNode() {
    let importComponent = await figma.importComponentByKeyAsync(key);
    importComponent.createInstance()
}

importNode()
developer.mozilla.org

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings