From a7aff866a03307508a342f178597b3d4e33d3d9a Mon Sep 17 00:00:00 2001 From: Margus Holland Date: Sat, 14 Nov 2015 13:08:41 +0200 Subject: [PATCH] Smarter selection, select groups, artboards etc --- .../Contents/Sketch/manifest.json | 2 +- .../Contents/Sketch/script.cocoascript | 25 +++++++++++++++++-- README.md | 8 ++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Comma Fucker.sketchplugin/Contents/Sketch/manifest.json b/Comma Fucker.sketchplugin/Contents/Sketch/manifest.json index 5748b07..263795a 100644 --- a/Comma Fucker.sketchplugin/Contents/Sketch/manifest.json +++ b/Comma Fucker.sketchplugin/Contents/Sketch/manifest.json @@ -98,7 +98,7 @@ "title": "Comma, Fucker" }, "identifier": "com.margusholland.commafucker", - "version": "1.0", + "version": "1.1", "description": "Set of commands to clean up and modify text layers.", "authorEmail": "margusholland@me.com", "name": "Comma, Fucker", diff --git a/Comma Fucker.sketchplugin/Contents/Sketch/script.cocoascript b/Comma Fucker.sketchplugin/Contents/Sketch/script.cocoascript index dcddf15..b89eaf7 100644 --- a/Comma Fucker.sketchplugin/Contents/Sketch/script.cocoascript +++ b/Comma Fucker.sketchplugin/Contents/Sketch/script.cocoascript @@ -3,13 +3,34 @@ var layers = function(context) { var selectedLayers = context.selection var selectedCount = doc.selectedLayersOfClass(MSTextLayer).count() if (selectedCount == 0) { - // return [[doc currentPage] layers] - [doc showMessage:'Please select at least one text layer'] + var selectedGroups = flatten(selectedLayers) + if (selectedGroups.count() > 0) { + return selectedGroups + } else { + [doc showMessage:'Nothing selected'] + return [NSArray new] + } } else { return doc.selectedLayersOfClass(MSTextLayer) } } +var flatten = function(array) { + var flatArray = [NSMutableArray new] + + for (var i = 0; i < array.count(); i++) { + var item = [array objectAtIndex:i] + if (([item class] == MSLayerGroup || [item class] == MSArtboardGroup) && item.layers().count() > 0) { + [flatArray addObjectsFromArray:flatten(item.layers())] + } else { + if ([item class] == MSTextLayer) { + [flatArray addObject:item] + } + } + } + return flatArray +} + var moveLayer = function(layer) { [layer adjustFrameToFit] layer.setIsVisible(false) diff --git a/README.md b/README.md index 25d4884..aa209dd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Comma, Fucker Sketch plugin for text modifications. -Select one or multiple text layers to change the text. Following commands are available: +Select text layers, layer groups or artboards to change the text. Following commands are available: * Change text to Lowercase, Uppercase, Title Case and Sentence Case * Change three consecutive periods to ellipsis @@ -11,8 +11,4 @@ Select one or multiple text layers to change the text. Following commands are av * Replace all single line breaks with double line breaks (paragraph style) * Show character and word counts -Comma, Fucker - -# Known issues - -* Doesn’t change text when layers are grouped, i.e. one must directly select the text layer(s). +Comma, Fucker \ No newline at end of file