Skip to content

Instantly share code, notes, and snippets.

@janily
Last active June 17, 2020 00:50
Show Gist options
  • Save janily/2a3c43cc3250c88fadea7e3e4f516128 to your computer and use it in GitHub Desktop.
Save janily/2a3c43cc3250c88fadea7e3e4f516128 to your computer and use it in GitHub Desktop.
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]);
const group = sketch.createLayerFromData(stringResponse,'svg')
group.parent = selectedArtboard
// 或者是
import sketch from 'sketch'
// documentation: https://developer.sketchapp.com/reference/api/
export default function() {
fetch("https://raw.githubusercontent.com/simple-icons/simple-icons/develop/icons/github.svg")
.then(response => {
return response.text()
})
.then(text => {
console.log(text)
// create symbol here from text
const group = sketch.createLayerFromData(text,'svg')
})
.catch(e => console.error(e))
}
var svgString = NSString.stringWithString('<svg width="200px" height="100px" viewBox="0 0 200 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect fill="#000000" x="0" y="0" width="200" height="100"></rect></svg>');
var svgData = svgString.dataUsingEncoding(NSUTF8StringEncoding);
var svgImporter = MSSVGImporter.svgImporter();
svgImporter.prepareToImportFromData(svgData);
var svgLayer = svgImporter.importAsLayer();
svgLayer.setName("LayerName");
context.document.currentPage().addLayers([svgLayer]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment