Tag Archive for macros

Alter an existing, hard-coded onclick function

/*
* When the button is moused over, the user is probably about to click
* At that pont, recalculate the onclick method
* so as to give the correct category list parameter to Rails
*/
Event.observe('filterButton', 'mouseover', function() {
var allthecheckboxes = $$('#catfilter li input');
var selectedCats = [];
var railsAjaxFn = this.onclick.toString();
allthecheckboxes.each(function(el){
if (el.checked) {
var cat = el.id.match(/d+$/);
if (cat != null) {
var catnumber = cat[0];
selectedCats.push(catnumber);
//console.debug(el, catnumber, selectedCats);
}
}
});
//console.debug(selectedCats);
var newFnString = railsAjaxFn.replace(/?.*?"/, '?' + escape('[]') + '=' + selectedCats.toString() + '"');
var fnBodyString = newFnString.match(/.*}/g)[0];
var newFn = new Function(['event'], fnBodyString + ');
return false;');
this.onclick = newFn;
console.debug(fnBodyString, newFn);
//this.onclick();
});

source

Useful Cocoa Macros

#define APP_NAME [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]
#define APP_VERSION [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
#define OPEN_URL(urlString) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]]

// Retrieving preference values
#define PREF_KEY_VALUE(x) [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:(x)]
#define PREF_KEY_BOOL(x) [(PREF_KEY_VALUE(x)) boolValue]
#define PREF_SET_KEY_VALUE(x, y) [[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:(y) forKey:(x)]
#define PREF_OBSERVE_VALUE(x, y) [[NSUserDefaultsController sharedUserDefaultsController] addObserver:y forKeyPath:x options:NSKeyValueObservingOptionOld context:nil];

/* key, observer, object */
#define OB_OBSERVE_VALUE(x, y, z) [(z) addObserver:y forKeyPath:x options:NSKeyValueObservingOptionOld context:nil];

#ifdef __OBJC__
static inline BOOL isEmpty(id thing) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
#endif

source