<?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:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Apparent Software blog &#187; Programming</title>
	<atom:link href="http://blog.apparentsoft.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.apparentsoft.com</link>
	<description>News and articles from Apparent Software</description>
	<lastBuildDate>Thu, 15 Dec 2011 14:30:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to open document in &#8220;Untitled&#8221; window instead of new one</title>
		<link>http://blog.apparentsoft.com/programming/169/how-to-open-document-in-untitled-window-instead-of-new-one/</link>
		<comments>http://blog.apparentsoft.com/programming/169/how-to-open-document-in-untitled-window-instead-of-new-one/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 15:23:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[NSDocument]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://blog.apparentsoft.com/?p=169</guid>
		<description><![CDATA[Have you noticed how TextEdit or TextMate will open an existing file in the &#8220;Untitled&#8221; window, if it is empty? I thought this was a default behaviour of the multi-document architecture of AppKit. While writing multi-document support for ImageFramer 3 (ImageFramer 2 is a single-document application) I noticed that it always opens a document in [...]]]></description>
			<content:encoded><![CDATA[<p>Have you noticed how TextEdit or TextMate will open an existing file in the &#8220;Untitled&#8221; window, if it is empty? I thought this was a default behaviour of the multi-document architecture of AppKit. While writing multi-document support for ImageFramer 3 (<a href="http://www.apparentsoft.com/imageframer">ImageFramer</a> 2 is a single-document application) I noticed that it always opens a document in a new window, even if I have not touched the default empty document.</p>

<p>After some research I&#8217;ve found the following facts:</p>

<ul>
<li><code>NSDocumentController</code> always opens documents in a new window by default</li>
<li>TextEdit uses a lot of custom code to manage this behavior. TextEdit is an open sourced examples which is installed with Xcode, at <code>/Developer/Examples/TextEdit/</code>. I didn&#8217;t want such a complicated solution for myself.</li>
</ul>

<p>I ended up implementing my own solution for my case. It might not suit your needs but could help choose the correct path for you.</p>

<p>My solution maybe unique to me since ImageFramer&#8217;s document are images and not some custom format. In ImageFramer version 3 I&#8217;ll be saving the framing designs in Core Data inside the application, so they&#8217;re not regular documents by default.</p>

<p>I have a method in my NSDocument subclass that loads the image into the already existing document. So the image is not read in <code>- (BOOL)readFromURL:ofType:error:</code> but rather at a later stage, in  <code>- (void)windowControllerDidLoadNib: (NSWindowController *)aController</code> method. There I check <code>[self fileURL]</code>. If it&#8217;s not <code>nil</code>, then I import the image into the document. If it&#8217;s <code>nil</code>, I load the default one and it becomes the &#8220;Untitled&#8221; document.</p>

<p>So what did I do to open images in the same window instead of the default one? I had to make changes in 2 classes:</p>

<p>1. In my NSDocument subclass I setup a method isDefaultImage which does what its name implies. I then set it to <code>YES</code> if I load the default image file. In general case you could name it <br/><code>- (BOOL)isReplaceableDocument</code> and handle all logic there. For example, you might want to return NO here if user has modified the document and you don&#8217;t want him to loose his changes.</p>

<p>2. In <code>NSDocumentController</code> subclass, I overloaded <code>openDocumentWithContentsOfURL:display:error:</code>. Here&#8217;s the whole method:</p>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p169code2'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1692"><td class="code" id="p169code2"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>openDocumentWithContentsOfURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>absoluteURL display<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>displayDocument error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/"><span style="color: #400080;">NSError</span></a> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>outError
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self currentDocument<span style="color: #002200;">&#93;</span> isDefaultImage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super openDocumentWithContentsOfURL<span style="color: #002200;">:</span>absoluteURL display<span style="color: #002200;">:</span>displayDocument error<span style="color: #002200;">:</span>outError<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self currentDocument<span style="color: #002200;">&#93;</span> importImage<span style="color: #002200;">:</span>absoluteURL<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self currentDocument<span style="color: #002200;">&#93;</span> setFileURL<span style="color: #002200;">:</span>absoluteURL<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self currentDocument<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


<p>So, if I can&#8217;t replace to document, I just call <code>super</code> and return its result. If I can replace, I ask the current document to import the image, replacing my placeholder image. Then I set document&#8217;s URL to the provided one, so that it won&#8217;t stay as Untitled and will place the icon on the titlebar of the window.</p>

<p>That&#8217;s all.</p>

<p>Granted, in TextEdit example they take a more complex path, which probably may be better for them. From what I gathered, they create a new document manually and then copy the window controller from the Untitled document (they call it <em>transient</em>) to the new document. There&#8217;s more code there and you probably should take a look there to see if it suits you better.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apparentsoft.com/programming/169/how-to-open-document-in-untitled-window-instead-of-new-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DMG for distribution incompatibilty on Snow Leopard?</title>
		<link>http://blog.apparentsoft.com/programming/112/dmg-for-distribution-incompatibilty-on-snow-leopard/</link>
		<comments>http://blog.apparentsoft.com/programming/112/dmg-for-distribution-incompatibilty-on-snow-leopard/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 13:07:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[DMG]]></category>
		<category><![CDATA[Leopard]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://blog.apparentsoft.com/?p=112</guid>
		<description><![CDATA[Do you use DMG to distribute your software for download? Do you create it using a script, by creating a template DMG and then replacing its content upon release? If you do, which I fully endorse, and you&#8217;ve created your template in Finder on Snow Leopard, read below. In the last couple of days before [...]]]></description>
			<content:encoded><![CDATA[<p>Do you use DMG to distribute your software for download?<br />
 Do you create it using a script, by creating a template DMG and then replacing its content upon release?</p>

<p>If you do, which I fully endorse, and you&#8217;ve created your template in Finder on Snow Leopard, read below.</p>

<p>In the last couple of days before the ininitial release of <a title="Cashflow personal finance for mac" href="http://www.apparentsoft.com/cashculator" target="_blank">Cashculator</a> to the public I&#8217;ve been struggling with the DMG creating process. I thought I&#8217;d already mastered it. After all, I&#8217;ve been already doing it with ImageFramer. So I prepared my template, copied the .DS_Store file from it (I used this process with ImageFramer) and script handled it all fine for me. I look at the final opened DMG and I see the following image (which is what I indended it to be):</p>

<p><br class="spacer_" /></p>

<div id="attachment_114" class="wp-caption aligncenter" style="width: 675px"><img class="size-full wp-image-114" title="Cashculator DMG background" src="http://blog.apparentsoft.com/wp-content/uploads/2009/11/Cashculator.png" alt="Cashculator DMG background" width="665" height="589" /><p class="wp-caption-text">Cashculator DMG background</p></div>

<p>I send it to the server and let my partner Kosta check it on his machine. He&#8217;s using Leopard and not the Snowy kind. He send me back the following image, which is far from what I thought it to be:</p>

<p><br class="spacer_" /></p>

<div id="attachment_115" class="wp-caption aligncenter" style="width: 627px"><img class="size-full wp-image-115" title="Cashculator DMG on Leopard" src="http://blog.apparentsoft.com/wp-content/uploads/2009/11/Cashculator_leopard.jpg" alt="Cashculator DMG on Leopard" width="617" height="409" /><p class="wp-caption-text">Cashculator DMG on Leopard</p></div>

<p>Not good. So I tried this way and that way. I even moved to another system, where I first create the DMG and use  the DMG itself as the template, without exracting .DS_Store first. Nothing helped on Leopard.</p>

<p>So we met and he brought the Leopard machine with him. I open the DMG, press Cmd-J and see that Finder thinks there&#8217;s no backgrond image and icon sizes are different.</p>

<p>After some research I came to the conclusion that DMGs created on Snow Leopard don&#8217;t show the same at all on Leopard. Frightened, I also tested my ImageFramer releases, which also sport a new background since the release of Snow Leopard. To my shock, it was also totally wrong on Leopard. That&#8217;s not how I wanted to convey the first impression of ImageFramer to potential customers.</p>

<p>The solution, of course, was to create the DMG on Leopard and use it as template instead. I ran the new template through my scripts and reopened the final DMG on Leopard to check. It was fine. Finally.</p>

<p>Today I did the same with <a title="Framing software for Mac" href="http://www.apparentsoft.com/imageframer">ImageFramer</a>&#8216;s DMG. For some reason, the one from Leopard showed without background on my Snow Leopard machine. I only added the background again and saved the DMG. It worked fine on Leopard too.</p>

<p>That&#8217;s it. So, if you use a similar technique for creating DMG, check them on Leopard and on Snow Leopard before shipping to avoid later embarrassment.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apparentsoft.com/programming/112/dmg-for-distribution-incompatibilty-on-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

