Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let sketch = require('sketch')
let settingsAttribute = getSettingsAttributeForKey_Value(kLowerCaseType, kLowerCaseSmallCapsSelector)
applySubstringFontModification()
//getFontAttributesForSelectedRange()
function applySubstringFontModification() {
let document = sketch.getSelectedDocument()
let textLayer = document.selectedLayers.layers[0]
let textView = textLayer.sketchObject.editingDelegate().textView()
let textStorage = textView.textStorage()
let fonts = getFontsFromTextLayer(textLayer)
fonts.forEach(fontForRange => {
let font = fontForRange.font
let range = fontForRange.range
let fontSize = font.pointSize()
let descriptor = font
.fontDescriptor()
.fontDescriptorByAddingAttributes(settingsAttribute)
let newFont = NSFont.fontWithDescriptor_size(descriptor,fontSize)
let attrsDict = NSDictionary.dictionaryWithObject_forKey(newFont,NSFontAttributeName)
textStorage.addAttributes_range(attrsDict,range)
textStorage.fixAttributesInRange(range)
})
textView.didChangeText()
}
function getFontAttributesForSelectedRange() {
let document = sketch.getSelectedDocument()
let textLayer = document.selectedLayers.layers[0]
let fonts = getFontsFromTextLayer(textLayer)
fonts.forEach((fontForRange, index) => {
let font = fontForRange.font
let fontFeatureSettings = font.fontDescriptor().fontAttributes()[NSFontFeatureSettingsAttribute]
console.log("Font" + (1 + index))
console.log(fontFeatureSettings)
})
}
// ----- Helper Functions
function getSettingsAttributeForKey_Value(key, value) {
let settingsAttribute = {
[NSFontFeatureSettingsAttribute]: [{
[NSFontFeatureTypeIdentifierKey]: key,
[NSFontFeatureSelectorIdentifierKey]: value
}]
}
return settingsAttribute
}
function getFontsFromTextLayer(textLayer) {
let msTextLayer = textLayer.sketchObject
let textView = msTextLayer.editingDelegate().textView()
let textStorage = textView.textStorage()
let selectedRange = textView.selectedRange()
let effectiveRange = MOPointer.alloc().init()
let fonts = []
if (selectedRange.length == 0) {
let font = textStorage.attribute_atIndex_longestEffectiveRange_inRange(
NSFontAttributeName,
selectedRange.location,
effectiveRange,
selectedRange
)
fonts.push({"font": font, "range": effectiveRange.value()})
}
while (selectedRange.length > 0) {
let font = textStorage.attribute_atIndex_longestEffectiveRange_inRange(
NSFontAttributeName,
selectedRange.location,
effectiveRange,
selectedRange
)
selectedRange = NSMakeRange(
NSMaxRange(effectiveRange.value()),
NSMaxRange(selectedRange) - NSMaxRange(effectiveRange.value())
)
fonts.push({"font": font, "range": effectiveRange.value()})
}
return fonts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment