How to use NSArrayController + NSTableView

Setting heading levels

if (![[range startContainer] isEqualNode:[range endContainer]])

return NO; // does not work with cross-node selections

if ([range startOffset] == [range endOffset])

return NO; // no selection.

// remove any heading tags from parents.

NSString *textContent = [[range startContainer] textContent];

NSString *leftStr = nil;

NSString *midStr = nil;

NSString *rightStr = nil;

// there is always a mid part, that's selected

midStr = [textContent substringWithRange:NSMakeRange([range startOffset], [range endOffset]-[range startOffset])];

// is any text left on the left

if ([range startOffset] > 0)

leftStr = [textContent substringToIndex:[range startOffset]];

// is any text left on the right

if ([range endOffset] < [[[range startContainer] textContent] length])

rightStr = [textContent substringFromIndex:[range endOffset]];

DOMNode *mid = [[range startContainer] parentElement]; // parent elements contains the tags wrapping the DOMText

[mid setTextContent:midStr];

if (leftStr != nil) {

DOMNode *first = [mid cloneNode:YES];

[first setTextContent:leftStr];

[[mid parentNode] insertBefore:first refChild:mid];


if (rightStr != nil) {

DOMNode *last = [mid cloneNode:YES];

[last setTextContent:rightStr];

[[mid parentNode] appendChild:last];


if (level > 0) {

[self wrapNewTag:[NSString stringWithFormat:@"h%d", level] aroundContentsOfElementNode:(DOMElement *)mid];

} else {

[self wrapNewTag:[NSString stringWithFormat:@"p", level] aroundContentsOfElementNode:(DOMElement *)mid];


[self HTMLUpdated];

return YES;

Copy Paste in WebView: Links


Pasteboard programming guide:


User Interface Validation example for enabling/disabling copy/paste buttons:


WebKit pasteboard:


WebKit dragging:
notes: by default for images in editable mode. can be added to custom divs!


DOMTreeWalker *treeWalker = [webView.mainFrameDocument createTreeWalker:[range commonAncestorContainer] whatToShow:DOM_SHOW_ALL filter:nil expandEntityReferences:YES];

while ([treeWalker nextNode]) {

NSLog(@"string rep:%@\n text:%@", [[treeWalker currentNode] stringRepresentation], [[treeWalker currentNode] textContent]);


Buttons to Imitate Format Menu Items in NSDocument

A discussion on why you can not use a button to imitate the format menu bar item for toggling bold and italic attributes:

in a nutshell, you use the tag to distinguish between bold (2) and italic (1) traits, but the toggle direction is determined by calling the validateMenuItem method which requires the sender to be a menuItem?

Detecting user inactivity/idle time since last screen touch

Found this gem on stackoverflow. Very useful for the lazy annotation loading on maps when combined with mapview:regionWillChangeAnimated and mapview:regionDidChangeAnimated delegates.

Objective C Programming Tips

Very useful and easy to follow tips:

“5) Set your properties as nonatomic. They’re atomic by default and upon synthesis, semaphore code will be created to prevent multi-threading problems. 99% of you probably don’t need to worry about this and the code is much less bloated and memory efficient when set to nonatomic.”