Last active
April 12, 2016 01:22
-
-
Save getflourish/718d69f445c6c5687f8ab127b44ee132 to your computer and use it in GitHub Desktop.
Find & Replace Fonts 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
// define the scope for the search/replace | |
var artboard = context.document.currentPage().currentArtboard(); | |
var layers = artboard.children(); | |
// this method will list all font names that are used in the scope | |
function listFontNames() { | |
var names = []; | |
for (var i = 0; i < layers.count(); i++) { | |
var layer = layers.objectAtIndex(i); | |
if (layer.className() == "MSTextLayer") { | |
names.push(layer.fontPostscriptName()); | |
} | |
} | |
var unique = names.filter( onlyUnique ) | |
for (var i = 0; i < unique.length; i++) { | |
log(unique[i]); | |
} | |
} | |
// this method will replaces font names that match the search | |
function replaceFontWithFont(scope, search, replace) { | |
var count = 0; | |
for (var i = 0; i < scope.children().count(); i++) { | |
var textLayer = scope.children().objectAtIndex(i); | |
if (textLayer.className() == "MSTextLayer") { | |
if(textLayer.fontPostscriptName() == search) { | |
textLayer.setFontPostscriptName(replace); | |
log("Replaced " + textLayer.stringValue()); | |
count++; | |
} | |
} | |
} | |
log("Done. Replaced " + count + " text layer fonts."); | |
} | |
// returns an array with only the unique elements from the input array | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
listFontNames(); | |
// replaceFontWithFont(artboard, "SourceSansPro-Semibold", "FiraSans-Medium"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment