Image:CocoaNav icon.png

PARMANOIR

Exposing everything to WebKit

When using WebKit with Cocoa, you can expose any ObjC object to Javascript. But there's a catch : you need to specify which keys and selectors are allowed to be called via isKeyExcludedFromWebScript and isSelectorExcludedFromWebScript. But what if we want to expose everything ? Just add these methods to NSobject with a category :
@implementation NSObject(ExposeEverything)

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector {
	return NO;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)property {
	return NO;
}

@end

Now you can expose a dummy object that returns NSApp, and call in your JS code myExposedObject.NSApp().keyWindow().setHasShadow_(false). Pretty neat :), save for the trailing underscore.

Why expose everything ? I've tried RubyCocoa for Cocoa development and have had strange bugs (crashing when adding a comment), plus I don't really like Ruby as a language. It doesn't handle the increment operator ++, multiline comments, and has a strange syntax for blocks — compared to Javascript who uses the unterse function (arg1, arg2) everywhere for functions and lambdas. And Javascript's (scope | inheritance) model, where a (function/lambda | object) inherit its parent (scope | prototype), is just so elegant. I can say I grok Javascript where everything is pretty simple and powerful, even if it's limited, but Ruby just confuses me. I wonder how much development can be done in JS instead of ObjC or RubyCocoa … I'll be exploring.

kourge
2008 06 18

Ruby does have multiline comments. Multilines comments begin with =being and end with =end, although this confused me a lot when I first learned Ruby.

Also, coming from JavaScript to Ruby, I prefer the braces syntax over the do...end syntax when using blocks precisely because it's more syntactically similar to JavaScript.

Patrick Geiller
2008 06 18

=begin … =end confused me too, and googling '=begin' to guess why they need to be positioned exactly at the start of the line or they won't work whas a pain.

That said, there are things I really love in Ruby, namely the return if (condition), the ability to omit parens in if/while/…. Too bad we can't cherry pick features from languages to make our own :)

2008 07 25Where in build phases is that file ?
2008 07 192Imagine clickable error messages
2008 07 18Succulent Stormhoek
2008 07 173Loving the for
2008 07 12(Parmanoir) Feed now validates
2008 07 108Telling classes from instances
2008 07 08Comma Trick
2008 07 06Using libffi
2008 07 04BridgeSupport's type and type64
2008 07 042Clickable Disabled MenuItems
2008 07 026Less bugs through compiler optimizations
2008 06 251CocoaNav JS, a light CocoaNav for Safari
2008 06 232NSWindow goodies : bottomCornerRounded, usesLightBottomGradient
2008 06 222Inspecting NSUndoManager's undo stack
2008 06 16Cocoa Regular Expressions via JavascriptCore
2008 06 15Crossing the WebKit bridge
2008 06 08Double and Triple Click
2008 06 05Photoshop-like compositing with Core Animation
2008 06 052One way binding to NSSlider
2008 05 30Threaded Core Animation, Part Deux
Image:rss.png
Image:rss.png

Powered by MediaWiki

Hi ! I'm learning Cocoa to (hopefully !) become an indie developer.

I've written software all my professional life, in C++, PHP, Javascript. I've designed websites and web interfaces. My last venture went into flames as clients were happy but didn't like paying very much.

I've had little luck in the B2B world, I'm hoping for a better future writing Mac applications.

Planet Cocoa