Skip to content

Instantly share code, notes, and snippets.

@pescode
Last active December 5, 2017 12:38
Show Gist options
  • Save pescode/171669dc7d6451da17503a51fa19c26a to your computer and use it in GitHub Desktop.
Save pescode/171669dc7d6451da17503a51fa19c26a to your computer and use it in GitHub Desktop.
Automatically replace text value on SketchAPP, ideal for localization!. Just copy & paste the following script on Plugin->Custom Plugin
// Author: Victor Corvalan
var mkt01 = "#";
var mkt02 = "#";
var font_name = "";
var doc = context.document;
mkt01 = [doc askForUserInput:'Text to replace:' initialValue:''];
mkt02 = [doc askForUserInput:'New text value:' initialValue:''];
font_name = [doc askForUserInput:'Change FONT:' initialValue:''];
var layers;
for (var i = 0; i < doc.pages().count(); i++) {
var page = doc.pages().objectAtIndex(i);
layers = page.children();
// Loop through all children of the page
for (var j = 0; j < layers.count(); j++) {
// get the current layer
var layer = layers.objectAtIndex(j);
// Check if the layer is a text layer
if(layer.class() == "MSTextLayer") {
if(layer.name().toLowerCase() == mkt01.toLowerCase()){
layer.setName(mkt02);
layer.setStringValue(mkt02);
if(font_name != ""){
layer.setFontPostscriptName(font_name);
}
refreshTextLayer(layer);
}
}
}
}
function refreshTextLayer(layer) {
[layer select: true byExpandingSelection: false];
[layer setIsEditingText: true];
[layer setIsEditingText: false];
[layer select: false byExpandingSelection: false];
[layer adjustFrameToFit];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment