Skip to content

Instantly share code, notes, and snippets.

💎
Cookin up some Sketch plugins

Kevin Gutowski KevinGutowski

💎
Cookin up some Sketch plugins
View GitHub Profile
View prefs.js
let defaults = NSUserDefaults.standardUserDefaults()
console.log(defaults.dictionaryRepresentation())
View checkForSFSymbols.js
function checkForSFSymbols() {
let systemVersionPlist = "/System/Library/CoreServices/SystemVersion.plist"
let systemVersionDictionary = NSDictionary.dictionaryWithContentsOfFile(systemVersionPlist)
let systemVersion = systemVersionDictionary.objectForKey("ProductVersion")
let systemVersionArray = systemVersion.split('.').map(stringNumber => parseInt(stringNumber))
if (systemVersionArray[0] >= 10 && systemVersionArray[1] >= 15) {
return true
} else {
return false
@KevinGutowski
KevinGutowski / moveColors.js
Created Jul 20, 2020
Move Global Assets Colors and Gradients to Document Asset Colors and Graidents
View moveColors.js
let globalAssets = MSPersistentAssetCollection.sharedGlobalAssets()
let gColorAssets = globalAssets.colorAssets()
let gGradientAssets = globalAssets.gradientAssets()
let doc = context.document
doc.documentData().assets().setColorAssets(gColorAssets)
doc.documentData().assets().setGradientAssets(gGradientAssets)
View resetImageFills.js
let presetPath = NSBundle.mainBundle().bundlePath() + '/Contents/Resources/assets.sketchpreset';
let presetURL = NSURL.fileURLWithPath(presetPath)
let nativeAssets = MSPersistentAssetCollection.assetCollectionWithURL(presetURL)
let imageCollection = nativeAssets.imageCollection()
let images = nativeAssets.images()
MSPersistentAssetCollection.sharedGlobalAssets().setImages(images)
MSPersistentAssetCollection.sharedGlobalAssets().setImageCollection(imageCollection)
@KevinGutowski
KevinGutowski / inspect.js
Created Jun 17, 2020
Internal API Inspection
View inspect.js
function dump_obj(obj){
log("#####################################################################################")
log("## Dumping object " + obj )
log("## obj class is: " + [obj className])
log("#####################################################################################")
log("obj.properties:")
log("#####################################################################################")
log([obj class].mocha().properties())
log("obj.propertiesWithAncestors:")
log([obj class].mocha().propertiesWithAncestors())
@KevinGutowski
KevinGutowski / attachModal.js
Last active Jun 14, 2020
Attach modal to Sketch document
View attachModal.js
let window = document.sketchObject.window()
let fiber = coscript.createFiber()
let alert = NSAlert.alloc().init()
alert.addButtonWithTitle("OK")
alert.addButtonWithTitle("Cancel")
alert.setMessageText("This is the message text")
alert.setInformativeText("This is some extra informative text")
alert.setAlertStyle(NSAlertStyleWarning)
@KevinGutowski
KevinGutowski / highlightFirstLetter.js
Last active Jun 13, 2020
Attempting to Highlight First Letter
View highlightFirstLetter.js
// Create a text layer within an artboard and select the layer to run properly
let Sketch = require('sketch')
let ShapePath = Sketch.ShapePath
let Rectangle = Sketch.Rectangle
// Removes old highlight layer so you can repeatedly run this code
let doc = Sketch.getSelectedDocument()
let oldHighlight = doc.pages[0].layers[0].layers.find(layer => layer.name=="highlight")
if (oldHighlight) { oldHighlight.remove() }
View NSAttributedStringAttributes
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selection = doc.selectedLayers.layers[0]
let menu = NSApplication.sharedApplication().mainMenu()
let pluginsMenu = menu.itemWithTitle('Plugins').submenu()
console.log(pluginsMenu.itemArray()[0].title())
/*
let path = "/Users/Kski/Downloads/file.rtf"
let rtfData = NSData.dataWithContentsOfFile(path)
@KevinGutowski
KevinGutowski / imageAttachment.js
Created Jun 10, 2020
Image Attachment to a Text Object
View imageAttachment.js
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selection = doc.selectedLayers.layers[0]
let imgPath = "/Users/Kski/Downloads/image-2.png"
let fileWrapper = NSFileWrapper.alloc().initWithPath(imgPath)
let attachment = NSTextAttachment.alloc().initWithFileWrapper(fileWrapper)
attachment.bounds = NSMakeRect(0,0,100,30) //seems like these bounds don't do anything?
// Currently could only figure out how to add it in when editing
View readPasteboard.js
let items = NSPasteboard.generalPasteboard().pasteboardItems()
console.log(items[0].types())
console.log(items[0].dataForType('com.bohemiancoding.sketch.v3'))
// __NSArrayM [
'com.bohemiancoding.sketch.v3',
'com.adobe.pdf',
'public.tiff',
'com.bohemiancoding.sketch.v3.tempfile',
'dyn.ah62d4rv4gu8y4y4xsv6023nukm10c6xenv61a3k',
'dyn.ah62d4rv4gu8y4y4ftb2g86xym72hk4ptr33zauxtqf3gkzd3sbwu' ]
You can’t perform that action at this time.