Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. abynim revised this gist Aug 7, 2015. 1 changed file with 3 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -26,6 +26,9 @@ if( openDialog.runModal() == NSOKButton ) {
    duplicateLayerToCopy.removeFromParent()
    var currentDoc = context.document
    currentDoc.currentPage().currentArtboard().addLayers([duplicateLayerToCopy])
    if(duplicateLayerToCopy.isSymbol()) {
    currentDoc.documentData().layerSymbols().addSymbolWithName_firstInstance(duplicateLayerToCopy.name(), duplicateLayerToCopy)
    }
    }
    }
    sourceDoc.close()
  2. abynim revised this gist Aug 5, 2015. 1 changed file with 1 addition and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ 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)
  3. abynim renamed this gist Aug 5, 2015. 1 changed file with 0 additions and 0 deletions.
  4. abynim created this gist Aug 5, 2015.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // Name of layer to copy
    var sourceLayerName = "gray_rectangle"

    // Ask user to select a Sketch file:
    var openDialog = NSOpenPanel.openPanel()
    openDialog.setCanChooseFiles(true)
    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])
    }
    }
    sourceDoc.close()
    sourceDoc = nil
    }