<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Espresso Served Here</title>
	<atom:link href="http://espresso-served-here.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://espresso-served-here.com</link>
	<description>Mac applications, brewed to perfection</description>
	<pubDate>Fri, 04 Jul 2008 18:26:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>NSViewController Late Night Cocoa Episode</title>
		<link>http://espresso-served-here.com/2008/07/04/nsviewcontroller-late-night-cocoa-episode/</link>
		<comments>http://espresso-served-here.com/2008/07/04/nsviewcontroller-late-night-cocoa-episode/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 18:26:12 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=59</guid>
		<description><![CDATA[You may have seen the articles Cathy Shive and I wrote on her blog about NSViewController and how to get it working well in your apps.  Well now you can hear us talk about it!  Scotty of the Mac Developer Network kindly had us both on for an episode of Late Night Cocoa, [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>You may have seen the <a href="http://katidev.com/blog/2008/04/09/nsviewcontroller-the-new-c-in-mvc-pt-1-of-3/">articles</a> <a href="http://katidev.com">Cathy Shive</a> and I wrote on her blog about <code>NSViewController</code> and how to get it working well in your apps.  Well now you can hear us talk about it!  Scotty of the <a href="http://www.macdevnet.com">Mac Developer Network</a> kindly had us both on for an episode of Late Night Cocoa, which you can download from iTunes <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=213023580">here</a>.  It&#8217;ll be on the MacDevNet site too.</p>
<p>I talked about my preferences window system, I&#8217;ll have to wait a few weeks until I can put it up here with a sample app.  Work will let up in August and I&#8217;ll have time to iron out a few bugs.</p>
<p>Thanks again to Scotty for having me on the episode, it was awesome!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/59/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/59/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=59&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/07/04/nsviewcontroller-late-night-cocoa-episode/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>Site redesign imminent</title>
		<link>http://espresso-served-here.com/2008/06/27/site-redesign-imminent/</link>
		<comments>http://espresso-served-here.com/2008/06/27/site-redesign-imminent/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 10:11:58 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=58</guid>
		<description><![CDATA[I&#8217;m slowly getting everything ready to get espresso served here off the ground.  Part of that has been talking to a couple of graphic designers about site design and icon design.  After all, I&#8217;m a programmer and I can&#8217;t draw for the life of me.  Photoshop to me is an app that just puts loads [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;m slowly getting everything ready to get espresso served here off the ground.  Part of that has been talking to a couple of graphic designers about site design and icon design.  After all, I&#8217;m a programmer and I can&#8217;t draw for the life of me.  Photoshop to me is an app that just puts loads of stuff all over my system.</p>
<p>Hopefully sometime in August things will start to take shape.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/58/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/58/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=58&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/06/27/site-redesign-imminent/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating iTunes Scrollers</title>
		<link>http://espresso-served-here.com/2008/06/18/creating-itunes-scrollers/</link>
		<comments>http://espresso-served-here.com/2008/06/18/creating-itunes-scrollers/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 09:02:11 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Cocoa]]></category>

		<category><![CDATA[How-to]]></category>

		<category><![CDATA[Mac Stuff]]></category>

		<guid isPermaLink="false">http://espresso-served-here.com/?p=55</guid>
		<description><![CDATA[Apple introduced new scrollers in iTunes 7 and then moved on to give us the HUD, which many developers want their own scrollers for too.  In Leopard, many of us thought that these would come in a nice, shiny box; but as they didn’t we’re all forced to roll our own.  The common [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Apple introduced new scrollers in iTunes 7 and then moved on to give us the HUD, which many developers want their own scrollers for too.  In Leopard, many of us thought that these would come in a nice, shiny box; but as they didn’t we’re all forced to roll our own.  The common method is to draw all the components in Photoshop and then make a composite image when subclassing, but now with <code>NSGradient</code> and some nice additions to <code>NSBezierPath</code> all these have become quite easy to do, even for those with little artistic ability.<br />
<span id="more-55"></span><br />
My first attempt was to do all of this using <code>NSAffineTransform</code>s, but without being able to mirror a bezier path, or define the the centre of rotation of a path or NSRect it becomes a loathsome task of tweaking and it just never quite works.  So all the elements are drawn separately in this example, which is more code, but turned out to be far easier to code.</p>
<p>The important point to realise when subclassing <code>NSScroller</code> is that Apple’s documentation is out of date and some of the methods that we immediately assume we can subclass aren’t called.  You can read the details in this thread on the cocoa-dev mailing list.  You end up having to also implement -drawRect: and calling these documented (but unused) methods yourself.</p>
<p>The <code>NSScroller</code> consists first of the <code>NSScrollerKnobSlot</code>, the track on which the <code>NSScrollerKnob</code> itself lies.  There are 2 (normally) buttons: in the vertical case the up arrow defined as the <code>NSScrollerDecrementLine</code> and the down arrow is the <code>NSScrollerIncrementLine</code>.  I’ve implemented a method that checks to see which configuration the user has the scrollbars in, whether they are at the top and bottom or both together at the bottom.  It does this by examining the <code>AppleScrollBarVariant</code> key in the global user defaults.  If this (AFAICT undocumented) key changes then it calculated which configuration the user has selected.  So when using these scrollers, the user can switch the configuration in System Preferences and the change can be seen.  In the mode with both together at the bottom of the bar, there is also a top ‘socket’ that’s drawn.</p>
<p>The scrollbars also support horizontal scrolling, and I found that instantiating the vertical scroller without a frame works fine, but the only way to get a horizontal scroller is by using <code>-initWithFrame:</code>. I can only assume that apple are doing something under-the-hood for horizontal ones.</p>
<p>The final hiccup comes when you can to support the last 20% of users that use the scrollbar variant with both buttons at both ends.  This is done in terminal and thankfully requires a restart of any program in which you want it to take effect.  Problems come when trying to support this case when you want to know which button has been hit so you can draw it highlighted.  The documented stuff from apple returns the same value whether the decrement line one end or the other is clicked! Making this impossible without tracking the mouse yourself, and I haven’t had the time to fix that.  If I were using a program that didn’t respect my settings (I’m looking at you General Electric), then I’d get angry, so I included an <code>NSScrollView</code> subclass that checks for this case and just uses Apple’s scrollers if the user has selected it.</p>
<p>I think they’re pretty close to the iTunes ones, but mine are resoution-independent, but only Apple can give the perfect solution.  You can download the XCode (3.1) project and sample app (10.5-only) <a href="http://www.mediafire.com/?xjwxtogm9jj">here</a> (<a href="http://rapidshare.com/files/123276316/Scroll_Bar.zip.html">alt link</a>).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/55/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/55/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=55&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/06/18/creating-itunes-scrollers/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>New Site Address www.espresso-served-here.com</title>
		<link>http://espresso-served-here.com/2008/06/15/new-site-address-wwwespresso-served-herecom/</link>
		<comments>http://espresso-served-here.com/2008/06/15/new-site-address-wwwespresso-served-herecom/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 12:54:24 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=54</guid>
		<description><![CDATA[Hi all,
Just thought I&#8217;d let you know that you can now reach this website at www.espresso-served-here.com.  I&#8217;m going to slowly be making changes to the site as I develop the brand for my software company.  All previous links around the web to http://jonathandann.wordpress.com will still work though.
Jon
       [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hi all,</p>
<p>Just thought I&#8217;d let you know that you can now reach this website at <a href="http://www.espresso-served-here.com">www.espresso-served-here.com</a>.  I&#8217;m going to slowly be making changes to the site as I develop the brand for my software company.  All previous links around the web to <a href="http://jonathandann.wordpress.com">http://jonathandann.wordpress.com</a> will still work though.</p>
<p>Jon</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/54/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/54/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=54&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/06/15/new-site-address-wwwespresso-served-herecom/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>NSTreeController and Core Data, Sorted.</title>
		<link>http://espresso-served-here.com/2008/05/13/nstreecontroller-and-core-data-sorted/</link>
		<comments>http://espresso-served-here.com/2008/05/13/nstreecontroller-and-core-data-sorted/#comments</comments>
		<pubDate>Tue, 13 May 2008 20:00:08 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Cocoa]]></category>

		<category><![CDATA[Mac Stuff]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=49</guid>
		<description><![CDATA[Having recently taken the plunge into Core Data I decided it was time to rip out all the model code from my current application and replace it with a Core Data version.  After about a day I had my app up and running again but with one huge problem, the content of my NSOutlineView [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Having recently taken the plunge into Core Data I decided it was time to rip out all the model code from my current application and replace it with a Core Data version.  After about a day I had my app up and running again but with one <strong>huge</strong> problem, the content of my <code>NSOutlineView</code> always appeared in a random order.  Such is the problem with Core Data that <code>NSManagedObject</code>s store their to-many relationships in an <code>NSSet</code>, <strong>not</strong> an NSArray, which is unordered.  So when your <code>NSTreeController</code> tries to display its data it appears in a random order.</p>
<p>This is not nice, imagine if the playlists in your iTunes library always changed their order?  It gets even worse if your user wants to use drag and drop.  In this case <strong>they</strong> decide the order, and they&#8217;d probably want it to stay that way.<br />
<span id="more-49"></span><br />
The question of how to do this comes up so many times, along with the question of how to use multiple classes for nodes in the tree. Well, here&#8217;s your answer, and I&#8217;ve made <a href="http://www.mediafire.com/?wxex0yecjz3">a sample Xcode project</a> to demonstrate. (<a href="http://rapidshare.com/files/120385221/SortedTree.zip.html">alt link</a>)</p>
<p><strong>The Model</strong><br />
<a href="http://jonathandann.files.wordpress.com/2008/05/abstracttreemodel.png"><img src="http://jonathandann.files.wordpress.com/2008/05/abstracttreemodel.png?w=300&h=278" alt="Multiple-entity model that can be used in an NSTreeController" width="300" height="278" class="alignleft size-medium wp-image-50" /></a><br />
The model I&#8217;ve set up has a single abstract entity called <code>TreeNode</code> which has a boolean <code>isLeaf</code> attribute, an <code>NSString *displayName</code>, and an <code>NSNumber *sortIndex</code> (which is the one of the main reasons I&#8217;m writing this). It also has a to-many <code>children</code> relationship that has an inverse to-one <code>parent</code> relationship.  Then there are two other entities: <code>Group</code> and <code>Leaf</code> both of which inherit from <code>TreeNode</code>.  The <code>Group</code> entity has a few other boolean attributes that make writing an <code>NSOutlineView</code> delegate class really simple.  The <code>Leaf</code> doesn&#8217;t have any of its own attributes yet, that&#8217;s for you guys to ponder.</p>
<p><strong>The Tree Controller</strong><br />
The tree controller is our custom <code>ESTreeController</code> class.  It is set up in the nib and has only 2 bindings, those of the <code>@"managedObjectContext"</code> and the <code>@"sortDescriptors"</code>.  It&#8217;s (obviously) operating in entity mode which is set to our abstract entity of <code>TreeNode</code>, and the children and leaf keypaths are set to attributes of the model: the oh-so-inventively named, <code>children</code> and <code>isLeaf</code>.</p>
<p><strong>The Outline View</strong><br />
The outline view is also a subclass of <code>NSOutlineView</code>, but only to support expanded-state saving.  The <code>NSTableColumn</code> in the view has only a <strong>single binding</strong>: the <code>@"value"</code> binding is bound to the tree controller&#8217;s <code>@"arrangedObjects.displayName"</code>.</p>
<p>The rest of the UI only exists for demonstration purposes (so yeah its ugly), so we can see which <code>ESTreeController</code> method is invoked by the class&#8217; standard actions.</p>
<p><strong>The Sort Index</strong><br />
The <code>@"sortIndex"</code> attribute is the key to keeping the tree sorted, and it&#8217;s persistent allowing the sort to be maintained across sessions.  It is simply an unsigned integer.  Not having to apply a unique value to each node in the tree makes this a whole lot easier, nodes only require to have a unique number that defines their location within their own group.  All we have to do is keep the sort index as the last index of the corresponding <code>NSTreeNode</code>&#8217;s index path.</p>
<p>There are 3 places this must be updated: on insertion, removal and movement.  Functionality for this is provided by a single method <code>-updateSortOrderOfModelObjects</code>, which takes the last index of the tree node&#8217;s index path and sets it as the sort index of the representedObject. Simple.  We make sure this is done correctly by overriding:</p>
<p><code>- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath;<br />
- (void)insertObjects:(NSArray *)objects atArrangedObjectIndexPaths:(NSArray *)indexPaths;<br />
- (void)removeObjectAtArrangedObjectIndexPath:(NSIndexPath *)indexPath;<br />
- (void)removeObjectsAtArrangedObjectIndexPaths:(NSArray *)indexPaths;<br />
- (void)moveNode:(NSTreeNode *)node toIndexPath:(NSIndexPath *)indexPath;<br />
- (void)moveNodes:(NSArray *)nodes toIndexPath:(NSIndexPath *)startingIndexPath;</code></p>
<p>The tree controller is then bound to an <code>NSSortDescriptor</code> with keypath <code>@"sortIndex"</code>.</p>
<p><strong>Points To Note</strong><br />
<code>NSTreeController</code>&#8217;s <code>-add:</code>, <code>-addChild:</code>, <code>-insert:</code>, <code>-insertChild:</code> and <code>remove:</code> all call the plural forms of the above methods, and proceed to try and add a <code>TreeNode</code> entity to the tree.  This can cause problems in the outline view&#8217;s delegate as this expects either <code>Leaf</code> or <code>Group</code> entities (try them in the sample project!), overriding these is usually necessary, which is what&#8217;s done with the <code>-newLeaf:</code> and <code>-newGroup:</code> actions.</p>
<p>The project also includes a bunch of categories that make life so much easier when working with <code>NSIndexPath</code>, <code>NSTreeController</code> and <code>NSTreeNode</code>.</p>
<p><strong>Drag and Drop</strong><br />
<a href="http://jonathandann.files.wordpress.com/2008/05/sortedtreedraganddrop1.png"><img src="http://jonathandann.files.wordpress.com/2008/05/sortedtreedraganddrop1.png?w=211&h=183" alt="" width="211" height="183" class="alignleft size-medium wp-image-52" /></a>
</p>
<p><code>-outlineView:writeItems: toPasteboard:</code><br />
Write the index paths of the dragged items to the pasteboard as <code>NSData</code> objects.</p>
<p><code>-outlineView:validateDrop:proposedItem:proposedChildIndex:</code><br />
Determine if the drop location is valid, not allowing drops on a leaf node (for the case of iTunes playlists being allowed to do this wouldn&#8217;t make sense), or if one of the dragged nodes is a group and the proposed location is on of its own descendants. </p>
<p><code>-outlineView:acceptDrop:item:childIndex:</code><br />
Accept the drop and move the nodes for the dragged index paths.  The edge case is is when dropping on the root of the tree.  In this case the proposed parent of the drop location is <code>nil</code> and generating the index path for insertion therefore returns <code>nil</code>.  We check for a <code>nil</code> parent and create a blank <code>NSIndexPath</code> if this is the case.</p>
<p><strong>Summary</strong><br />
With the (relatively small) amount of code we have an <code>NSOutlineView</code> powered by Core Data and <code>NSTreeController</code> with some great features:<br />
1) Drag and drop!<br />
2) Persistent state saving!<br />
3) Multiple entities in the tree (extend <em>ad infinitum</em>)<br />
4) Sorting!<br />
5) Some indispensable categories that make these classes <em>so</em> much easier to use (<code>-treeNodeForObject:</code> is great).</p>
<p><a href="http://www.mediafire.com/?wxex0yecjz3">Download</a> the Xcode project (requires Mac OS X 10.5)</p>
<p><strong>Update</strong><br />
I&#8217;ve realised that there is some repeated code in the extensions, for my own work I&#8217;ve replaced the implementation of <code>-flattenedContent</code> with this:<br />
<code>- (NSArray *)flattenedContent;<br />
{<br />
	return [[self flattenedNodes] valueForKey:@&#8221;representedObject&#8221;];<br />
}</code></p>
<p>There are also these I&#8217;ve found useful:<br />
<code>- (void)setSelectedNode:(NSTreeNode *)node;<br />
{<br />
	[self setSelectionIndexPath:[node indexPath]];<br />
}</p>
<p>- (void)setSelectedObject:(id)object;<br />
{<br />
	[self setSelectedNode:[self treeNodeForObject:object]];<br />
}</p>
<p>- (NSIndexPath *)indexPathToObject:(id)object;<br />
{<br />
	return [[self treeNodeForObject:object] indexPath];<br />
}<br />
</code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/49/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/49/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=49&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/05/13/nstreecontroller-and-core-data-sorted/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>

		<media:content url="http://jonathandann.files.wordpress.com/2008/05/abstracttreemodel.png?w=300" medium="image">
			<media:title type="html">Multiple-entity model that can be used in an NSTreeController</media:title>
		</media:content>

		<media:content url="http://jonathandann.files.wordpress.com/2008/05/sortedtreedraganddrop1.png?w=211" medium="image" />
	</item>
		<item>
		<title>I&#8217;m on the Mac Developer Network!</title>
		<link>http://espresso-served-here.com/2008/04/22/im-on-the-mac-developer-network/</link>
		<comments>http://espresso-served-here.com/2008/04/22/im-on-the-mac-developer-network/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 21:09:10 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=48</guid>
		<description><![CDATA[I recently did a post on NSViewController with Cathy Shive, and I realised today that  the post was link to on the MacDevNet (Thanks Scotty!).  I&#8217;m actually feeling like a developer for once.
Welcome all, nice to see you.  Sit down, have a cup of coffee (espresso is served here), and I&#8217;ll write [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I recently did a <a href="http://katidev.com/blog/2008/04/17/nsviewcontroller-the-new-c-in-mvc-pt-2-of-3/">post</a> on NSViewController with <a href="http://katidev.com/blog/">Cathy Shive</a>, and I realised today that  the post was link to on the <a href="http://www.macdevnet.com/">MacDevNet</a> (Thanks <a href="http://www.mamooba.com/">Scotty</a>!).  I&#8217;m actually feeling like a developer for once.</p>
<p>Welcome all, nice to see you.  Sit down, have a cup of coffee (espresso is served here), and I&#8217;ll write some more in a bit.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/48/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/48/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=48&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/04/22/im-on-the-mac-developer-network/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>Using NSTreeController</title>
		<link>http://espresso-served-here.com/2008/04/06/using-nstreecontroller/</link>
		<comments>http://espresso-served-here.com/2008/04/06/using-nstreecontroller/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 13:32:45 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/?p=46</guid>
		<description><![CDATA[A common UI concept in Mac development is that of the source list, which everybody knows from iTunes, iPhoto, etc. To do this, NSTreeController can come in particular handy, although it can come with a bit of a headache as the API is somewhat lacking and a proper model is essential to making this easy [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A common UI concept in Mac development is that of the source list, which everybody knows from iTunes, iPhoto, etc. To do this, NSTreeController can come in particular handy, although it can come with a bit of a headache as the API is somewhat lacking and a proper model is essential to making this easy to use.  I&#8217;m going to go over the solution that&#8217;s worked for me.<br />
<span id="more-46"></span><br />
The starting point is the design of the model, we&#8217;ll begin by constructing a &#8216;node&#8217; class that will represent a single item in our tree.  A node will need a few properties to work with the tree controller well.  The first is a &#8216;children&#8217; property that&#8217;ll be an NSMutableArray intended to hold a series of our nodes.  The second is a BOOL, &#8216;isLeaf&#8217; that we&#8217;ll toggle depending on whether we want the node to have the possibility of children or not.  Think of leaves as files and the opposite (I&#8217;ll call them groups) as folders, if you like.</p>
<p>Here&#8217;s how the code will look:</p>
<pre name="code" class="cpp">
@interface ESNode : NSObject  {
 @protected
 ESNode *_parent;
 NSString *_nodeName;
 NSMutableArray *_children;
 BOOL _isLeaf;
}
@property(copy) NSString *nodeName;
@property(copy) NSMutableArray *children;
@property(assign) ESNode *parent;
@property(assign) BOOL isLeaf;
</pre>
<p>The problem is that objective-c 2.0 properties don&#8217;t have a mutableCopy keyword, so we&#8217;ll have to add the accessors ourselves, the isLeaf setter will do some magic too.</p>
<pre name="code" class="cpp">

@synthesize nodeName = _nodeName;
@synthesize parent = _parent;
@dynamic isLeaf;
@dynamic children;

- (void)setIsLeaf:(BOOL)flag;
{
 _isLeaf = flag;
 if (_isLeaf)
 	self.children = [NSMutableArray arrayWithObject:self];
 else
 	self.children = [NSMutableArray array];
}
- (BOOL)isLeaf;
{
 return _isLeaf;
}

- (NSMutableArray *)children;
{
 return _children;
}

- (void)setChildren:(NSMutableArray *)newChildren;
{
 if (_children == newChildren)
 	return;
 [_children release];
 _children = [newChildren mutableCopy];
}

- (NSUInteger)countOfChildren;
{
 if (self.isLeaf)
 	return 0;
 return [self.children count];
}

- (void)insertObject:(id)object inChildrenAtIndex:(NSUInteger)index;
{
 if (self.isLeaf)
 	return;
 [self.children insertObject:object atIndex:index];
}

- (void)removeObjectFromChildrenAtIndex:(NSUInteger)index;
{
 if (self.isLeaf)
 	return;
 [self.children removeObjectAtIndex:index];
}

- (id)objectInChildrenAtIndex:(NSUInteger)index;
{
 if (self.isLeaf)
 	return nil;
 return [self.children objectAtIndex:index];
}

- (void)replaceObjectInChildrenAtIndex:(NSUInteger)index withObject:(id)object;
{
 if (self.isLeaf)
 	return;
 [self.children replaceObjectAtIndex:index withObject:object];
}
</pre>
<p>So we have a typical mutable array accessor and all the indexed accessors that allow you to place nodes anywhere in the children array.  If you need to see more about why these are good to put in, then read the documentation on <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html#//apple_ref/doc/uid/20002174">Key-Value Coding</a>.  We&#8217;ve also included a &#8216;parent&#8217; property that is non-retained so all nodes can know about their parent object and a &#8216;nodeName&#8217; which I commonly use to identify my nodes in the tree.  The &#8216;isLeaf&#8217; accessor will create either an empty mutable array ready for some children nodes or an array containing &#8217;self&#8217; if &#8217;self&#8217; has isLeaf set to YES.  The only caveat here is is you adopt the NSCopying or NSCoding protocols then you have to set isLeaf <strong>before</strong> setting the children array.</p>
<p>In the implementation of our ESNode class we&#8217;ll include a few useful methods for searching and filtering the tree:</p>
<pre name="code" class="cpp">

- (NSArray *)descendants;
{
 NSMutableArray *descendantsArray = [NSMutableArray array];

for (ESNode *node in self.children) {
 	[descendantsArray addObject:node];
 	if (!node.isLeaf)
 		[descendantsArray addObjectsFromArray:[node descendants]];
 }

return [[descendantsArray copy] autorelease]; // return immutable
}

- (NSArray *)leafDescendants;
{
 NSMutableArray *leafsArray = [NSMutableArray array];

for (ESNode *node in self.children) {
 	if (node.isLeaf)
 		[leafsArray addObject:node];
 	else
 		[leafsArray addObjectsFromArray:[node leafDescendants]];
 }

return [[leafsArray copy] autorelease]; // return immutable
}

- (NSArray *)groupDescendants;
{
 NSMutableArray *groupsArray = [NSMutableArray array];

for (ESNode *node in self.children) {
 	if (!node.isLeaf) {
 		[groupsArray addObject:node];
 		[groupsArray addObject:[node groupDescendants]];
 	}
 }

return [[groupsArray copy] autorelease]; // return immutable
}
</pre>
<p>These allow us to get any of our node objects and construct an array of their descendants down to the bottom of the tree.  The final methods are our -init, -copyWithZone:, -initWithCoder: and -encodeWithCoder:</p>
<pre name="code" class="cpp">

- (id)init;
{
 if (![super init])
 	return nil;
 self.nodeName = [NSString stringWithString:@&quot;untitled&quot;];
 self.isLeaf = NO; // this will also set the children array to [NSArray array].
 self.parent = nil;
 return self;
}

- (id)initGroup;
{
 return [self init];
}

- (id)initLeaf;
{
 if (![self init])
 	return nil;
 self.isLeaf = YES; // this will set the children array to an NSArray containing self.
 return self;
}

- (id)copyWithZone:(NSZone *)zone;
{
 ESNode *copy = [[[self class] allocWithZone:zone] init];
 if (!copy)
 	return nil;
 copy.nodeName = self.nodeName;
 copy.isLeaf = self.isLeaf;
 if (!self.isLeaf)
 	copy.children = self.children;
 return copy;
}

- (NSArray *)keysForEncoding;
{
 return [NSArray arrayWithObjects:@&quot;isLeaf&quot;,@&quot;children&quot;,@&quot;parent&quot;,nil];
}

-(id)initWithCoder:(NSCoder *)coder;
{
 if (![self init])
 	return nil;

for (NSString *key in self.keysForEncoding) {
 	[self setValue:[coder decodeObjectForKey:key] forKey:key];
 }

return self;
}

- (void)encodeWithCoder:(NSCoder *)coder;
{
 for (NSString *key in self.keysForEncoding)
 	[coder encodeObject:[self valueForKey:key] forKey:key];
}

- (void)setNilValueForKey:(NSString *)key;
{
 if ([key isEqualToString:@&quot;isLeaf&quot;])
 	self.isLeaf = NO;
 else
 	[super setNilValueForKey:key];
}
</pre>
<p>The nice thing about doing your NSCoding in this way is that concrete subclasses of ESNode can simply implement a single method to adopt NSCoding:</p>
<pre name="code" class="cpp">

- (NSArray *)keysForEncoding;
{
 return [[super keysForEncoding] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@&quot;key1&quot;,@&quot;key2&quot;,@&quot;key3&quot;,nil]];
}
</pre>
<p>Ok, so we have our node class and now we can use it with NSTreeController.  In your AppController class (this may be anything, for example an NSDocument subclass) create an NSMutableArray instance variable and call it treeContent. This array will hold instances of our ESNodes (or a subclass of ESNode if you want to extend it).</p>
<p>In the next part I&#8217;m assuming you&#8217;re used to Interface Builder and your app controller is either the File&#8217;s Owner of your nib, or is instantiated in the nib. Create an instance of NSTreeController in Interface Builder and bind the @&#8221;contentArray&#8221; to the keypath to the &#8216;treeContent&#8217; array.  This tree controller will be powering an NSOutlineView.  Click through on your NSOutlineView instance until you can edit the bindings for the NSTableColumn and bind the @&#8221;value&#8221; of the table column in the outline view to the tree controller&#8217;s @&#8221;arrangedObjects&#8221; controller key with the keypath @&#8221;nodeName&#8221;.  You&#8217;ll also have to set the tree controller&#8217;s children keypath to @&#8221;children&#8221; and the leaf keypath to @&#8221;isLeaf&#8221;.</p>
<p>In the -init method of the class that contains your treeContent array, create a bunch of ESNodes and fill the tree, set some children and whatnot and all of this will appear in your NSOutlineView when you run the program.</p>
<p>So once your tree is created, whether by you in the -init method or by your user at runtime, you&#8217;ll most likely want to search through the tree from time to time. In your app controller you can generate a &#8216;flat&#8217; version of the tree with the following method:</p>
<pre name="code" class="cpp">

- (NSArray *)discreteProjectContent;
{
 NSMutableArray *discreteProjectContent = [NSMutableArray array];
 for (ESSourceNode *item in self.projectContent) {
 	[discreteProjectContent addObject:item];
 	if (!item.isLeaf)
 		[discreteProjectContent addObjectsFromArray:[item descendants]];
 }
 return [[discreteProjectContent copy] autorelease]; // return immutable
}
</pre>
<p>Try it out.  You get all the nodes in the tree in one long array, you can then search through the array for the item you want, for example by nodeName, and get a pointer to any item in the tree.</p>
<p>For really great additions to NSTreeController I point you in the direction of  <a href="http://www.wilshipley.com/blog/2006/04/pimp-my-code-part-10-whining-about.html">Wil Shipley&#8217;s blog</a>, which has a great few NSTreeController extensions.  When used in conjunction with the code above, you can get a pointer to an item in the tree and call his -indexPathToObject: method and you can then select any object in the tree.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/46/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/46/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=46&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/04/06/using-nstreecontroller/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Install MCNP4C2 on Mac OS X 10.5.1</title>
		<link>http://espresso-served-here.com/2008/01/13/how-to-install-mcnp4c2-on-mac-os-x-1051/</link>
		<comments>http://espresso-served-here.com/2008/01/13/how-to-install-mcnp4c2-on-mac-os-x-1051/#comments</comments>
		<pubDate>Sun, 13 Jan 2008 14:01:00 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/2008/01/13/how-to-install-mcnp4c2-on-mac-os-x-1051/</guid>
		<description><![CDATA[Well if you&#8217;ve already read my post on installing MCNP on OS 10.4.10 then you mostly know what to do when installing on Leopard!  Its nearly identical but with Apple migrating the X11 system they use to the one from X.org the X-window library that MCNP looks for is now a different name so [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Well if you&#8217;ve already read my post on installing MCNP on OS 10.4.10 then you mostly know what to do when installing on Leopard!  Its nearly identical but with Apple migrating the X11 system they use to the one from X.org the X-window library that MCNP looks for is now a different name so the install fails and tells you it can&#8217;t find X11.</p>
<p>Some things are easier though, as X11 is installed by default you no longer have to install it from the Leopard DVD (unless you&#8217;ve selected not in install it in a custom installation of Leopard, in which case you probably know that you&#8217;ve done it).</p>
<p>Well when you&#8217;re at the stage that you&#8217;ve run the ./install linux mcnp command and have set the system option to Linux then you have to change the X11 path to the defaults except for the library name. This is now <strong>libx11la.lib</strong> then the rest f the install will run smoothly.</p>
<p>The new X11 in Leopard is buggy and the windows take an age to draw bit that&#8217;s going to be fixed soon. It all works fine its just the geometry plotting that is a bit slow, although this can be sped up if you switch to another app with a window that covers the X11 window during drawing (for example, iTunes), then switch back to find the window is completely drawn.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/44/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/44/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=44&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2008/01/13/how-to-install-mcnp4c2-on-mac-os-x-1051/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>How-to: Install MCNP4C2 on Mac OS X 10.4.10 (Intel)</title>
		<link>http://espresso-served-here.com/2007/10/08/how-to-install-mcnp4c2-on-mac-os-x-10410-intel/</link>
		<comments>http://espresso-served-here.com/2007/10/08/how-to-install-mcnp4c2-on-mac-os-x-10410-intel/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 15:09:49 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[How-to]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/2007/10/08/how-to-install-mcnp4c2-on-mac-os-x-10410-intel/</guid>
		<description><![CDATA[MCNP (Monte Carlo N-Particle) is a transport code that is used throughout the physics community to model how particles interact as they travel through a system. The code is quite old, and therefore requires tweaking to get it running on newer systems like Mac OS X 10.4.10. There are programs and compilers it needs to [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>MCNP (Monte Carlo N-Particle) is a transport code that is used throughout the physics community to model how particles interact as they travel through a system. The code is quite old, and therefore requires tweaking to get it running on newer systems like Mac OS X 10.4.10. There are programs and compilers it needs to use when installing that aren&#8217;t included in Mac OS X anymore due to their age, and a few that are on the Mac OS X CD that came with your computer, but aren&#8217;t installed by default. The first things we need are:</p>
<p>A FORTRAN-77 compiler and a &#8216;C&#8217; compiler of the same version.<br />
The &#8216;fsplit&#8217; program.<br />
X11 installed.<br />
The XCode tools.<br />
<span id="more-43"></span><br />
The last is the easiest to install, and may come in useful if you ever do any programming under OS X. The XCode tools are on the Mac OS X install disc 1 that came with your Intel Mac. Open the CD and start the XCode installer (XCodeTools.mpkg) in</p>
<p>/Mac OS X Install Disc 1/XCode Tools/</p>
<p>During the installation, you get the option to customise the install, just select everything. I know some which are needed by MCNP, like the X11 SDK (software development kit), the GCC (GNU compiler collection) compilers the 10.4 SDK; but only selecting some messes things up and you never know when you&#8217;ll need them.</p>
<p>Once these are installed, you have to find the X11 installer (you&#8217;ve just installed the deveopment kit, but not the X11 application itself). X11 is the window server that allows you to display the geometry you define in MCNP. Double-click the X11User.pkg package on the Mac install CD at the path:</p>
<p>/Mac OS X Install Disc 1/System/Installation/Packages.</p>
<p>Run the installer and your done with the CD.</p>
<p>Now you need to install the FORTRAN-77 compiler, G77 version 3.4.0, and the C compiler CC 3.4.0. Both of these are done in the next step, are free, and are part of the GNU Comppiler Collection (GCC). This step is taken from<a href="http://www1.gly.bris.ac.uk/~wookey/MEFTLG/g77.htm"></a></p>
<p><a href="http://www1.gly.bris.ac.uk/~wookey/MEFTLG/g77.htm">http://www1.gly.bris.ac.uk/~wookey/MEFTLG/g77.htm</a></p>
<p><a href="http://www1.gly.bris.ac.uk/~wookey/MEFTLG/g77.htm"></a><br />
Download the compilers&#8217; source code to your desktop by clicking the link below, then double-click the downloded file to unpack the code to a folder on your desktop.</p>
<p><a href="ftp://ftp.gnu.org/gnu/gcc/gcc-3.4.0/gcc-3.4.0.tar.gz">ftp.gnu.org/gnu/gcc/gcc-3.4.0/gcc-3.4.0.tar.gz</a></p>
<p>Then download the patch file to your desktop (which allows this old compiler set to run on an Intel Mac) by right clikcing this link and saving. This may add &#8216;.txt&#8217; to the file, if it does, right click and select &#8216;Get Info&#8217;. Then remove the .txt suffix.<a href="http://www1.gly.bris.ac.uk/~wookey/MEFTLG/gcc-3.4.0.diffs"></a></p>
<p><a href="http://www1.gly.bris.ac.uk/~wookey/MEFTLG/gcc-3.4.0.diffs">http://www1.gly.bris.ac.uk/~wookey/MEFTLG/gcc-3.4.0.diffs</a></p>
<p>Now you need to make your account an administrator account if it wasn&#8217;t one already, then open a Terminal window (found in /Applications/Utilities/) and type the following commands, hitting return at the end of each line:<code></code></p>
<p><code>cd Desktop<br />
</code><code>patch -p0 &lt; ./gcc.3.4.0.diffs<br />
cd gcc-3.4.0</code><code><br />
./configure &#8211;enable-altivec &#8211;enable-languages=c,f77<br />
sudo make bootstrap<br />
sudo make install</code></p>
<p>(It seems that wordpress is removing two hyphens in an attempt to be smart. Both the &#8216;enable&#8217; directives should be prepended by two hyphens, not a single one.  The single hyphen between &#8216;enable&#8217; and &#8216;langauges&#8217; should be single, though.)</p>
<p>The &#8217;sudo make bootstrap&#8217; command will ask for your password and take forever, even on my new 2.2GHz Core 2 Duo MacBook Pro! The &#8217;sudo install&#8217; command may tell you nothing else was required, so thats cool.This will create a few new programs in you /usr/local/bin folder, these are the compilers. One this is done you can delete the gcc-3.4.0 folder from your desktop. Then open up a terminal window, we are going to add the g77 (FORTRAN) and gcc-3.4.0 compilers to the standard folder where all things like this are kept. This allows you to invoke them from the terminal without typing the folder path each time too. We add them to this magical folder (/usr/bin) by creating a symbolic link (an alias in Mac language or shortcut if you speak Windows). In the terminal, type:</p>
<p><code>sudo ln -s /usr/local/bin/g77 /usr/bin/g77<br />
sudo ln-s /usr/local/bin/gcc /usr/bin/gcc-3.4.0</code></p>
<p>Done! This leaves the 3.4.0 versions of the compilers in your /usr/local/bin folder so the XCode-installed new shiny versions don&#8217;t get messed with.Now we need to install &#8216;fsplit&#8217; which is needed during the MCNP install when it splits up its monolithic FORTRAN source into smaller files, as I said MCNP is old. Download the fsplit source to your desktop:<a href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/fsplit/fsplit.c"></a></p>
<p><a href="http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/fsplit/fsplit.c">http://ftp.bg.openbsd.org/OpenBSD/src/usr.bin/fsplit/fsplit.c</a></p>
<p>The open a Terminal and type</p>
<p><code>cd Desktop<br />
cc -o fsplit fsplit.c</code></p>
<p>This compiles the program and places it on your desktop. Then type:</p>
<p><code>sudo mv ./fsplit /usr/bin</code></p>
<p><code></code>which moves the compiled program to the magical folder which the terminal likes all programs to be in. Then delete the fsplit.c file.</p>
<p>Now have a coffee, then we&#8217;re ready to install MCNP (finally). Put the CD in and open up a terminal window. When writing a folder path you can press tab, and the path will attempt to complete itself. If it can&#8217;t, for example when there are 2 folders beginning with the letter &#8216;c&#8217;, then press tab again and a list of all the possible matches will come up.</p>
<p>You&#8217;ll use this now if the MCNP CD isn&#8217;t called what I type below, mine was just called MCNP, but yours may not be. Whatever though, it will be the name that appears on the desktop.</p>
<p><code>mkdir /Applications/mcnp<br />
cd /Applications/mcnp<br />
gunzip -c /Volumes/MCNP/C701TAR.GZ | tar xvf -<br />
gunzip -c /Volumes/MCNP/D200TAR.GZ | tar xvf -</code></p>
<p>This creates two folders in you /Applications/mcnp folder called &#8216;mcnp&#8217; and &#8216;mcnp4c2&#8242; the former containes cross-section data and the latter is the source code for MCNP itself. You have to place the cross sections is another folder, in terminal again:<code></code></p>
<p><code>cd /Applications/mcnp/mcnp/xs<br />
sudo mkdir /usr/mcnpxs<br />
mv ./* /usr/mcnpxs</code></p>
<p>Done. Now delete the /Applications/mcnp/mcnp folder. Leave the mcnp4c2 folder though, we need that now.</p>
<p>In Finder, go to the mcnp4c2 folder we just created and double click the install.fix file. This can be opened in TextEdit, where we just need to uncomment a few lines.Scroll down to the line that says FIXES FOR SYSTEM 5 (PC LINUX), below it is asks you to &#8216;uncomment the two lines for a debuggable version&#8217; (some of you may need this when running MCNP), all this means is delete the &#8216;c&#8217; charachter from the two lines below the statment, so they look like this:<code></code></p>
<p><code>5 2 4 1 1<br />
DEBUG=&#8221;-g&#8221;</code></p>
<p>Save and close the file. Now you need to open up the mcsetup.id file in the mcnp4c2 folder. This opens in TextEdit again, so scroll down to the line that says &#8216;COM file to make MCNP 4C2 on the PC Linux&#8217;. The section of code below this line are all the commands used to compile MCNP, its automated later on, but these need to change a bit so they use the right compilers which we installed earlier. A few lines down you see</p>
<p><code>7 'mv compile mcnpc.c','cc -c mcnpc.c',......</code></p>
<p>Change the &#8216;cc&#8217; part to read &#8216;gcc-3.4.0&#8242; (without the quotation marks). A few lines down you see &#8216;./fsplit&#8217; which you simply change to &#8216;fsplit&#8217;. This is ready to go now. You told it to use gcc-3.4.0, which is the same version as the g77 comiler we installed, this was very important.Open up a terminal window and type:<code></code></p>
<p><code>cd /Applications/mcnp/mcnp4c2<br />
chmod a+x ./install<br />
./install linux mncp</code></p>
<p>This then starts the install program, you&#8217;re greeted with a few options, you can change a few, but not all of them work with the Mac, and will tell you they haven&#8217;t been tested on linux (which were pretending to be, shhh don&#8217;t tell anyone).</p>
<p>In this mode you type 1.1, then press 5 to change to a linux setup, then type 2.3, press 2, then enter the amount of RAM you have according to the formula:</p>
<p><img src='http://l.wordpress.com/latex.php?latex=%5Cfrac%7B%28RAM-2%29%7D%7B4%7D%5Ctimes+10%5E6&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='\frac{(RAM-2)}{4}\times 10^6' title='\frac{(RAM-2)}{4}\times 10^6' class='latex' /></p>
<p>Where the value &#8216;RAM&#8217; is in MB. This will say there&#8217;s an error with the value you typed (whoa THAT much RAM doesn&#8217;t exist!), again, MCNP = OLD. Just press C to continue.</p>
<p>Then back in the main MCNP install screen, press P to start installation. Should only take a few mins at most, not 20-40 like it says!</p>
<p>Once it&#8217;s done, you can delete most of the source. Open a terminal and type:<code></code></p>
<p><code>cd /Applications/mcnp<br />
mv mcnp4c2/mcnp ./<br />
mv mcnp4c2/xs ./<br />
rm -r mcnp4c2<br />
sudo ln -s /Applications/mcnp/mcnp /usr/bin/mcnp</code></p>
<p>Done! MCNP is now installed, and all you need to do is open a terminal and type &#8216;mcnp&#8217; to run it! (It&#8217;ll ask for a input file, but if you&#8217;ve done all this then you&#8217;d probably know that!)</p>
<p>Ask questions, I&#8217;ll do my best!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=43&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2007/10/08/how-to-install-mcnp4c2-on-mac-os-x-10410-intel/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
		<item>
		<title>How-to series</title>
		<link>http://espresso-served-here.com/2007/10/08/how-to-series/</link>
		<comments>http://espresso-served-here.com/2007/10/08/how-to-series/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 13:44:30 +0000</pubDate>
		<dc:creator>Jonathan Dann</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Computing]]></category>

		<category><![CDATA[How-to]]></category>

		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://jonathandann.wordpress.com/2007/10/08/how-to-series/</guid>
		<description><![CDATA[The next few posts I think will be a little more obscure than the others.  As a scientist, I frequently have to work out how to install random science-y programs on my computer, and how to do the odd geeky thing.
 Occasionally I get asked how to do these things, and the things I need to [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The next few posts I think will be a little more obscure than the others.  As a scientist, I frequently have to work out how to install random science-y programs on my computer, and how to do the odd geeky thing.</p>
<p> Occasionally I get asked how to do these things, and the things I need to do are frequently undocumented, or badly documented at best.  I hope someof you find these things useful.  If not, at least they&#8217;ll exist for when someone asks me how.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonathandann.wordpress.com/42/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonathandann.wordpress.com/42/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonathandann.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonathandann.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonathandann.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonathandann.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonathandann.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonathandann.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonathandann.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonathandann.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonathandann.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonathandann.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=espresso-served-here.com&blog=656654&post=42&subd=jonathandann&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://espresso-served-here.com/2007/10/08/how-to-series/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/jpdann-128.jpg" medium="image">
			<media:title type="html">jpdann</media:title>
		</media:content>
	</item>
	</channel>
</rss>