Last active
May 7, 2022 17:50
-
-
Save bomberstudios/8456746 to your computer and use it in GitHub Desktop.
Change font family for all text layers in Sketch
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
// 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) | |
} | |
} | |
} |
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!
This is really cool man, many thanks!
Can you provide some instruction on exactly what I need to paste into Custom Plugin please and how to change to desired font.
@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
This helped me, thanks
here is my very simplified edition to replace a font with another one