<?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>Cocoa Kid&#039;s Net</title>
	<atom:link href="http://cocoakids.net/feed" rel="self" type="application/rss+xml" />
	<link>http://cocoakids.net</link>
	<description>Sweet bits of cocoa programming</description>
	<lastBuildDate>Tue, 09 Mar 2010 10:49:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Recipe #3: Adding your app to Login Items using LSSharedFileList</title>
		<link>http://cocoakids.net/37-recipe-3-adding-your-app-to-login-items-using-lssharedfilelist</link>
		<comments>http://cocoakids.net/37-recipe-3-adding-your-app-to-login-items-using-lssharedfilelist#comments</comments>
		<pubDate>Tue, 09 Mar 2010 08:26:18 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[recipes]]></category>
		<category><![CDATA[lssharedfilelist login-items]]></category>
		<category><![CDATA[recipe]]></category>

		<guid isPermaLink="false">http://cocoakids.net/37-recipe-3-adding-your-app-to-login-items-using-lssharedfilelist</guid>
		<description><![CDATA[I&#8217;ve been developing an utility app. I&#8217;m really excited about this and I do hope to release it quite soon (still have some issues with choctop and sparkle stuff). I even got an icon for it (thanks, Scarlet Bits!). In this blog post I&#8217;d like to share about a solution to a problem that I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been developing an utility app. I&#8217;m really excited about this and I do hope to release it quite soon (still have some issues with choctop and sparkle stuff). I even got an icon for it (thanks, <a href="http://scarletbits.com/" title="Scarlet Bits">Scarlet Bits</a>!). In this blog post I&#8217;d like to share about a solution to a problem that I faced while developing my app &#8211; launching it at the beginning of user&#8217;s session, i.e. on login. </p>
<p>Since Mac OS X Leopard (10.5) apple has dedicated API for these kind of things. And by writing &#8220;kind of&#8221; I&#8217;m going to quote the Apple developer documentation itself:</p>
<blockquote><p>The Shared File List API is new to Launch Services in Mac OS X Leopard. This API provides access to several kinds of system-global and per-user persistent lists of file system objects, such as recent documents and applications, favorites, and <strong>login items</strong>. For details, see the new interface file LSSharedFileList.h.</p></blockquote>
<p> Read on if you&#8217;d like to know how to add your app to login items list.</p>
<h2>Getting login items list</h2>
<p>First off &#8211; we&#8217;ll fetch login items to see if our app is already in that list. Well, at first it won&#8217;t be there, but it will pay off in the future, trust me. So here&#8217;s the code snippet for fetching existing login items and assigning them to an NSArray:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Some seed data</span>
UInt32 seedValue;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Let's create reference to shared file list</span>
LSSharedFileListRef loginItems <span style="color: #002200;">=</span> LSSharedFileListCreate<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span>, kLSSharedFileListSessionLoginItems, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Then just pop values from referenced list into array</span>
<span style="color: #400080;">NSArray</span>  <span style="color: #002200;">*</span>loginItemsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>LSSharedFileListCopySnapshot<span style="color: #002200;">&#40;</span>loginItems, <span style="color: #002200;">&amp;</span>seedValue<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>You can now use this array as any table&#8217;s data source to see what this list contains. If you&#8217;re too lazy, then I can tell you that each item has &#8220;assigned display name, icon, and url as well as other optional properties&#8221; (quote from source code).</p>
<p>Now that we&#8217;ve got the list of login items &#8211; it&#8217;s really easy to add/remove our app to/from the list of login items. But for starters &#8211; we&#8217;re going to check for item&#8217;s presence in the list.</p>
<h2>Checking for item&#8217;s existence</h2>
<p>Since it is my first experience with login items and LSSharedFileList, from what I found on the net, I believe that main criteria is to check item&#8217;s url against given one. This code snippet shows how it should be done:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>loginItemExistsWithLoginItemReference<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>LSSharedFileListRef<span style="color: #002200;">&#41;</span>theLoginItemsRefs ForPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span>thePath <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">BOOL</span> exists <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;  
  UInt32 seedValue;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)</span>
  <span style="color: #11740a; font-style: italic;">// and pop it in an array so we can iterate through it to find our item.</span>
  <span style="color: #400080;">NSArray</span>  <span style="color: #002200;">*</span>loginItemsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>LSSharedFileListCopySnapshot<span style="color: #002200;">&#40;</span>theLoginItemsRefs, <span style="color: #002200;">&amp;</span>seedValue<span style="color: #002200;">&#41;</span>;  
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> item <span style="color: #a61390;">in</span> loginItemsArray<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>    
    LSSharedFileListItemRef itemRef <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>LSSharedFileListItemRef<span style="color: #002200;">&#41;</span>item;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>LSSharedFileListItemResolve<span style="color: #002200;">&#40;</span>itemRef, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>thePath, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> noErr<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>thePath path<span style="color: #002200;">&#93;</span> hasPrefix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/Applications/MyApp.app&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        exists <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> exists;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<h2>Adding an item to login items list</h2>
<p>Method for adding an item to login items list is <code>LSSharedFileListInsertItemURL</code>. &#8211; method for inserting. This method accepts 7 parameters, but only 3 of them will be of interest to us. These are the accepted parameters: </p>
<ol>
<li>Reference to login item list (created in the code above. Do not mix up with array of items)</li>
<li>Position of where to insert new login item. To insert item to the beginning of the list use <code>kLSSharedFileListItemBeforeFirst</code>. To insert item to the ending of the list use <code>kLSSharedFileListItemLast</code>. Otherwise pass <code>LSSharedFileListItemRef</code>.</li>
<li>Display name of new login item. Can be NULL. Defaults to app name.</li>
<li>Icon of the login item. Can be NULL. Defaults to app icon.</li>
<li><code>CFURLRef</code> of item to insert. This usually is a path to your app (like /Applications/MyApp.app)</li>
<li><code>CFDictionary</code> &#8211; dictionary of options for new login item.</li>
<li><code>CFDictionary</code> &#8211; dictionary of options to clear if item already exists in the list.</li>
</ol>
<p>The ones interesting for us are 1, 2 and 5. So the code for adding an item to login items list could be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Reference to shared file list</span>
LSSharedFileListRef theLoginItemsRefs <span style="color: #002200;">=</span> LSSharedFileListCreate<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span>, kLSSharedFileListSessionLoginItems, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// CFURLRef to the insertable item.</span>
CFURLRef url <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/Applications/MyApp.app&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Actual insertion of an item.</span>
LSSharedFileListItemRef item <span style="color: #002200;">=</span> LSSharedFileListInsertItemURL<span style="color: #002200;">&#40;</span>theLoginItemsRefs, kLSSharedFileListItemLast, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">NULL</span>, thePath, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Clean up in case of success</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>item<span style="color: #002200;">&#41;</span> 
  CFRelease<span style="color: #002200;">&#40;</span>item<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h2>Removing an item from login items list</h2>
<p>Method for removal of item in question is <code>LSSharedFileListItemRemove</code>. This method contrary to the one for adding the item to login items list accepts only two parameters:</p>
<ol>
<li>The reference to login items list</li>
<li>The item reference in question</li>
</ol>
<p>It could not get simpler than this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">LSSharedFileListItemRemove<span style="color: #002200;">&#40;</span>theLoginItemsRefs, itemRef<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h2>Conclusion</h2>
<p>This is the first part of managing login items in Mac OS X. In next part I&#8217;ll show you how to keep track of changes while your app is not running and adding/removing your app to/from login items isn&#8217;t handled by your app.</p>
<p>Please note that I found code samples in one place on the internet but was too lazy to bookmark it, hence this code was extracted from the app. If you recognize your stuff &#8211; please post a note/link in the comments. Thanks a heap for reading this!</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/37-recipe-3-adding-your-app-to-login-items-using-lssharedfilelist/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recipe #2: Initializing user preferences using NSUserDefaults</title>
		<link>http://cocoakids.net/36-recipe-2-initializing-user-preferences-using-nsuserdefaults</link>
		<comments>http://cocoakids.net/36-recipe-2-initializing-user-preferences-using-nsuserdefaults#comments</comments>
		<pubDate>Sat, 16 Jan 2010 15:04:23 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[recipes]]></category>
		<category><![CDATA[nsuserdefaults]]></category>
		<category><![CDATA[preferences]]></category>

		<guid isPermaLink="false">http://cocoakids.net/36-recipe-2-initializing-user-preferences-using-nsuserdefaults</guid>
		<description><![CDATA[Recently I was in need of some user preferences. I read a guide on user defaults programming. Everything seemed easy and clear, but I still couldn&#8217;t find the thing that interested me most &#8211; initializing user preferences. By &#8220;initialization&#8221; I mean set defaults once so they can be overridden by user and not set again. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was in need of some user preferences. I read a guide on user defaults programming. Everything seemed easy and clear, but I still couldn&#8217;t find the thing that interested me most &#8211; initializing user preferences. By &#8220;initialization&#8221; I mean set defaults once so they can be overridden by user and not set again. For some reason I thought that I&#8217;d have to check each preferences key for its existence and set it accordingly. </p>
<p>Then the light bulb lit above my spiky head. I present you with the solution:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Place this code into your apps delegate class</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize <span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">*</span>defaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// initialize the dictionary with default values</span>
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>appDefaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fooValue&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fooKey&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;barValue&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;barKey&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// and set them appropriately</span>
  <span style="color: #002200;">&#91;</span>defaults registerDefaults<span style="color: #002200;">:</span>appDefaults<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now for refactored and more comfortable version you should use a separate plist for initializing user defaults. I believe it&#8217;s easier to add a row to plist file than add key/value pair into dictionary:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// get the filename for &quot;default defaults&quot;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>defaultsFilename <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Defaults&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// initialize a dictionary with contents of it</span>
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>defaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span>defaultsFilename<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// register the stuff</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> registerDefaults<span style="color: #002200;">:</span>defaults<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The advantage of this approach is that even after updates to your app even unset defaults will be initialized (in case of newly added preferences).</p>
<p>That&#8217;s it! Kudos goes to <a href="http://blog.laurent.etiemble.com/" title="Laurent Etiemble">Laurent Etiemble</a> and <a href="http://www.koolistov.net/" title="Johan Kool">Johan Kool</a> for their answers to <a href="http://stackoverflow.com/questions/2076816/how-to-register-user-defaults-using-nsuserdefaults-without-overwriting-existing-v" title="How to register user defaults using NSUserDefaults without overwriting existing values?">my question</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/36-recipe-2-initializing-user-preferences-using-nsuserdefaults/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XCode file template for custom NSWindow subclass</title>
		<link>http://cocoakids.net/33-xcode-file-template-for-custom-nswindow-subclass</link>
		<comments>http://cocoakids.net/33-xcode-file-template-for-custom-nswindow-subclass#comments</comments>
		<pubDate>Mon, 14 Dec 2009 06:02:06 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[content-view]]></category>
		<category><![CDATA[custom-class]]></category>
		<category><![CDATA[customization]]></category>
		<category><![CDATA[NSView]]></category>
		<category><![CDATA[NSWindow]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=33</guid>
		<description><![CDATA[This has been for a while now in my drafts and I wanted to polish it so it wouldn&#8217;t require as much editing as some previous tutorials. I hope you&#8217;ll enjoy reading this one as much as I enjoyed writing this. I present you &#8211; custom NSWindow template tutorial.
From time to time while programming you&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>This has been for a while now in my drafts and I wanted to polish it so it wouldn&#8217;t require as much editing as some previous tutorials. I hope you&#8217;ll enjoy reading this one as much as I enjoyed writing this. I present you &#8211; custom NSWindow template tutorial.</p>
<p>From time to time while programming you&#8217;ll have to draw custom window. If it&#8217;s more often than &#8220;from time to time&#8221; you&#8217;ll find yourself writing the same code again and again. Of course it&#8217;s easy to extract repetitive code into separate class and include that class in every project (or few times into the same project), but this still not the prettiest solution. Im suggesting you an approach that exploits XCode&#8217;s customization &#8211; template for custom class that you&#8217;ll be able to include into your projects anytime you need it. In this case &#8211; it&#8217;s <strong>custom NSWindow subclass</strong>.</p>
<p>So here&#8217;s how we&#8217;re going to achieve this:
<ol>
<li>Create project in XCode where we will code a template for custom NSWindow. If you are really good with Objective C &#8211; you can code our custom class in any text editor (TextEdit, TextMate) or in an IDE of your choice.</li>
<li>Create custom NSWindow class</li>
<li>Replace hardcoded constants (class names) with identifiers</li>
<li>Move created class files to a location where XCode can consider them as being custom template files</li>
</ol>
<p><span id="more-33"></span></p>
<h2>Creating XCode project</h2>
<p>This part should be really clear. Create simple cocoa application project. Let&#8217;s name it &#8220;CustomWindowTemplate&#8221;.</p>
<p><img src="/wp-content/uploads/2009/12/newXcodeProject.jpg" alt="New XCode project" /></p>
<h2>Custom NSWindow subclass</h2>
<p>Now we need to create <em>the meat</em> of our custom NSWindow class that will serve as template for future implementations. When I was googling around for custom NSWindow drawing I found this very useful and concise tutorial on how to easily draw custom NSWindow. I present you <a href="http://cocoawithlove.com/" title="Cocoa with Love">Matt Gallagher&#8217;s Cocoa With Love</a> post &#8220;<a href="http://cocoawithlove.com/2008/12/drawing-custom-window-on-mac-os-x.html" title="Drawing a custom window on Mac OS X">Drawing a custom window on Mac OS X</a>&#8220;. I&#8217;d suggest to go through all of that tutorial for deeper understanding of how window drawing is done in Cocoa.</p>
<p>Once you&#8217;re done with the custom NSWindow class (you can download the prepared XCode project from the same tutorial page), you need to replace hardcoded class names with XCode identifiers, so that upon new class creation they would be replaced with your provided name.</p>
<h2>Custom XCode identifiers</h2>
<p>The identifier we are interested in most is <code>«FILEBASENAMEIDENTIFIER»</code>. It plays the most important role as it&#8217;s the placeholder who&#8217;s value will be used for our newly created custom NSWindow class. Here&#8217;s the list of other interesting identifiers you may have a use for (like constructing the comment above the declaration of the class).</p>
<ul>
<li><code>«FILENAME»</code> &#8211; the actual file name that will be created (will be substituted for both .h and .m files);</li>
<li><code>«PROJECTNAME»</code> &#8211; the name of the project you&#8217;re creating new custom window class;</li>
<li><code>«YEAR»</code> &#8211; pretty much self explanatory;</li>
<li><code>«ORGANIZATIONNAME»</code> &#8211; self explanatory as well. You can change that via <code>defaults</code>;</li>
</ul>
<p>If you&#8217;re interested, the default one looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  «FILENAME»</span>
<span style="color: #11740a; font-style: italic;">//  «PROJECTNAME»</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by «FULLUSERNAME» on «DATE».</span>
<span style="color: #11740a; font-style: italic;">//  Copyright «YEAR» «ORGANIZATIONNAME». All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span></pre></div></div>

<p>So the declaration of custom NSWindow class in our header template should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
<span style="color: #a61390;">@interface</span> «FILEBASENAMEASIDENTIFIER» <span style="color: #002200;">:</span> <span style="color: #400080;">NSWindow</span> <span style="color: #002200;">&#123;</span>
...</pre></div></div>

<p>Hence the declaration of the class in implementation template should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
«OPTIONALHEADERIMPORTLINE»
&nbsp;
<span style="color: #a61390;">@implementation</span> «FILEBASENAMEASIDENTIFIER»
...</pre></div></div>

<p>The <code>«OPTIONALHEADERIMPORTLINE»</code> identifier I haven&#8217;t mentioned is the one replaced with <code>#import</code> statement of custom class&#8217;s header file.</p>
<p>Now for almost-the-last step we need to create template&#8217;s meta-data file which is practically a simple plist with a few properties. Or you can just copy paste the following XML markup code:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MainTemplateFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>class.m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CounterpartTemplateFile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>class.h<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Description<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A custom NSWindow subclass.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>All tags and values should be self explanatory: we define two template files (one is the counterpart of the other, like .m and .h) and add a description that will be shown in XCodes &#8220;New File&#8230;&#8221; dialog window.</p>
<h2>Final destination</h2>
<p>Now open up <strong>finder</strong> and go (<strong>⌘ + ⇧ + G</strong>) to <code>~/Library/Application Support/Developer/Shared/Xcode</code>. Create a folder named <code>File templates</code> (if it&#8217;s not already there). This folder will keep your all custom file template categories as folders. Go inside that folder and create <code>Cocoa Class</code> folder.  This folder will serve as a &#8220;category&#8221; for custom Cocoa classes. Now for the final destination go to that newly created folder folder and create another one named <code>Objective-C NSWindow subclass.pbfiletemplate</code> (mind the &#8220;extension&#8221; of the folder). This is the final destination for all template-related files. Now move template meta-data file (the plist) together with your header and implementation files to that folder. Just to check &#8211; your final path for files should be this: </p>
<ul>
<li>~/Library</li>
<li style="margin-left: 10px">/Application Support</li>
<li style="margin-left: 20px">/Developer</li>
<li style="margin-left: 30px">/Shared</li>
<li style="margin-left: 40px">/Xcode</li>
<li style="margin-left: 50px">/File templates</li>
<li style="margin-left: 60px">/Cocoa class</li>
<li style="margin-left: 70px">/Objective-C NSWindow subclass.pbfiletemplate</li>
</ul>
<p> Note that your header and implementation files must be named <code>class.h</code> and <code>class.m</code> accordingly.</p>
<p>Now if you&#8217;ve done everything correctly &#8211; fire up XCode and create a new project or open up any existing one. After that &#8211; try adding new file to your project. In new file dialog you&#8217;ll see this:</p>
<p><img src="/wp-content/uploads/2009/12/custom-template.png" alt="custom NSWindow subclass template" /></p>
<p>Now press next, type a name for your custom NSWindow subclass and you&#8217;re good to go!</p>
<h2>Things not included here</h2>
<p>As you may have noticed in the tutorial &#8211; this one does not include content view that fills the transparent window. Well, it&#8217;s not here &#8217;cause it&#8217;s impossible so far to create two files out of template while creating &#8220;New File&#8230;&#8221; in a project (we&#8217;d need to create the subclass of NSView to fill our custom NSWindow). At least if it IS possible &#8211; I&#8217;m yet to find that out. Or if you know &#8211; please feel free to share that with me in the comments. </p>
<p>But there is still solution for that &#8211; you can create custom NSView class that does some simple drawing and create a template out of it using the technique above (by replacing the classnames with identifiers and writing template meta-data file).</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/33-xcode-file-template-for-custom-nswindow-subclass/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>git for everyone!!!111!!ONE</title>
		<link>http://cocoakids.net/30-git-for-everyone111one</link>
		<comments>http://cocoakids.net/30-git-for-everyone111one#comments</comments>
		<pubDate>Tue, 01 Dec 2009 12:30:16 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[etc]]></category>
		<category><![CDATA[bookmark]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[scm]]></category>
		<category><![CDATA[version-control]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=30</guid>
		<description><![CDATA[I&#8217;ve found really nifty site today for dealing with git source control management system. It&#8217;s git ready. I think I may be one of the last ones to find it, cause it has loads of articles on different topics. You should definitely check it out if you haven&#8217;t yet.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found really nifty site today for dealing with git source control management system. It&#8217;s <a href="http://gitready.com/">git ready</a>. I think I may be one of the last ones to find it, cause it has loads of articles on different topics. You should definitely check it out if you haven&#8217;t yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/30-git-for-everyone111one/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quickly change desktop background pt. 2</title>
		<link>http://cocoakids.net/25-quickly-change-desktop-background-pt-2</link>
		<comments>http://cocoakids.net/25-quickly-change-desktop-background-pt-2#comments</comments>
		<pubDate>Sat, 28 Nov 2009 15:32:21 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[chaba]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=25</guid>
		<description><![CDATA[Few weeks ago I wrote about writing an App for quickly changing background for Mac OS X desktop. 
Things that&#8217;ve changed:

Up to 10 items in history list (suggestion from Perspx).
App is UI agent now (pseudo-background app). Activates via ⌘+⌥+⌃+B.

To download click this link: Change Background 0.0.2
]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago <a href="http://cocoakids.net/16-quickly-change-desktop-background" title="Quickly change desktop background">I wrote about writing an App</a> for quickly changing background for Mac OS X desktop. </p>
<p>Things that&#8217;ve changed:</p>
<ul>
<li>Up to 10 items in history list (suggestion from <a href="http://perspx.com/" title="Alex Perspx Rozanski">Perspx</a>).</li>
<li>App is UI agent now (pseudo-background app). Activates via ⌘+⌥+⌃+B.</li>
</ul>
<p>To download click this link: <a href="http://cocoakids.net/wp-content/uploads/2009/11/ChangeBackground2.zip" title="Change Background 0.0.2">Change Background 0.0.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/25-quickly-change-desktop-background-pt-2/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Switch between header and implementation files</title>
		<link>http://cocoakids.net/24-switch-between-header-and-implementation-files</link>
		<comments>http://cocoakids.net/24-switch-between-header-and-implementation-files#comments</comments>
		<pubDate>Thu, 26 Nov 2009 13:22:26 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[shortcuts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=24</guid>
		<description><![CDATA[In addition to most common key combination for switching between header and implementation files in xcode you can do that by swiping three fingers up or down.
Neato!!!
]]></description>
			<content:encoded><![CDATA[<p>In addition to most common key combination for switching between header and implementation files in xcode you can do that by <strong>swiping three fingers up or down</strong>.</p>
<p>Neato!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/24-switch-between-header-and-implementation-files/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get difference between two NSDate objects</title>
		<link>http://cocoakids.net/23-get-difference-between-two-nsdate-objects</link>
		<comments>http://cocoakids.net/23-get-difference-between-two-nsdate-objects#comments</comments>
		<pubDate>Tue, 24 Nov 2009 20:42:17 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[nsdate]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=23</guid>
		<description><![CDATA[One of the most often asked questions is how to get difference between two NSDate objects. Since one of the most useful advices I&#8217;ve got while learning cocoa is using what&#8217;s there, I&#8217;d suggest all the folks look carefully at NSDate class reference documentation and see what it provides. Then just fireup the XCode and [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most often asked questions is how to get difference between two NSDate objects. Since one of the most useful advices I&#8217;ve got while learning cocoa is <em>using what&#8217;s there</em>, I&#8217;d suggest all the folks look carefully at NSDate class reference documentation and see what it provides. Then just fireup the XCode and write this code into some method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSTimeInterval interval <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>endDate timeIntervalSinceDate<span style="color: #002200;">:</span>startDate<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>I presume you DO know how to get endDate and startDate object. Otherwise you shouldn&#8217;t be reading this. You&#8217;ll get the difference in seconds with fractions which is pure double. So you can do with it whatever you want. Feed it to some fancy number formatter, start countdown or what not. None of my business ,)</p>
<p>Oh, and btw, here&#8217;s a demo project for you to see this code in action: <a href="/wp-content/uploads/2009/11/DateDifference.zip" title="DateDifference.zip">DateDifference XCode project</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/23-get-difference-between-two-nsdate-objects/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to listen to all possible notifications in 7 lines of code</title>
		<link>http://cocoakids.net/21-how-to-listen-to-all-possible-notifications-in-7-lines-of-code</link>
		<comments>http://cocoakids.net/21-how-to-listen-to-all-possible-notifications-in-7-lines-of-code#comments</comments>
		<pubDate>Sat, 14 Nov 2009 10:40:24 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[geekery]]></category>
		<category><![CDATA[NSNotification]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=21</guid>
		<description><![CDATA[In the last post I mentioned about setting the background to your dragged image and sending the appropriate [private] notification so that Mac OS X would actually change it. At first it didn&#8217;t work so I thought that this notification is no longer valid (considering the fact that it&#8217;s private). So I&#8217;ve setup a little [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://cocoakids.net/16-quickly-change-desktop-background" title="Quickly change desktop background">last post</a> I mentioned about setting the background to your dragged image and sending the appropriate [private] notification so that Mac OS X would actually change it. At first it didn&#8217;t work so I thought that this notification is no longer valid (considering the fact that it&#8217;s private). So I&#8217;ve setup a little app for myself which listens to all possible notifications while run.</p>
<p>Main snippet that does all the job is put in 7 lines of code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aNotification <span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSDistributedNotificationCenter</span> <span style="color: #002200;">*</span>dnc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDistributedNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>dnc addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>processNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>processNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>notification <span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Notification: %@&quot;</span>, notification<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So here&#8217;s another idea for another developer tool. Maybe there&#8217;s one that already exists, but it&#8217;d be really cool to have an app that registers all your your wanted notifications (either by name, app or any other parameter).</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/21-how-to-listen-to-all-possible-notifications-in-7-lines-of-code/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quickly change desktop background</title>
		<link>http://cocoakids.net/16-quickly-change-desktop-background</link>
		<comments>http://cocoakids.net/16-quickly-change-desktop-background#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:00:43 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[chaba]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=16</guid>
		<description><![CDATA[Saturday morning. Can of RedBull. Cocoa. And me. Came up with a simple idea to develop an app for quickly changing desktop background. Just drag an image on an app window and you&#8217;re done!
Had to google around for some tips and tricks. First results disappointed me much since they referenced usage of Carbon and AppleEvents. [...]]]></description>
			<content:encoded><![CDATA[<p>Saturday morning. Can of RedBull. Cocoa. And me. Came up with a simple idea to develop an app for quickly changing desktop background. Just drag an image on an app window and you&#8217;re done!</p>
<p>Had to google around for some tips and tricks. First results disappointed me much since they referenced usage of Carbon and AppleEvents. Oh yeah, and they were dated back to 2004 or so. Then <a href="http://forum.soft32.com/mac/Changing-desktop-background-programatically-ftopict44527.html" title="Changing desktop background programmatically">one post here</a> had what I needed: a ChangeBackground and a [private] notification.</p>
<p>After couple of hours of trial and error I came up with this application that you can download and try out. I think I&#8217;m going to work on this app a bit more so it&#8217;d become more decent product.</p>
<p>It&#8217;s so rough that I can&#8217;t guarantee it will work on your Mac. But here&#8217;s the requirements so far:</p>
<ul>
<li>Snow Leopard</li>
</ul>
<p>Download and [try to] enjoy. Needless to say &#8211; I accept any feedback.</p>
<p>UPDATE: <a href="http://perspx.com/">Perspx</a> found a bug which I successfully reproduced and fixed. You can download updated version <a href="http://cocoakids.net/wp-content/uploads/2009/11/ChangeBackground1.zip" title="Change background">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/16-quickly-change-desktop-background/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Centering NSWindow in Cocoa</title>
		<link>http://cocoakids.net/15-centering-nswindow-in-cocoa</link>
		<comments>http://cocoakids.net/15-centering-nswindow-in-cocoa#comments</comments>
		<pubDate>Mon, 26 Oct 2009 07:10:03 +0000</pubDate>
		<dc:creator>eimantas</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[center]]></category>
		<category><![CDATA[NSWindow]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[tips'n'tricks]]></category>

		<guid isPermaLink="false">http://cocoakids.net/?p=15</guid>
		<description><![CDATA[I was messing around with Cocoa. Building some dummy app to try out GData framework. I was creating preferences window and each time I ran the application &#8211; preferences window was showing somewhere not in the center of the screen. Yes, I know I can adjust the position via Size inspector for the window. Still, [...]]]></description>
			<content:encoded><![CDATA[<p>I was messing around with Cocoa. Building some dummy app to try out GData framework. I was creating preferences window and each time I ran the application &#8211; preferences window was showing somewhere not in the center of the screen. Yes, I know I can adjust the position via Size inspector for the window. Still, the adjustment area is so small I couldn&#8217;t adjust it correctly. And my OCD wanted pixel-perfect positioning. iGoogled and all I found was this NSWindow instance method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>window center<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Note: it does not show your window on screen. This method only sets it&#8217;s frame to the center of the main (read &#8211; the one that has menu bar on top) screen.</p>
]]></content:encoded>
			<wfw:commentRss>http://cocoakids.net/15-centering-nswindow-in-cocoa/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
