Can you be a bit more specific about what happened to the symbol in the library? I presume the symbol in the library was deleted but the symbol instances in the local doc are still there.
From a symbol instance, you can check to see if the symbol master still exists like so:
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selectedLayer = doc.selectedLayers.layers[0]
let selectedLayerSymbolId = selectedLayer.symbolId
let myLibrary = doc.selectedLayers.layers[0].master.getLibrary()
if (!myLibrary) {
// Symbol master is in current document
return
}
let symbolRefs = myLibrary.getImportableSymbolReferencesForDocument(doc)
let attemptToFindInLibrary = symbolRefs.find(symbol => symbol.id == selectedLayerSymbolId)
let isFoundInLibrary = attemptToFindInLibrary ? true : false
I'll break down each of the lines
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
This just lets you use the Sketch JS API and all its helpful methods and data structures [doc ref]. I'm also getting the current document [doc ref].
let selectedLayer = doc.selectedLayers.layers[0]
let selectedLayerSymbolId = selectedLayer.symbolId
let symbolMasterLib = doc.selectedLayers.layers[0].master.getLibrary()
The crux of what we are trying to do is check to see if the symbol instance's master is found in the library where the current document is reporting where it's from. We will use the symbolId
of the selected layer (I'm assuming its a symbol instance) [doc ref] and the getLibary()
method on a SymbolMaster
to get the source's library [doc ref].
if (!symbolMasterLib) {
// Symbol master is in current document
return
}
This just returns early if the selected layer's symbol is from the current document.
let symbolRefs = myLibrary.getImportableSymbolReferencesForDocument(doc)
We don't peer into the library document directly but rather get a list of ImportableObjects
from the library. It's a little funky but this will ensure the ID's of the objects we want to use will match up properly [doc ref].
let attemptToFindInLibrary = symbolRefs.find(symbol => symbol.id == selectedLayerSymbolId)
Next we check to see if any of the symbol ids of our ImportableObjects
match with the one form our selected layer.
let isFoundInLibrary = attemptToFindInLibrary ? true : false
Then we can use a simple ternary operator to return true or false.
Hope all that helps! Do look through the documentation at each step. Happy to help with any further questions.