github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

parmanoir / jscocoa

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 103
    • 7
  • Source
  • Commits
  • Network (7)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Write Cocoa applications in Javascript — Read more

  cancel

http://inexdo.com/JSCocoa

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Cutom assign operator @= translated to setValue:forKey: 
parmanoir (author)
Sun Feb 07 16:48:58 -0800 2010
commit  13906737746174e4f31d4b6d88384cd177f82baf
tree    24891fb8f1a6cf4efcd888a8e61485caf25a269f
parent  7d58498a35694d0000e9ec485c7625ea306b098d
jscocoa /
name age
history
message
directory GC ObjC JSCocoa/ Fri Mar 20 04:12:03 -0700 2009 CHECK - removed tests 11 12 15 30 32 - works wi... [parmanoir]
directory GUIConsole/ Mon Jan 04 00:56:48 -0800 2010 Added protocol specifier check in ObjJ methods [parmanoir]
directory JSCocoa/ Sun Feb 07 16:48:58 -0800 2010 Cutom assign operator @= translated to setValue... [parmanoir]
directory JSCocoaLauncher OLD/ Fri Apr 03 07:22:28 -0700 2009 starting new launcher [parmanoir]
directory JSCocoaLauncher/ Wed Dec 02 07:06:28 -0800 2009 Check markedText for editor [parmanoir]
directory JSLocalizedString/ Tue Mar 10 09:41:15 -0700 2009 Made loading class.js optional [parmanoir]
file License Thu Feb 12 13:33:29 -0800 2009 Added license [parmanoir]
directory Multiple JSCocoa instances/ Sun Sep 13 18:08:02 -0700 2009 fixed a crasher where references to dealloced g... [August Mueller]
directory NSLogConsole from JSCocoa/ Tue Dec 30 17:06:02 -0800 2008 first commit [parmanoir]
directory ProcessViewer/ Sat Nov 14 09:37:08 -0800 2009 GUIConsole fix [parmanoir]
file README.markdown Sat Nov 14 09:29:44 -0800 2009 GUIConsole fix [parmanoir]
directory Samples/ Thu Feb 04 09:47:40 -0800 2010 Added autocall warning when autocall is ON and ... [parmanoir]
directory Tests/ Sun Feb 07 16:48:58 -0800 2010 Cutom assign operator @= translated to setValue... [parmanoir]
directory TestsRunner/ Sun Feb 07 16:48:58 -0800 2010 Cutom assign operator @= translated to setValue... [parmanoir]
directory WebKit additions/ Mon Sep 28 17:03:10 -0700 2009 JSCocoa WebKit sample doc update [parmanoir]
directory XCode Templates/ Tue Dec 30 17:06:02 -0800 2008 first commit [parmanoir]
directory iPhone/ Thu Feb 04 09:47:40 -0800 2010 Added autocall warning when autocall is ON and ... [parmanoir]
README.markdown

JSCocoa, a bridge from Javascript to Cocoa

With JSCocoa, you can write Cocoa applications (almost) entirely in Javascript or use it as a Plugin engine (like Acorn and Spice). JSCocoa uses WebKit's Javascript framework, JavascriptCore.

JSCocoa is a way to use Cocoa from Javascript. It works on the Mac (i386, x86_64, PPC), the iPhone and the iPhone simulator. You can write new Cocoa classes in Javascript, replace existing methods of classes by Javascript functions (swizzling them) and call Javascript functions on Cocoa objects (call filter on an NSArray, or use Javascript regular expressions on NSString with myNSString.match(/pattern/)).

JSCocoa can also be used as a replacement for the existing WebKit bridge, letting you use C functions, structs, and calling pretty much anything from your WebView. Access restriction can be setup by JSCocoa's delegate messages (canGetProperty:ofObject:inContext:, canCallMethod:ofObject:argumentCount:arguments:, etc.)

Basically, JSCocoa works like these bridges :

  • RubyCocoa, MacRuby write Cocoa in Ruby
  • PyObjC write Cocoa in Python
  • LuaCore write Cocoa in Lua

JSCocoa isn't a Javascript framework to use on the Web. For that, check out :

  • Cappuccino an open source framework that makes it easy to build desktop-caliber applications that run in a web browser
  • SproutCore makes building javascript applications fun and easy

Contribute and discuss

  • Discussion group Questions ? Join the Google group and ask away !
  • Twitter Tweet me questions and comments
  • Github fork JSCocoa from Github, add changes, and notify me with a pull request
  • Documentation on Google Code

Who uses it ?

  • Spice-sugar Spice.sugar allows the Espresso text editor to be extended using JSCocoa
  • Narwhal-jsc A JavascriptCore + optional JSCocoa module for Narwhal (Server-side Javascript)
  • JSTalk Gus Mueller, to let Cocoa applications be scripted in Javascript
  • PluginManager Grayson Hansard wrote a manager that enables you to write Cocoa plugins in AppleScript, F-Script, Javascript, Lua, Nu, Python and Ruby.
  • Elysium Matt Mower, to script Elysium, a MIDI sequencer
  • Acorn Plugins Gus Mueller, to let Acorn users write Acorn plugins in Javascript
  • Interactive console for iPhone Kumagai Kentaro wrote a console to interact with the iPhone simulator from a web page !
  • JSCocoaCodaLoader write Javascript plugins that work in Coda
  • REPL console Tom Robinson's command line interface

Are you missing on that list ? Send me a mail !

What does it look like ?

// Get current application name : dot syntax
var appName = NSWorkspace.sharedWorkspace.activeApplication.NSApplicationName
// The same with Objective-J syntax
var appName = [[[NSWorkspace sharedWorkspace] activeApplication] NSApplicationName]

// Alloc an object (need to release)
var button = NSButton.alloc.initWithFrame(NSMakeRect(0, 0, 100, 40))
// Alloc an object (no need to release)
var button = [NSButton instanceWithFrame:NSMakeRect(0, 0, 100, 40)]

// Setting : an ObjC method starting with 'set' can be set like a Javascript property
var window = NSWorkspace.sharedWorkspace.activeApplication.keyWindow
// Instead of calling setTitle ...
window.setTitle('new title')
// ... set the 'title' property
window.title = 'new title'

// Call methods with Objective-J syntax
[obj callWithParam1:'Hello' andParam2:'World']
// Or with a jQuery-like syntax (need to be enabled with __jsc__.setUseSplitCall = true)
obj.call({ withParam1:'Hello', andParam2:'World' }) 
obj['callWithParam1:andParam2:']('Hello', 'World') 
obj.callWithParam1_andParam2('Hello', 'World' )

// Unicode identifiers !
function    追加する(最初の, 次の) {   return 最初の+ 次の }
var 結果 = 追加する('こんにちは', '世界')
NSApplication.sharedApplication.keyWindow.title = 結果

// Write a new Cocoa class in Javascript (inspired by Cappucino)
class MyClass < NSObject
{
    // Custom drawing, calling parent method
    - (void)drawRect:(NSRect)rect
    {
        // do some drawing here
        ...
        // Call parent method
        this.Super(arguments)           
    }
    // Class method
    + (float)addFloatX:(float)x andFloatY:(float)y
    {
        return x + y
    }
}

// Manipulate an existing class
class NSButton
{
    // Add a method to an existing class
    - (void)someNewMethod:(NSString*)name
    {
        ...
    }

    // Swizzle an instance method of an existing class
    Swizzle- (void)drawRect:(NSRect)rect
    {
        // Draw something behind the button
        ...
        // Call original swizzled method
        this.Original(arguments)
        // Draw something in front of the button
        NSBezierPath.bezierPathWithOvalInRect(rect).stroke
    }
}

Starting up

This will start a controller, eval a file, call a Javascript method and get an ObjC object out of it. You can start multiple interpreters, e.g. one for each document.

// Start
JSCocoa* jsc = [JSCocoa new];

// Eval a file
[jsc evalJSFile:@"path to a file"];
// Eval a string
[jsc evalJSString:@"log(NSWorkspace.sharedWorkspace.activeApplication.NSApplicationName)"];

// Add an object of ours to the Javascript context
[jsc setObject:self withName:@"controller"];

// Call a Javascript method - we can use any object we added with setObject
JSValueRef returnValue = [jsc callJSFunctionNamed:@"myJavascriptFunction" withArguments:self, nil];
// The return value might be a raw Javascript value (null, true, false, a number) or an ObjC object
// To get an ObjC object
id resultingObject = [jsc toObject:returnValue];

// (Cleanup : only needed if you don't use ObjC's Garbage Collection)
[jsc release];

Add JSCocoa to your project

Use it as a framework :

  • Build JSCocoa/JSCocoa.xcodeproj
  • Add built JSCocoa.framework to your project
  • import <JSCocoa/JSCocoa.h>

Integrate its files directly into your project :

  • Drag the JSCocoa folder in your project
  • Delete irrelevant files (Info.plist, JSCocoa_Prefix.pch, English.lproj, project files)
  • Add the JavascriptCore framework
  • In 'Build' project settings, add -lffi to 'Other linker flags'
  • import "JSCocoa.h"

Thanks !

  • 280 North — Objective-J syntax idea
  • Douglas Crockford — JSLint
  • Gus Mueller — Distant Object code
  • Jonathan 'Wolf' Rentzsch — JRSwizzle

Questions, comments, patches

Send me a mail !

Patrick Geiller
parmanoir@gmail.com

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server