CocoaScript

CocoaScript is a bridge providing access to the internal Sketch APIs and macOS frameworks in JavaScript.

From CocoaScript’s README:

CocoaScript is built on top of Apple’s JavaScriptCore, the same JavaScript engine that powers Safari. So when you write in CocoaScript, you are really writing JavaScript.

CocoaScript also includes a bridge which lets you access Apple’s Cocoa frameworks from JavaScript. This means you have a ton wonderful classes and functions you can use in addition to the standard JavaScript library.

Syntax

The square bracket syntax of Objective-C is converted to dot-syntax in JavaScript. Internally, CocoaScript creates opaque JavaScript proxy objects which have the following attributes:

  • Objective-C properties are exported to JavaScript:
    • Getter: object.name()
    • Setter: object.name = 'Sketch'
  • Objective-C selectors are exposed as methods of the JavaScript proxy.
  • : are converted to _, the last underscore is optional.
  • Calling a method with an underscore requires you to double up: sketch_method becomes sketch__method
  • Each component of the selector is concatenated into a single string with no separation.

Objective-C

[executeOperation:withObject:error:]

JavaScript

executeOperation_withObject_error()

Pointers

Some Objective-C selectors require pointer parameters. Since JavaScript does not support passing objects by reference CocoaScript provides MOPointer, a proxy object to create references from variables.

let str = NSMutableString.alloc().init()
let pointer = MOPointer.alloc().initWithValue(str)

str.setString('Hello Sketch')
console.log(pointer.value())

str.appendString(' 👋')
console.log(pointer.value())

Use macOS Frameworks

To use a macOS framework, it needs to be imported first. Please note that Foundation and CoreGraphics are imported by default.

framework('AVFoundation')

let url = NSURL.alloc().initWithString(
  'https://cdn.sketchapp.com/assets/pages/home/prototyping-video1.mp4'
)
let asset = AVAsset.assetWithURL_(url)

console.log(asset)

Read more about how to use CocoaScript and macOS frameworks.

See something wrong or incomplete? Improve this page.