<?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/"
	>

<channel>
	<title>Apokalypse Software Corp. &#187; code</title>
	<atom:link href="http://apokalypsesoftware.com/blog/tags/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://apokalypsesoftware.com</link>
	<description>words from the wilderness</description>
	<pubDate>Thu, 02 Apr 2009 16:48:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Double Vision in Xcode doesn&#8217;t Double Your Fun</title>
		<link>http://apokalypsesoftware.com/blog/2008/08/17/119/</link>
		<comments>http://apokalypsesoftware.com/blog/2008/08/17/119/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 19:39:57 +0000</pubDate>
		<dc:creator>huperniketes</dc:creator>
		
		<category><![CDATA[code]]></category>

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

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

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

		<category><![CDATA[process improvement]]></category>

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

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

		<category><![CDATA[unit tests]]></category>

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

		<guid isPermaLink="false">http://apokalypsesoftware.com/?p=119</guid>
		<description><![CDATA[The Mac developer scene is loaded with a lot of helpful and supportive developers out there, sharing ideas, code and encouragement with one another. I&#8217;ve benefitted from the development community, as have the Mori and Clockwork code. I say this not only as one who&#8217;s currently putting new code into them, but seeing the comments [...]]]></description>
			<content:encoded><![CDATA[<p>The Mac developer scene is loaded with a lot of helpful and supportive developers out there, sharing ideas, code and encouragement with one another. I&#8217;ve benefitted from the development community, as have the Mori and Clockwork code. I say this not only as one who&#8217;s currently putting new code into them, but seeing the comments <a href='http://www.hogbaysoftware.com'>Jesse</a> left in the code before me!</p>
<p>So I&#8217;d like to drop an idea into your lap you might find useful for the right type of application. As there are different types of software one can develop, e.g., operating systems, utilities, paint programs and the like, they have particular usage characteristics that they encourage. Some, like <a href='http://apokalypsesoftware.com/products/mori'>digital notebooks</a> and <a href='http://apokalypsesoftware.com/products/clockwork'>desktop timers</a> for example *cough*, tend to remain in operation while the user is logged into his account. Usually, this isn&#8217;t a problem for the user. Unless, of course, the user is also someone further developing said software. Then several questions have to be answered, such as, &#8220;How do I run unit tests on builds while still running a stable version of my app?&#8221; and &#8220;How do I try out the latest build without affecting the files which are currently open in my app?&#8221;</p>
<p>On operating systems based on more primitive process management, it isn&#8217;t troublesome because each built application is fairly independent of others, and often multiple invocations of a single app can be run concurrently. It wasn&#8217;t problematic on the Mac either back on MacOS 9 and earlier, and even in the early MOX releases. But now with Launch Services, running test versions of apps you run on an all-day basis can be problematic. It&#8217;s best to give that test version some different signature so its defaults and file changes don&#8217;t corrupt your normal environment.</p>
<p>Note that this tip doesn&#8217;t prevent two versions from munging the same files; even two unrelated apps can mess up a data file if they&#8217;re updating it at the same time. What this tip does is provide an alternate identity, if you will, for test builds so MOX doesn&#8217;t try to hand it the same data set it gives the release builds of your app. You mustn&#8217;t try to open files in the test build that are currently opened by a release build.</p>
<p>Here&#8217;s how I do it:</p>
<p>1. In the Info.plist file, use the C pre-processor&#8217;s conditional compilation directive to give your app its normal metadata when a release build is made, but a special set of metadata for all other build configurations:</p>
<p><code>#if	defined(Release)<br />
	&lt;key>CFBundleIdentifier&lt;/key><br />
	&lt;string>com.apokalypsesoftware.Mori&lt;/string><br />
#else<br />
	&lt;key>CFBundleIdentifier&lt;/key><br />
	&lt;string>com.apokalypsesoftware.Mori-debug&lt;/string><br />
#endif</code></p>
<p><strong>Update:</strong> I forgot to mention that Xcode 3 and later versions now use specialized editors for the Info.plist files. To override this editor selection, bring up the file&#8217;s <strong>Info</strong> window (by selecting the file and pressing cmd-I) or the <strong>Inspector</strong> window and change the <strong>File Type</strong> setting from &#8220;text.plist.<em>whatever</em>&#8221; to &#8220;text&#8221;. Once you add the conditional to Info.plist you can change <strong>File Type</strong> back and access Info.plist in its raw form from its target&#8217;s <strong>Properties</strong> tab. That way, you can view it in either mode within Xcode. <strong><em>However</em></strong>, making any changes in Xcode&#8217;s plist editor <strong>will</strong> wipe out any conditional directive in the file, so use it only for reviewing current settings, not for actual editing!</p>
<p>2. In the <strong>Target Info</strong> window (brought up via the &#8216;Project > Edit Active Target &#8220;Mori&#8221;&#8216; menu item), select the <strong>Build</strong> tab, specify &#8220;All Configurations&#8221; for the <strong>Configuration</strong> pop-up menu, and enter &#8220;plist&#8221; in the search toolbar widget to bring up the relevant setting items.</p>
<p><img src="http://apokalypsesoftware.com/wp-content/uploads/2008/01/plist-settings.png"/></p>
<p>3. For the &#8220;Info.plist Other Preprocessor Flags&#8221; setting, enter <strong>-traditional</strong> as the value. While not needed to support the test version twin, it will help to prevent any URLs included in the Info.plist from being swallowed up by the pre-processor and possibly causing you sleepless nights because neither MOX nor the Xcode build system have the slightest idea of what the proper format for URLs are, but will behave erratically when it isn&#8217;t just so. So it&#8217;s best to make it a non-issue from the start. (No charge for that and other tips related to usage of the pre-processor you can find in <a href="http://developer.apple.com/technotes/tn2007/tn2175.html">Technical Note TN2175</a>. Incidentally, TN2175 says to use &#8220;-traditional&#8221;, but I use &#8220;-CC&#8221;. Use whatever works for the version you&#8217;re on.)</p>
<p>4. For the <strong>Info.plist Preprocessor Definitions</strong> setting, enter <em>$(CONFIGURATION)</em>. Xcode will change the display to read <strong>&lt;multiple values&gt;</strong>, but that&#8217;s okay because the value will vary according to the current build configuration. It will define a value that matches the build configuration that the pre-processor will test against in the Info.plist. (Try switching the setting in the <strong>Configuration</strong> pop-up menu of the <strong>Build</strong> tab to watch the value change to match the current setting.) This is what will direct the pre-processor to output the correct value for <strong>CFBundleIdentifier</strong>.</p>
<p>5. Check the <strong>Preprocess Info.plist File</strong> checkbox so Xcode invokes the pre-processor on the Info.plist before copying it to the application.</p>
<p>When built, you&#8217;ll be able to run the app concurrently with the release version, and without causing conflicts with data files in use by it. Of course, you won&#8217;t have your preferences set up, but that&#8217;s only a problem until you save preferences for the test version. (Remember, if you try to get around that problem by copying the release version&#8217;s defaults file that it also holds the recently opened files. Purge, close or what-have-you as appropriate!)</p>
<p>One of the extra things I attempted to do in this technique was to provide an alternate name for the private builds or test versions so they would be distinctly named for testers. We used to use naming conventions such as &#8220;Mori 1.7&#946;4&#8243; when distributing such versions back in the pre-MOX days, but including the metadata keys <strong>CFBundleExecutable</strong>, <strong>CFBundleName</strong>, and <strong>CFBundleDisplayName</strong> don&#8217;t have any noticeable effect. Any tips in this regard would be greatly welcomed.</p>
<p>I had also attempted to get the pre-processor to generate the bundle identifier with &#8220;Mori-$(CONFIGURATION)&#8221; that fit whatever build configuration was in effect automatically, which would&#8217;ve avoided the use of the <strong>#if defined</strong> directive. Unfortunately, because the macro expansion would generate Mori-Release that method proved ineffective.</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/oneillicon.png'/ style="float:left"/> A corresponding idea is to use an alternate icon (via the <strong>CFBundleIconFile</strong> specifier) to represent the test version of your app. It helps you and your testers realize when odd behavior is due to running the wrong build just by looking at the dock or application-switcher. It&#8217;s also fun to double-click a document only to realize the test app is opening vital data meant only for the stable version. Hilarity is sure to ensue. But I&#8217;m a professional, don&#8217;t you kids try this at home!</p>
<p>If you don&#8217;t have the construction guy handy, you can resort to the technique I used before I discovered Info.plist processing: MOX&#8217;s own stamping of missing dock files makes for a handy visual guide to your test app. To try it yourself, add the debug version from within your <strong>build/Debug</strong> folder to your dock items. That&#8217;s right, the debug version (or whatever you use for test builds). Now delete it and empty the trash. (You will be building new ones, won&#8217;t you?) Now click on the app icon you just added to the dock. It adds a big fat &#8216;?&#8217; to your lovely icon, doesn&#8217;t it? (At least it should if you deleted the correct icon.) Now rebuild your app and run it <strong>from within Xcode</strong>.</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/docked-alternate.png' /></p>
<p>Now bring up the application switcher (cmd-tab). What do you see? A big fat ol&#8217; &#8216;?&#8217; emblazoned on the debug version of your app! Oh, you don&#8217;t? You see the normal icon? Yeah, you&#8217;re on Leopard, aren&#8217;t you? This trick doesn&#8217;t work as well on Leopard because MOX validates the file state more often than Tiger did. Such as, starting the test build from the Dock instead of only within Xcode. On Leopard, clicking on the dock icon when there&#8217;s a built debug version of your app in the debug folder will restore its unstamped icon. But anytime the question mark goes away, just delete the debug version, empty the trash, and click on the dock icon again.</p>
<p>Have fun, and keep &#8216;em flying!</p>
]]></content:encoded>
			<wfw:commentRss>http://apokalypsesoftware.com/blog/2008/08/17/119/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Solving a New Event Bug Present in iCal When Scripting</title>
		<link>http://apokalypsesoftware.com/blog/2008/01/25/100/</link>
		<comments>http://apokalypsesoftware.com/blog/2008/01/25/100/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 16:46:44 +0000</pubDate>
		<dc:creator>huperniketes</dc:creator>
		
		<category><![CDATA[AppleScript]]></category>

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

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

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

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

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

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

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

		<category><![CDATA[process failure]]></category>

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

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

		<guid isPermaLink="false">http://apokalypsesoftware.com/blog/2008/01/25/100/</guid>
		<description><![CDATA[While trying to solve a user&#8217;s problem with an mGTD script, I came across a subtle issue that demonstrates some issues that arise when violating a programming philosophy, tackling bugs in other people&#8217;s code, and general uncertainty whenever coding in AppleScript.
Working with AppleScript is generally considered iffy, because a lot seems ambiguous and so much [...]]]></description>
			<content:encoded><![CDATA[<p>While trying to solve a user&#8217;s <a href='http://apokalypsesoftware.com/products/node/2597'>problem with an mGTD script</a>, I came across a subtle issue that demonstrates some issues that arise when violating a programming philosophy, tackling bugs in other people&#8217;s code, and general uncertainty whenever coding in AppleScript.</p>
<p>Working with AppleScript is generally considered iffy, because a lot seems ambiguous and so much is dependent on how the dialect is interpreted and how scriptable apps handle some of the application events which scripting is dependent on. I&#8217;ve written scripts before, some I&#8217;m pretty awed by (that it works, actually, but also what it does), but I&#8217;m still hesitant to tackle some scripting issues. In addition, being a GTD greenhorn, and an mGTD noob made trying to respond to this issue authoritatively very questionable.</p>
<p>Thankfully, BMEGuy, mGTD&#8217;s author and all-around community nice guy, tackled the question with a quick solution. But the updated script was still problematic, and so I felt I really needed to participate in coming up with a solution.</p>
<p>Again, being an mGTD noob and all, it took me at least half an hour to figure out how the plugin worked, and the script on top of that. Then, after I was able to get the script to run, it worked for me. Hmm.</p>
<p>But that&#8217;s because I was testing with an entry with a date due of today. Once I switched it to later in the week, the entry was still showing up for today. <em>Isn&#8217;t that odd?</em> It seemed I had inadvertently left in the date line from the original script. When I removed it, I witnessed the same problem.</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbugweek.png' alt='Artifacts of the shy scripted event' align='left' /></p>
<p>It turns out there&#8217;s a bug in MOX 10.4.11&#8217;s iCal 2.0.5 (I&#8217;m guessing it&#8217;s present in earlier versions as well) where it doesn&#8217;t properly update the calendar display for new events made by the script. You won&#8217;t see it in the monthly view. However, you might notice a little oddness in the weekly view.</p>
<p>You can see the event if you add &#8217;show theEvent&#8217; after the script makes a new display alarm for the event (between the 2nd and 3rd &#8216;end tell&#8217; up from the bottom). This will display it&#8217;s properties in the info drawer, but you won&#8217;t see the event anywhere on the calendar (in either week or month view) until iCal is restarted.</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbugweekshow.png' alt='Although the event doesn't show up in the calendar, its properties are displayed in the info drawer' /></p>
<p>Running the script in monthly view doesn&#8217;t show any artifact in the calendar, but the data is shown in the info drawer.</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbugmonthshow.png' alt='icalbugmonthshow.png' /></p>
<p>You could also run the script in the weekly view and then switch to the monthly view, in which case you get this: </p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbugmonthswitched.png' alt='icalbugmonthswitched.png' align='left' /></p>
<p>So now that the question of the event&#8217;s presence in the calendar was settled in my mind, I had to figure out why my faulty script displayed the event, but not the proper one; and how to coax iCal to display it.</p>
<p>Being unfamiliar with mGTD still, I tried to figure out the difference between the <var>attribute name &#8220;dateDue&#8221;</var> and <var>due date</var>. <var>due date</var> is one of the standard properties for entries in a Mori document. <var>attribute name &#8220;dateDue&#8221;</var> is a user column added in the example mGTD notebook. You can view them all the user columns by selecting the menu item <q>Edit > Edit Notebook Columnsâ€¦</q></p>
<p>Okay, good so far, but why would one cause iCal to display properly and not the other? After moving the <var>due date</var> line about for a while, I checked <em>Script Editor</em>&#8217;s <cite>Event Log</cite>, and saw</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbugmissingvalue.png' alt='icalbugmissingvalue.png'/></p>
<p>The event reply for the <var>due date</var> had a <q>missing value</q>! Mori wasn&#8217;t returning a value for the <var>due date</var> property because it wasn&#8217;t set (and wouldn&#8217;t be in the example notebook). Now I had to find a way to use one of those missing values to make <var>theEvent</var> visible without setting it to the wrong date. And the problem with that is most of the properties used in Mori&#8217;s entries aren&#8217;t appropriate for an iCal event.</p>
<p>I eventually thought about re-ordering the messages to iCal instead of being so fixated on a change in the messages to Mori or playing with the properties being set in creating the event. What I came up with was a plan to use the messed up <q>missing value</q> date as before to make the event visible first, and <em>then</em> set the date correctly. The code turned out like this:</p>
<pre>
tell application "Mori"
	tell current entry
		set theDate to (get attribute name "dateDue")
		set faultyDate to due date
		set theName to name
		set theNote to note
	end tell
end tell
tell application "iCal"
	tell calendar "Scramble" -- the user should specify the name of the target calendar here
		set theEvent to make new event at end with properties {description:theNote, summary:theName, start date:faultyDate, allday event:true}
		tell theEvent
			make new display alarm at end with properties {trigger date:theDate}
		end tell
		-- show theEvent
		set theEvent's start date to theDate
	end tell
end tell
</pre>
<p>And to my surprise, it worked! So as I began gathering the materials together for my reply to the issue, I noticed something in the event&#8217;s info drawer that had escaped my attention before:</p>
<p><img src='http://apokalypsesoftware.com/wp-content/uploads/2008/01/icalbughilite.png' alt='The previous script set up appointments for H. G. Wells.' align='left' /></p>
<p>iCal, that&#8217;s just crazy talk! But at least it would explain why it would display traces of an event, if anything at all; and why it wasn&#8217;t noticeable earlier: iCal would correct the event data when reading it in when it started (&#8221;iCal database, that&#8217;s just crazy talk!&#8221;). But somebody forgot to add a sanity check when creating a new event from the properties passed to it by our script. (This is an example of why the <a href='http://en.wikipedia.org/wiki/Once_and_only_once'>Once and Only Once</a> principle should be heeded. If there&#8217;s only one place where events are synthesized from pre-recorded values, whether those values are from a stored file, a script or the UI, then all those code paths will benefit from any sanity checks added to event creation.)</p>
<p>Knowing this, here&#8217;s another means of working around this bug, by sending iCal info that won&#8217;t confuse it:</p>
<pre>tell application "Mori"
	tell current entry
		set theDate to (get attribute name "dateDue")
		-- set faultyDate to due date
		set theName to name
		set theNote to note
	end tell
end tell
tell application "iCal"
	tell calendar "Scramble" -- the user should specify the name of the target calendar here
		set theEvent to make new event at end with properties {description:theNote, summary:theName, start date:theDate, end date:(theDate + 1), allday event:true}
		tell theEvent
			make new display alarm at end with properties {trigger date:theDate}
		end tell
		-- show theEvent
		-- set theEvent's start date to theDate
	end tell
end tell
</pre>
<p>Thinking about these two solutions it&#8217;s clear that picking the latter one, with well-formed properties, is the safest choice to make. <strong>Here&#8217;s additional proof:</strong> the first solution, the one which plays with the start date to make the event appear, will indeed make the event appear. But if there&#8217;s less than 24 hours until the event begins, <em>it will appear on the wrong date</em> and still require iCal to be restarted to appear in the proper location!</p>
<p>It just goes to show you, while you might be able to get away with just the barest minimum, and someone else might normally clean up after you, it&#8217;s best if you did the job correctly from the start in case your safety net disappears from under you.</p>
]]></content:encoded>
			<wfw:commentRss>http://apokalypsesoftware.com/blog/2008/01/25/100/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
