Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Insert a Sketch layer from an absolute path'd file
// Name of layer to copy
var sourceLayerName = "Card",
url = NSURL.URLWithString("file:///Users/ryan/Downloads/Test.sketch")
if( true ) {
var sourceDoc = MSDocument.new()
if(sourceDoc.readFromURL_ofType_error(url, "com.bohemiancoding.sketch.drawing", nil)) {
var allChildren = sourceDoc.pages().valueForKeyPath("@distinctUnionOfArrays.children")
// Modify the predicate to change search parameters. Here it's by layer name
var predicate = NSPredicate.predicateWithFormat("name == %@", sourceLayerName);
var layerToCopy = allChildren.filteredArrayUsingPredicate(predicate).firstObject()
if (layerToCopy) {
var duplicateLayerToCopy = layerToCopy.duplicate()
duplicateLayerToCopy.removeFromParent()
var currentDoc = context.document
currentDoc.currentPage().currentArtboard().addLayers([duplicateLayerToCopy])
if(duplicateLayerToCopy.isSymbol()) {
currentDoc.documentData().layerSymbols().addSymbolWithName_firstInstance(duplicateLayerToCopy.name(), duplicateLayerToCopy)
}
}
}
sourceDoc.close()
sourceDoc = nil
}
// Name of layer to copy
var sourceLayerName = "Card",
path = @"~/Downloads/Test.sketch",
path = path.stringByExpandingTildeInPath(),
url = NSURL.URLWithString(path)
var sourceDoc = MSDocument.new()
if(sourceDoc.readFromURL_ofType_error(url, "com.bohemiancoding.sketch.drawing", nil)) {
var allChildren = sourceDoc.pages().valueForKeyPath("@distinctUnionOfArrays.children")
// Modify the predicate to change search parameters. Here it's by layer name
var predicate = NSPredicate.predicateWithFormat("name == %@", sourceLayerName);
var layerToCopy = allChildren.filteredArrayUsingPredicate(predicate).firstObject()
if (layerToCopy) {
var duplicateLayerToCopy = layerToCopy.duplicate()
duplicateLayerToCopy.removeFromParent()
var currentDoc = context.document
currentDoc.currentPage().currentArtboard().addLayers([duplicateLayerToCopy])
if(duplicateLayerToCopy.isSymbol()) {
currentDoc.documentData().layerSymbols().addSymbolWithName_firstInstance(duplicateLayerToCopy.name(), duplicateLayerToCopy)
}
}
}
sourceDoc.close()
sourceDoc = nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.