Skip to content

Instantly share code, notes, and snippets.

@bomberstudios
Last active May 7, 2022 17:50
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bomberstudios/8456746 to your computer and use it in GitHub Desktop.
Save bomberstudios/8456746 to your computer and use it in GitHub Desktop.
Change font family for all text layers in Sketch
// Change font (ctrl a)
var doc = context.document,
selection = context.selection,
font_name = [doc askForUserInput:"Font name:" initialValue:"Arial"];
function check_layer(layer){
log(layer)
var className = layer.className()
log("Checking layer " + layer + " of klass: " + className)
if (className == "MSTextLayer") {
log("Found text layer!")
layer.setFontPostscriptName(font_name);
}
if (className == "MSArtboardGroup" || className == "MSLayerGroup"){
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.objectAtIndex(i);
check_layer(sublayer);
}
}
}
log("################################################################")
// Use selection, if any
if(selection && [selection length]){
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)
}
}
}
@phaistonian
Copy link

This is awesome - thanks!:-)

@marcgibert
Copy link

You have reduced me many hours of work! Thanks :-)

Copy link

ghost commented Apr 23, 2014

Awesome! Doesn't work for Sketch 3 anymore. Will there come a fix?

@janniks
Copy link

janniks commented Dec 12, 2014

Fix:
-[MSArray length] is deprecated. Please call -[MSArray count] instead

@Falkeyn
Copy link

Falkeyn commented Dec 16, 2014

Crashing Sketch on huge files with big amount of layers :(

@morgunder
Copy link

i had a need to correct a file that was using both a custom font and FontAwesome and when i opened it the fonts feel back to their defaults which were LastResort and Helvetica. once i installed a decent google font replacement and font awesome I tweaked the script above to do the replacements i needed. this script works page by page to get around the timeout issues i was having when looking at the whole document.

hope this helps someone else. paste this into the custom plugin window and save as plugin if desired.

function check_layer(layer, font_name, debug) {
if (debug) log("Checking " + [layer class] + " on layer " + layer)
switch ([layer class]) {
case MSTextLayer:
var new_font = font_name
var old_font = [layer fontPostscriptName]

    if (old_font == 'LastResort') {
      new_font = 'FontAwesome';
    } else if (old_font == 'HelveticaNeue') {
      new_font = 'HelveticaNeue';
    } else {
      new_font = font_name;
    }

    log("Setting font from " + old_font + " to " + new_font + " on text layer " + layer)
    layer.setFontPostscriptName(new_font);
    break;
}

}

// Change font (ctrl a)
var selection = context.selection;
var doc = context.document;

var font_name = [doc askForUserInput:"Font name:" initialValue:"Montserrat"];
log("################################################################")

// Use selection, if any
if (selection && [selection count]) {
for (var i = 0; i < [selection count]; i++) {
var layer = selection[i]
var layers = [layer children]
for (var j = 0; j < [layers count]; j++) {
check_layer(layers[j], font_name);
}
}
} else {
// Otherwise, loop through layers on current page
var page = [doc currentPage]
var layers = [page children]

for (var i = 0; i < [layers count]; i++) {
  check_layer(layers[i], font_name)
}

}

@KingWu
Copy link

KingWu commented Aug 16, 2015

get this error. Anyone help
ReferenceError: Can't find variable: doc

@brianlovin
Copy link

Getting the same error as KingWu:

ReferenceError: Can't find variable: doc

@Youhan
Copy link

Youhan commented Dec 16, 2015

This helped me, thanks

here is my very simplified edition to replace a font with another one

function check_layer(layer) 
{

  switch ([layer class]) 
  {
    case MSTextLayer:

      var layer_font = [layer fontPostscriptName]

      if ( layer_font == ".AppleSystemUIFont" ) {

        layer.setFontPostscriptName("Play-Bold");

      }

      break;
  }
}


var doc = context.document;

// loop through layers on current page
var page = [doc currentPage]
var layers = [page children]

for (var i = 0; i < [layers count]; i++) {
  check_layer(layers[i])
}

@monkikkichi
Copy link

Would be nice if later can have some sort of font mapping (i.e. change specific font A to font B & font C to font D at the same time), thanks for the work though!

@idream85
Copy link

idream85 commented May 8, 2016

This is really cool man, many thanks!

@Digitalondon
Copy link

Can you provide some instruction on exactly what I need to paste into Custom Plugin please and how to change to desired font.

Copy link

ghost commented Dec 8, 2021

@Youhan Thank you buddy it helped me out and worked perfectly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment