有关 CocoaScript 的更多信息
From CocoaScript’s README:
- Every thing else isn’t. That means that
consoleetc. are not available.
- NodeJS core modules are not available.
Accessing Cocoa and Sketch APIs
You can access all Cocoa and Sketch APIs from CocoaScript.
- All colons are converted to underscores (the latest underscore is optional).
- Each component of the selector is concatenated into a single string with no separation.
As such, a selector such as
executeOperation:withObject:error: is converted to the function name
For example, if you want to open a File Picker panel, you can use the NSOpenPanel class:
var openPanel = NSOpenPanel.openPanel() openPanel.setCanChooseDirectories(false) openPanel.setCanChooseFiles(true) openPanel.setCanCreateDirectories(false) openPanel.setDirectoryURL(NSURL.fileURLWithPath('~/Documents/')) openPanel.setTitle('Choose a file') openPanel.setPrompt('Choose') openPanel.runModal()
If you need more information about Cocoa, check the Resources section.
A few specific globals
var ptr = MOPointer.alloc().init() var ptrToSomething = MOPointer.alloc().initWithValue(something)
Long running script
If your script is doing something asynchronous, we will need to tell Sketch to keep it around and to not garbage collect it.
You can do so by accessing
COScript.currentCOScript().shouldKeepAround = true
When the script has finished its work, don’t forget to release it:
COScript.currentCOScript().shouldKeepAround = false
For more information about how the bridge really works, take a look at the Mocha Readme, it is really complete (but requires some notion of Obj-C).