Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Last active November 1, 2021 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinGutowski/6a8c20cfe08dde97fc3092e9333d768f to your computer and use it in GitHub Desktop.
Save KevinGutowski/6a8c20cfe08dde97fc3092e9333d768f to your computer and use it in GitHub Desktop.
Reading/writing a sketch file without opening it
let sketch = require('sketch')
let Document = sketch.Document
let path = "/Users/kgutowski/Downloads/Test.sketch"
let url = NSURL.fileURLWithPath(path)
let externalDoc = MSDocument.alloc().init()
externalDoc.readDocumentFromURL_ofType_error(url, "com.bohemiancoding.sketch.drawing", nil); // Be sure to capture error properly
let jsDoc = Document.fromNative(externalDoc)
console.log(jsDoc)
let doc = MSDocument.alloc().init()
let url = NSURL.URLWithString('/Users/Kski/Downloads/mysketchfile.sketch')
doc.writeToURL_ofType_options_error(url,"com.bohemiancoding.sketch.drawing",nil,nil)
@humbleCoder1990
Copy link

humbleCoder1990 commented Oct 30, 2021

Hi Kevin.

I came to your github while searching Swift codes.

You are using "NSURL" in javascript.

How do you access object.c or swift object in Javascript ?

I'm writing app using node.js, I want to call some swift function in node.js

@KevinGutowski
Copy link
Author

You can’t call swift functions with only node.js. You will need to have your app compiled for MacOS and to have node bundled in it, and then use something like Cocoascript to bridge your JS commands to objective-c. I recommend if you want to write Swift then switch over to writing a Mac app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment