Skip to content

Commit

Permalink
Merge pull request #59 from timuric/beta
Browse files Browse the repository at this point in the history
added optional layer renaming
  • Loading branch information
timuric committed Jun 10, 2015
2 parents dce0e3b + c5fb9a0 commit 2640c14
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions Text/Custom string.sketchplugin
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,78 @@ if([selection count] == 0) {
else exec();

function exec(){
var alertMessage = "Example: Price [0-20]$ [old~new]";
createCheckbox = function ( label, value, flag ) {
flag = ( flag == false ) ? NSOffState : NSOnState;
var checkbox = NSButton.alloc().initWithFrame( NSMakeRect( 0, 0, 300, 18 ) );
checkbox.setButtonType( NSSwitchButton );
checkbox.setTitle( label );
checkbox.setTag( value );
checkbox.setState( flag );

var inputString = [doc askForUserInput:alertMessage initialValue:""];
return checkbox;
}
var userInput = COSAlertWindow.new();

var regexBlocks = new RegExp("\\[[^\\]]*");
var regexWords = new RegExp("\\[[^~]+~.+\\]");
var regexNumberRange = new RegExp("\\[\\d+-\\d+\\]");
userInput.setMessageText("Rename Text Layers");
userInput.addTextLabelWithValue("Example: Price [0-20]$ [old~new]");
userInput.addTextFieldWithValue("");
userInput.addAccessoryView( createCheckbox( 'rename layers', 'r', false ) );
userInput.addButtonWithTitle('OK');
userInput.addButtonWithTitle('Cancel');

var blocks = [];
var opening = inputString.indexOf("[");
var closing = inputString.indexOf("]", opening);
if(opening != -1 && closing != -1) blocks.push(inputString.slice(0, opening));
else blocks.push(inputString);
var responseCode = userInput.runModal();

while(opening != -1 && closing != -1){
blocks.push(inputString.slice(opening, closing+1));
opening = inputString.indexOf("[", closing);
blocks.push(inputString.slice(closing+1, opening));
closing = inputString.indexOf("]", opening);
}
if(1000 == responseCode){
var inputString = userInput.viewAtIndex(1).stringValue();
var renameLayers = userInput.viewAtIndex(2).state()
var regexBlocks = new RegExp("\\[[^\\]]*");
var regexWords = new RegExp("\\[[^~]+~.+\\]");
var regexNumberRange = new RegExp("\\[\\d+-\\d+\\]");

var blocks = [];
var opening = inputString.indexOf("[");
var closing = inputString.indexOf("]", opening);
if(opening != -1 && closing != -1) blocks.push(inputString.slice(0, opening));
else blocks.push(inputString);

blocks = blocks.map(function(block){
if(block.match(regexNumberRange)){ //Number range
var min = block.match(/\d+/)[0],
max = block.match(/-[\d]*/)[0].replace("]","").replace("-","");
return getRandomNumber.bind(null, min,max);
while(opening != -1 && closing != -1){
blocks.push(inputString.slice(opening, closing+1));
opening = inputString.indexOf("[", closing);
blocks.push(inputString.slice(closing+1, opening));
closing = inputString.indexOf("]", opening);
}
else if(block.match(regexWords)) { //Words list
block = block.replace("[","");
block = block.replace("]","");
var words = block.split("~");
return getRandomWord.bind(null, words);

blocks = blocks.map(function(block){
if(block.match(regexNumberRange)){ //Number range
var min = block.match(/\d+/)[0],
max = block.match(/-[\d]*/)[0].replace("]","").replace("-","");
return getRandomNumber.bind(null, min,max);
}
else if(block.match(regexWords)) { //Words list
block = block.replace("[","");
block = block.replace("]","");
var words = block.split("~");
return getRandomWord.bind(null, words);
}
else return block;
});

function getRandomNumber (min, max) {
min = new Number(min);
max = new Number(max);
return Math.floor( Math.random() * (max - min) + min );
}
else return block;
});
function getRandomWord (words) {
var index = getRandomNumber(0,words.length);
return words[index]
};

function getRandomNumber (min, max) {
min = new Number(min);
max = new Number(max);
return Math.floor( Math.random() * (max - min) + min );
}
function getRandomWord (words) {
var index = getRandomNumber(0,words.length);
return words[index]
};

for (var i = 0, l = [selection count]; i < l; i++) { //Loop every selected layer
var layer = selection[i];
var generatedString = blocks.map(function(block){ return typeof block === "function" ? block() : block; }).join("");
[layer setStringValue: generatedString];
[layer setName: generatedString];
}
for (var i = 0, l = [selection count]; i < l; i++) { //Loop every selected layer
var layer = selection[i];
var generatedString = blocks.map(function(block){ return typeof block === "function" ? block() : block; }).join("");
[layer setStringValue: generatedString];
if(renameLayers) [layer setName: generatedString];
}

}
}

0 comments on commit 2640c14

Please sign in to comment.