Last active
April 30, 2024 10:02
-
-
Save abynim/dfb0b23f8d0a645b7ef8 to your computer and use it in GitHub Desktop.
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 = "gray_rectangle" | |
// Ask user to select a Sketch file: | |
var openDialog = NSOpenPanel.openPanel() | |
openDialog.setCanChooseFiles(true) | |
openDialog.setAllowedFileTypes(["sketch"]) | |
openDialog.setCanChooseDirectories(false) | |
openDialog.setAllowsMultipleSelection(false) | |
openDialog.setCanCreateDirectories(false) | |
openDialog.setTitle("Select the source Sketch document") | |
if( openDialog.runModal() == NSOKButton ) { | |
var sourceDoc = MSDocument.new() | |
if(sourceDoc.readFromURL_ofType_error(openDialog.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