Skip to content

Instantly share code, notes, and snippets.

@boton
Forked from bomberstudios/Change Font.sketchplugin
Last active January 3, 2016 11:59
Show Gist options
  • Select an option

  • Save boton/8459667 to your computer and use it in GitHub Desktop.

Select an option

Save boton/8459667 to your computer and use it in GitHub Desktop.
Change font family for all text layers in Sketch
// Change font (ctrl a)
var font_name,
select_one_textlayer = false;
function check_layer(layer){
log("Checking layer " + layer + " of klass: " + [layer class])
switch ([layer class]) {
case MSTextLayer:
log("Found text layer!")
layer.setFontPostscriptName(font_name);
break;
case MSPage:
case MSLayerGroup:
case MSArtboardGroup:
var sublayers = [layer layers];
log("This is a group/artboard/page with " + [sublayers length] + " sublayers")
for(var i=0; i < [sublayers length]; i++) {
var sublayer = sublayers[i];
check_layer(sublayer);
}
break;
}
}
log("################################################################")
//If only select one MSTextLayer use its font name
//if (selection && [selection length] === 1 && [selection[0] fontPostscriptName]) { //[selection[0] fontPostscriptName] <- ERROR :S
if (selection && [selection length] === 1) {
var layer = selection[0];
if ([layer class] === MSTextLayer) {
font_name = [layer fontPostscriptName];
select_one_textlayer = true;
}
}
if (!select_one_textlayer) {
font_name = [doc askForUserInput:"Font name:" initialValue:"Arial"];
}
// Use selection, if any
if(selection && [selection length] && !select_one_textlayer){
for (var i = 0; i < [selection length]; i++) {
check_layer(selection[i]);
}
} else {
// Otherwise, loop trough pages, artboards & layers
var pages = [doc pages];
for (var i = 0; i < [pages length]; i++) {
var current_page = pages[i];
if ([[current_page artboards] length]) {
log("Traversing artboards")
for (var i = 0; i < [[current_page artboards] length]; i++) {
var artboard = [current_page artboards][i]
check_layer(artboard)
}
} else {
log("Page has no artboards")
check_layer(current_page)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment