Last active
December 5, 2017 12:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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