If you know the swatch ID then you could do something like this. You’ll need to iterate through all the swatches on the document
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selection = doc.selectedLayers.layers[0]
let docData = doc.sketchObject.documentData()
let swatches = docData.allSwatches()
try {
let swatch = findSwatchWithID('140EB118-0398-4124-9948-8D8A15383B58')
console.log(swatch.name())
} catch {
// no swatches found
console.log("no swatches matched")
}
function findSwatchWithID(id) {
for (let i=0; i<swatches.length;i++){
if (id == swatches[i].objectID()) {
return swatches[i]
}
if (swatches.length - 1 === i) {
return nil
}
}
}
Note that doc.swatches
doesn’t return all swatches if there are foreign swatches used in the document. Jumping down to the private API gives you access to all swatches used
doc.sketchObject.documentData().allSwatches()
// Just foreign swatches
doc.sketchObject.documentData().foreignSwatches()
// Just local
doc.sketchObject.documentData().sharedSwatches()