Last active
August 29, 2015 14:27
-
-
Save ryngonzalez/eab67d0a6c38329e1c15 to your computer and use it in GitHub Desktop.
Insert a Sketch layer from an absolute path'd file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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