Parmanoir

JSCocoa, a bridge from Javascript to Cocoa

JSCocoa ...
  • allows you to write Cocoa programs in Javascript (via JavascriptCore, WebKit's Javascript engine)
  • works on x86 and PPC and requires Leopard (an uptodate Leopard, 10.5.2 from back then crashes)
  • is open source !

Syntax

  • Only dots ! Everyone loves dots !
    NSWorkspace.sharedWorkspace.activeApplication.NSApplicationName

  • An instance command to replace alloc.init(…)
    NSButton.instance({ withFrame:NSMakeRect(0, 0, 100, 40) }) 

  • More dots ! Did you know that a message like setTitle: is really a dot trapped in a closet, begging to come out ?
    // Changes window title
    window.title = 'Hello !'
    

  • A split call syntax, to almost have ObjC-like calls — if you use jQuery you'll feel right at home
    this.perform({ selector:'printSomeDots:', withObject:null, afterDelay:0 })

  • Some JS-like manipulation of arrays and dicts :
    myNSMutableDictionary.mykey = 'value'
    myNSMutableArray[5] = 'hello world !'
    

    But Javascript arrays and hashes are not (yet) converted to NSArray and NSDictionary.
    Meaning object.wantsADictionary({fontSize:14}) will fail.


And of course the Javascript benefit of ending your lines without semicolon barbwire.

Samples

There's only one :) — A Core Animation process viewer.
Image:JSCocoa process viewer.png

Get it

If you want to contribute, mail me at [email protected] for svn access.

Mickaël Menu
2008 10 09

Ce projet à l'air vraiment prometteur, je vais le suivre avec attention. :) Est-ce que c'est possible d'isoler l'interpreteur de script afin d'éviter que les scripts puissent atteindre toute l'application hôte ? (Pour faire un système de plugins JavaScript, par exemple).

Patrick Geiller
2008 10 09

(English below ! :) )

Oui, c'est possible. Pour l'instant, tout est ouvert et l'interpreteur passe par 2 endroits pour appeler des fonctions C ou Cocoa :

  • OSXObject_getPropertyCallback recupere les fonctions C classes ObjC, constantes : filtre propertyName pour interdire des fonctions C ou classes ObjC (interdire tout acces a NSView par ex)
  • jsCocoaObject_getProperty recupere les propriétés des instances : le nom d'un method, d'un propriété : filtre propertyName et inspecte callee pour interdire l'acces a des methodes ou propriétés d'une instance.

// English !

Right now everything is open : anyone can call anything : any C function, class method, …

  • To restrict access to only certain classes, C functions, structs, constants : filter propertyName in OSXObject_getPropertyCallback
  • To restrict access to only certain methods of certain classes, (eg allowing to call anything but alloc and init in NSView) filter propertyName with the help of callee (the instance being called) in OSXObject_getPropertyCallback
Tom Robinson
2008 10 10

Here's a command line wrapper I threw together: http://tlrobinson.net/blog/?p=55

Rahul
2008 10 10

thanks for this javascript. This is certainly very good. cheers mate.


Follow me on Twitter
Planet Cocoa
Cocoa.fr

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki