I want to writing a script that can update all instance inside a frame which is selected. So I need to check and update instance inside. But I can’t find a right api to do that.
What do you mean by update instances? Which API specifically can you not find?
When I use Figma for design, if a instance has a newer component version in team library, it show symbol like this:
And I was wondering if there a API that can check if a instance “can update”.
1 Like
You know, for my Figma plugin development.
Unfortunately there is no direct API for that but it’s possible to do.
To find if instance is up to date:
- Get its main component ID and import it
- Check if
importedComponent === instance.mainComponent
- If they are different, the instance can be updated
To update the instance:
instance.swapComonent(importedComponent)
1 Like
OK, let me try it later
It’s work!
I post my function code in case someone needs it
async function checkIfInstanceIsUpdated(instance: InstanceNode) {
try {
const asyncMainComponent = await instance.getMainComponentAsync()
if (asyncMainComponent != null) {
const importedComponent = await figma.importComponentByKeyAsync(asyncMainComponent.key)
if (importedComponent != null) {
return (importedComponent.id == asyncMainComponent.id)
}
}
return null
} catch (error) {
console.log("error ! " + error)
return null
}
}
async function checkAndUpdateInstance(instance: InstanceNode) {
try {
const asyncMainComponent = await instance.getMainComponentAsync()
if (asyncMainComponent != null) {
const importedComponent = await figma.importComponentByKeyAsync(asyncMainComponent.key)
if (importedComponent != null) {
if (importedComponent.id != asyncMainComponent.id) {
console.log("update")
instance.swapComponent(importedComponent)
return instance
}
}
}
return null
} catch (error) {
console.log("error ! " + error)
return null
}
}
1 Like