Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Fixed split script in Sketch 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwayne committed Jul 23, 2016
1 parent 9899722 commit 36fe369
Showing 1 changed file with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
var onRun = function(context) {
// Split text lines to layers

var selectedLayers = context.selection,
selectedCount = selectedLayers.count();
var selectedLayers = context.selection,
selectedCount = selectedLayers.count();

if (selectedCount == 0) {
log('No layers are selected.');
} else {
// Loop through selected layers
for (var i=0; i<selectedCount; i++) {
var layer = selectedLayers[i],
frame = [layer frame],
layerString = [layer stringValue],
lines = layerString.split("\n"), // split text at new line character
parent = [layer parentGroup]; // layer parent for removing original layer
if (selectedCount == 0) {
log('No layers are selected.');
} else {
// Loop through selected layers
for (var i=0; i<selectedCount; i++) {
var layer = selectedLayers[i],
frame = [layer frame],
layerString = [layer stringValue],
lines = layerString.split("\n"), // split text at new line character
parent = [layer parentGroup]; // layer parent for removing original layer

// Create new layer for each line
for (j=0; j<lines.length; j++) {
var line = lines[j];
// Create new layer for each line
for (j=0; j<lines.length; j++) {
var line = lines[j];

if (line.length == 0) {
continue; // skip if it's a blank line
} else {
var newLayer = [layer duplicate],
newFrame = [newLayer frame],
lineHeight = [newLayer lineSpacing];
if (line.length == 0) {
continue; // skip if it's a blank line
} else {
var newLayer = [layer duplicate],
newFrame = [newLayer frame],
lineHeight = [newLayer lineSpacing];

// Set text, layer name, and offset y for new layer and select it
[newLayer setStringValue: line];
[newLayer setName: line];
[newFrame addY: (lineHeight * j)];
[newLayer select:true byExpandingSelection:true];
// Set text, layer name, and offset y for new layer and select it
[newLayer setStringValue: line];
[newLayer setName: line];
[newFrame setY: (lineHeight * j + frame.y())];
[newLayer select:true byExpandingSelection:true];

// Duplicated layers still retain old height info; bug?
[newFrame setHeight: [newLayer lineSpacing]];
// Duplicated layers still retain old height info; bug?
[newFrame setHeight: [newLayer lineSpacing]];
};
};
};

// Remove original layer
[parent removeLayer: layer];
// Remove original layer
[parent removeLayer: layer];
};
};
};
};
};

1 comment on commit 36fe369

@joshwayne
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #4 and #5

Please sign in to comment.