2008 05 18Exposing 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.
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.
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 :)
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.