Skip to content

Instantly share code, notes, and snippets.

View abynim's full-sized avatar

Aby Nimbalkar abynim

View GitHub Profile
@abynim
abynim / sketch-select-filtered-layers.js
Created September 12, 2019 03:14
Select filtered layers in Sketch
// run this code in Sketch's script window (Plugins > Run Script...)
// tested in Sketch 57
context.document.currentPage().changeSelectionBySelectingLayers(nil);
let children = context.document.currentPage().children().objectEnumerator(), layer;
while(layer = children.nextObject()) {
layer.select_byExtendingSelection(context.document.sidebarController().filterViewController().filter().shouldIncludeNode(layer), true);
}
// 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)
var doc = context.document
var selectLayersOfType_inContainer = function(layerType, containerLayer) {
// Filter layers using NSPredicate
var scope = (typeof containerLayer !== 'undefined') ? [containerLayer children] : [[doc currentPage] children],
predicate = NSPredicate.predicateWithFormat("(className == %@)", layerType),
layers = [scope filteredArrayUsingPredicate:predicate];
// Deselect current selection
@abynim
abynim / Sketch Plugin Snippet - Managing Files.js
Last active April 30, 2024 10:02
Sketch Plugin functions for working with files.
var writeTextToFile = function(text, filePath) {
var t = [NSString stringWithFormat:@"%@", text],
f = [NSString stringWithFormat:@"%@", filePath];
return [t writeToFile:f atomically:true encoding:NSUTF8StringEncoding error:nil];
}
var readTextFromFile = function(filePath) {
var fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath]) {
return [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];