Skip to content

Instantly share code, notes, and snippets.

@janily
janily / sketch libaray
Created May 6, 2021 02:25
get libaray props
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let libraries = sketch.getLibraries()
let textStyles = doc.sharedTextStyles
//var importedStyles = []
//var textStyleRefs = libraries.getImportableTextStyleReferencesForDocument(doc)
//textStyleRefs.forEach( ref => importedStyles.push(ref.import()))
@janily
janily / Frame.js
Created November 17, 2020 09:14 — forked from robertgonzales/Frame.js
Use React portals to render inside shadow dom and iframes
class Frame extends Component {
componentDidMount() {
this.iframeHead = this.node.contentDocument.head
this.iframeRoot = this.node.contentDocument.body
this.forceUpdate()
}
render() {
const { children, head, ...rest } = this.props
return (
@janily
janily / randomDomainNameGenerator.js
Created October 14, 2020 05:44 — forked from pedrouid/randomDomainNameGenerator.js
Random Domain Name Generator
// @desc Creates a list of domains with custom TLDs using a pattern string with fixed length with option for keywords
// @param {String} pattern
// @param {Array|String} tlds
// @returns {Array} domainList
//
//
// Pattern string is defined by:
// - keywords lowercase
// - any letter uppercase A
// - consonants uppercase C
@janily
janily / mouse location
Created July 26, 2020 15:44
mouse location
function mouseInCanvasViewForDocument(document) {
var mouseInWindow = document.documentWindow().convertScreenToBase(NSEvent.mouseLocation());
return document.contentDrawView().convertPoint_fromView(mouseInWindow,null);
}
function CGPointToObject(point) {
return {
x: point.x.doubleValue(),
y: point.y.doubleValue()
}
@janily
janily / auto layout
Last active July 17, 2020 08:40
auto layout
let selection = doc.selectedLayers
selection.forEach(layer => {
var sel = layer.sketchObject
if (sel.resizeToFitChildrenWithOption) {
sel.resizeToFitChildrenWithOption(0);
} else {
sel.fixGeometryWithOptions(0);
@janily
janily / Import SVG from http
Last active June 17, 2020 00:50
Import SVG from http
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selectedArtboard = doc.selectedLayers.layers[0]
var request = NSMutableURLRequest.alloc().init();
request.setHTTPMethod_("GET");
request.setURL(NSURL.URLWithString('https://upload.wikimedia.org/wikipedia/commons/5/59/Sketch_Logo.svg'));
var responseData = NSURLConnection.sendSynchronousRequest_returningResponse_error(request,nil,nil);
var stringResponse = NSString.alloc().initWithData_encoding(responseData,NSUTF8StringEncoding]);
@janily
janily / text is selected update font properties
Created June 16, 2020 11:22
text is selected update font properties
const Sketch = require('sketch')
framework("CoreText");
let myTextLayer = Sketch.getSelectedDocument().selectedLayers.layers[0]
let textLayer = myTextLayer.sketchObject
if (textLayer.isEditingText() == 0) {
console.log("select some text")
} else {
@janily
janily / Get selected layers object
Created June 16, 2020 11:21
Get selected layers object
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selectedLayersObject = doc.selectedLayers
let selectedLayersArray = selectedLayersObject.layers
let interalLayersArray = selectedLayersArray.map(layer => layer.sketchObject)
// convert your JS layers to internal Sketch Objects
let msLayerArray = MSLayerArray.arrayWithLayers(interalLayersArray)
// MSSymbolCreator actually needs this MSLayerArray object, not just a NSArray of MSLayers for some reason
@janily
janily / Select all layers in selected artboard
Created June 16, 2020 11:18
Select all layers in selected artboard
var sketch = require('sketch')
export default () => {
const doc = sketch.getSelectedDocument()
const selectedArtboard = doc.selectedLayers.layers[0]
function selectChildren(layer) {
layer.selected = true
if (layer.layers) {
layer.layers.forEach(selectChildren)
@janily
janily / resize symbol
Last active June 16, 2020 11:17
resize symbol
selection.sketchObject.resizeToFitContentsIfNeededNoCache() //重置 symbol 尺寸
layer.select_byExpandingSelection(true,false) // 设置图层为选中状态