<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	
		
	

	
	
	
		<id>tag:belkadan.com,2007:dealloc//0//</id>
	

	<updated>2012-04-26T00:00:00-04:00</updated>
	
	
		<title type="text">-dealloc</title>
		
			<subtitle type="text">The Belkadan Software Blog</subtitle>
		
	

	<link rel="alternate" href="http://belkadan.com/blog" />
	<link rel="self" href="http://belkadan.com/blog/atom" />
	
	<generator>Jekyll/Shadowbox</generator>
	
		<author>
			<name>Jordy Rose</name>
		</author>
	

	
	
	
	<entry>
		<id>tag:belkadan.com,2007:dealloc/2012/04/Keyboard-Adventures/</id>
		<title type="text">Keyboard Adventures</title>

		<link rel="alternate" href="http://belkadan.com/blog/2012/04/Keyboard-Adventures/" />

		<published>2012-04-26T00:00:00-04:00</published>
		

		
			<updated>2012-04-26T00:00:00-04:00</updated>
		

		

		
			<category term="mac-os-x" scheme="http://belkadan.com/blog/tags" label="Mac OS X" />
		
			<category term="text" scheme="http://belkadan.com/blog/tags" label="Text" />
		

		<summary type="html">&lt;p&gt;The best utility for making custom keyboard layouts on Mac OS X has long been &lt;a href=&quot;http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&amp;amp;id=ukelele&quot;&gt;Ukelele&lt;/a&gt; (sic). Sure, Apple came up with an &lt;a href=&quot;https://developer.apple.com/library/mac/technotes/tn2056&quot; title=&quot;TN2056&quot;&gt;XML format&lt;/a&gt; for keyboard layouts, but when you want a variant of an existing keyboard, it’s a lot easier to just &lt;em&gt;copy the existing keyboard&lt;/em&gt; in Ukelele and modify the keys you want to change.&lt;/p&gt;

&lt;p&gt;A few years ago I took a class on Phonetics and Phonology, and found myself needing to type in &lt;a href=&quot;http://en.wikipedia.org/wiki/International_Phonetic_Alphabet&quot; title=&quot;International Phonetic Alphabet&quot;&gt;IPA&lt;/a&gt; quite often. So I fired up Ukelele and made a copy of the standard Dvorak keyboard and got to work. I didn’t…&lt;/p&gt;</summary>
		<content type="html">&lt;p&gt;The best utility for making custom keyboard layouts on Mac OS X has long been &lt;a href=&quot;http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&amp;amp;id=ukelele&quot;&gt;Ukelele&lt;/a&gt; (sic). Sure, Apple came up with an &lt;a href=&quot;https://developer.apple.com/library/mac/technotes/tn2056&quot; title=&quot;TN2056&quot;&gt;XML format&lt;/a&gt; for keyboard layouts, but when you want a variant of an existing keyboard, it’s a lot easier to just &lt;em&gt;copy the existing keyboard&lt;/em&gt; in Ukelele and modify the keys you want to change.&lt;/p&gt;

&lt;p&gt;A few years ago I took a class on Phonetics and Phonology, and found myself needing to type in &lt;a href=&quot;http://en.wikipedia.org/wiki/International_Phonetic_Alphabet&quot; title=&quot;International Phonetic Alphabet&quot;&gt;IPA&lt;/a&gt; quite often. So I fired up Ukelele and made a copy of the standard Dvorak keyboard and got to work. I didn’t change any of the regular keys, but I added a number of IPA characters for when you hold Option, and turned my numpad into an approximation of the human vowel space.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://belkadan.com/blog/2012/04/Keyboard-Adventures/dvorak-ipa.png&quot; alt=&quot;My keyboard layout when the option key is down&quot; onmouseover=&quot;this.src = 'http://belkadan.com/blog/2012/04/Keyboard-Adventures/dvorak.png'&quot; onmouseout=&quot;this.src = 'http://belkadan.com/blog/2012/04/Keyboard-Adventures/dvorak-ipa.png'&quot; /&gt; &lt;img src=&quot;http://belkadan.com/blog/2012/04/Keyboard-Adventures/dvorak.png&quot; alt=&quot;&quot; style=&quot;display:none&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(mouse over if you’ve forgotten what regular Dvorak looks like)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What are all those orange keys? Those are &lt;em&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Dead_key&quot;&gt;dead keys&lt;/a&gt;,&lt;/em&gt; usually used to make accented characters and co-opted by me for typing characters linguistically similar to the first one.&lt;sup id=&quot;fnref:example&quot;&gt;&lt;a href=&quot;#fn:example&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; Ukelele can handle these as well.&lt;/p&gt;

&lt;p&gt;To use your custom keyboard, just save the &lt;code&gt;keylayout&lt;/code&gt; file from Ukelele in a “Keyboard Layouts” folder inside your Library folder. You can even give your keyboard an icon for the menu bar switcher by putting a 16x16 icon file in the folder, as long as it’s in ICNS format and has the same name as the layout file.&lt;/p&gt;

&lt;p&gt;All that was good enough for me until Mac OS X Lion came around.&lt;/p&gt;

&lt;h3 id=&quot;press-and-hold&quot;&gt;Press and Hold&lt;/h3&gt;

&lt;p&gt;When Lion came out, there was a new feature, clearly inspired by iOS: the ability to hold a key for a bit and get a &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html#//apple_ref/doc/uid/20000961-SW4&quot;&gt;popover&lt;/a&gt; of variants. This generated quite a bit of outcry from a number of people who apparently need to type repeated letters a lot. I, on the other hand, think it’s a pretty cool feature.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://belkadan.com/blog/2012/04/Keyboard-Adventures/press-and-hold.png&quot; alt=&quot;Variations on &amp;quot;e&amp;quot;, in TextEdit&quot; title=&quot;Press-and-hold in TextEdit&quot; class=&quot;bordered&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’d be really nice to have configurable press-and-hold options in the same way that we have configurable keyboard layouts with configurable dead keys, right? Having taken a year of Mandarin Chinese, it might be useful if, say, the first four variants of “e” corresponded to the four tones in Mandarin. But there’s nowhere in the &lt;code&gt;keylayout&lt;/code&gt; spec that includes this feature.&lt;/p&gt;

&lt;p&gt;Fortunately, &lt;a href=&quot;http://apple.stackexchange.com/a/44928&quot;&gt;someone’s already solved this problem&lt;/a&gt;. For maybe 95% of the world, Lri’s solution is good enough: it works, and it’s fairly extensible. There was just one problem: my custom Dvorak IPA keyboard layout wasn’t getting the press-and-hold popover at all.&lt;/p&gt;

&lt;h3 id=&quot;the-text-input-system&quot;&gt;The Text Input System&lt;/h3&gt;

&lt;p&gt;The OS X text input system is a complicated beast, but then again text input is pretty complicated in and of itself. Keyboard layouts are well and good for many alphabetic languages, but for something like Japanese or Chinese you need a bit more power. For this, there are programs called &lt;em&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/#releasenotes/Cocoa/RN-InputMethodKit/_index.html&quot;&gt;input methods&lt;/a&gt;,&lt;/em&gt; which handle, say, the translation from Latin characters to Chinese ones with the corresponding Mandarin pronunciation.&lt;sup id=&quot;fnref:palettes&quot;&gt;&lt;a href=&quot;#fn:palettes&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;The press-and-hold feature is implemented as an input method that listens for held-down key events. Unlike the Japanese or Chinese input methods, the press-and-hold feature is not exclusive, and does not appear in the input menu. Other than that, though, the text input system treats it like any other input method, and in theory you could write my own input method that did exactly the same thing. (More on this later…)&lt;/p&gt;

&lt;p&gt;Underneath the nice interface for writing input methods is the &lt;a href=&quot;https://developer.apple.com/library/mac/#documentation/TextFonts/Reference/TextInputSourcesReference/Reference/reference.html&quot;&gt;Text Input Source Services&lt;/a&gt; layer. This is a CoreFoundation-compatible C API for the text input system — essentially a programmatic interface to the input menu. That means it has to handle both the complex input methods like Japanese and the humble keyboard layouts for QWERTY, Dvorak, and my “Dvorak IPA”.&lt;/p&gt;

&lt;p&gt;In order to bring the &lt;code&gt;keylayout&lt;/code&gt; files in line with the way OS X plugins are structured, and more importantly to make them localizable, there’s a second way to provide keyboard layouts to the text input system: bundles containing &lt;code&gt;keylayout&lt;/code&gt; files. This is also described &lt;a href=&quot;https://developer.apple.com/library/mac/technotes/tn2056&quot; title=&quot;TN2056&quot;&gt;alongside the XML format&lt;/a&gt;, and shows a pretty standard bundle layout. The main benefit is being able to provide a &lt;code&gt;strings&lt;/code&gt; file for the localized keyboard names, but there’s also the added effects of having the icon and the keyboard layout in a single package, and being able to distribute multiple keyboard layouts together if you want.&lt;/p&gt;

&lt;p&gt;None of that was going to solve my problem, though. It wasn’t until I looked in the actual header file for the Text Input Source Services that I discovered there was more to writing keyboard layouts, or for that matter input methods, than existed in Apple’s online documentation. Specifically:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For Leopard, there are two new keys for use in plists to provide information that supports the Text Input Sources functions above (these keys will be ignored in earlier systems):&lt;/p&gt;

  &lt;p&gt;&lt;code&gt;&quot;TISInputSourceID&quot;&lt;/code&gt; - a key to specify the InputSourceID, a reverse-DNS-style string meant to uniquely identify any input source. If this key is not specified, the Text Input Sources functions will attempt to construct an InputSourceID from other information.&lt;/p&gt;

  &lt;p&gt;&lt;code&gt;&quot;TISIntendedLanguage&quot;&lt;/code&gt; - a key to specify the primary language which the input source is intended to input. If there is none - as with the Unicode Hex Input key layout, for example - this key need not be specified. The language is indicated by a string in in the format described by &lt;a href=&quot;http://en.wikipedia.org/wiki/IETF_language_tag&quot;&gt;BCP 47&lt;/a&gt; (the successor to RFC 3066).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;In the Info.plist file, the value for the &lt;code&gt;CFBundleIdentifier&lt;/code&gt; key must be a string that includes &lt;code&gt;&quot;.keyboardlayout.&quot;&lt;/code&gt;; typically this might be something like &lt;code&gt;&quot;com.companyname.keyboardlayout.MyKeyboardLayouts&quot;&lt;/code&gt;. (Before Leopard, it was required to be a string that began &lt;code&gt;&quot;com.apple.keyboardlayout&quot;&lt;/code&gt;, even for keyboard layouts not supplied by Apple).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;A dictionary of properties for each key layout in the bundle should be provided using a key of the form &lt;code&gt;&quot;KLInfo_&lt;/code&gt;&lt;em&gt;keylayoutname&lt;/em&gt;&lt;code&gt;&quot;&lt;/code&gt; (even if &lt;em&gt;keylayoutname&lt;/em&gt; includes spaces or punctuation). This dictionary is where to specify the keys &lt;code&gt;&quot;TISInputSourceID&quot;&lt;/code&gt; and &lt;code&gt;&quot;TISIntendedLanguage&quot;&lt;/code&gt; and their associated values.&lt;/p&gt;

  &lt;p&gt;&lt;code&gt;&quot;TISInputSourceID&quot;&lt;/code&gt; note: For keyboard layouts this should typically be something like &lt;code&gt;&quot;com.companyname.keylayout.&lt;/code&gt;&lt;em&gt;keylayoutname&lt;/em&gt;``”&lt;code&gt;. If this key is not specified, an InputSourceID will be constructed by combining _bundleID_ + &lt;/code&gt;“.keylayout.”` + &lt;em&gt;keylayoutname&lt;/em&gt;.&lt;/p&gt;

  &lt;p&gt;If the keyboard layouts in the above example were intended to input Azerbaijani in Latin script, then the Info.plist entries could be:&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;&amp;lt;key&amp;gt;KLInfo_MyLayoutOne&amp;lt;/key&amp;gt;
&amp;lt;dict&amp;gt;
  &amp;lt;key&amp;gt;TISInputSourceID&amp;lt;/key&amp;gt;
  &amp;lt;string&amp;gt;com.companyname.keylayout.MyLayoutOne&amp;lt;/string&amp;gt;
  &amp;lt;key&amp;gt;TISIntendedLanguage&amp;lt;/key&amp;gt;
  &amp;lt;string&amp;gt;az-Latn&amp;lt;/string&amp;gt;
&amp;lt;/dict&amp;gt;
&amp;lt;key&amp;gt;KLInfo_MyLayoutTwo&amp;lt;/key&amp;gt;
&amp;lt;dict&amp;gt;
  &amp;lt;key&amp;gt;TISInputSourceID&amp;lt;/key&amp;gt;
  &amp;lt;string&amp;gt;com.companyname.keylayout.MyLayoutTwo&amp;lt;/string&amp;gt;
  &amp;lt;key&amp;gt;TISIntendedLanguage&amp;lt;/key&amp;gt;
  &amp;lt;string&amp;gt;az-Latn&amp;lt;/string&amp;gt;
&amp;lt;/dict&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Cocoa programmers will probably be smirking at the Text Input Source Services’ abuse of Info.plist: it’s not that this information shouldn’t be here, but that it shouldn’t use *prefixed* keys. The usual way to do this is an array of dictionaries with a &lt;code&gt;&quot;name&quot;&lt;/code&gt; key of some kind. Also, the keyboard names are localized in InfoPlist.strings, which is supposed to only be for data in Info.plist.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is more information on how input methods use these keys, but the main point is that this information isn’t available &lt;em&gt;anywhere&lt;/em&gt; online. And though Ukelele now has an option to save in bundle format, it doesn’t have a way to set the magic language key — which, it turns out, is the key to getting the press-and-hold feature to work with custom keyboard layouts. By manually editing the Info.plist file and specifying that my keyboard had an intended language of &lt;code&gt;&quot;en&quot;&lt;/code&gt;, then logging out and back in, I got exactly what I wanted: press-and-hold accents in Dvorak IPA mode.&lt;/p&gt;

&lt;p&gt;I’m planning to send an e-mail to the developer of Ukelele to add this feature, and I’ve filed &lt;a href=&quot;rdar://problem/11327265&quot; title=&quot;Radar&quot;&gt;rdar&lt;/a&gt;://&lt;a href=&quot;http://openradar.appspot.com/11327265&quot; title=&quot;OpenRadar&quot;&gt;11327265&lt;/a&gt; with Apple about the lack of documentation here. Meanwhile, I’ve copied the relevant parts of the headers here in the hopes that it’ll show up on [search engine] next time someone has this problem.&lt;/p&gt;

&lt;h3 id=&quot;custom-press-and-hold&quot;&gt;Custom Press and Hold&lt;/h3&gt;

&lt;p&gt;So, this ought to be enough, right? I managed to get the accents popover to show up for my custom keyboard layout, and I know how to change the set of variants. If I was just interested in that, I’d be done, right?&lt;/p&gt;

&lt;p&gt;…Except I don’t really like messing with system files, especially not ones in the /System folder that you’re really not supposed to touch. Because system apps and plugins are &lt;a href=&quot;http://en.wikipedia.org/wiki/Code_signing&quot;&gt;signed&lt;/a&gt; now, modifying resources inside PressAndHold.app could disable the feature altogether. (Clearly it doesn’t right now, but that could change.) On a more practical note, any system update that touches PressAndHold.app could overwrite your changes, which is at least a bit annoying.&lt;/p&gt;

&lt;p&gt;I said before that it’s possible to write your own input method that does everything PressAndHold.app does. But…&lt;/p&gt;

&lt;blockquote&gt;
  &lt;dl&gt;
    &lt;dt&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/dt&gt;
    &lt;dd&gt;Some applications do about 90% of what I want.&lt;/dd&gt;
    &lt;dt&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/dt&gt;
    &lt;dd&gt;Develop my own applications.&lt;/dd&gt;
    &lt;dt&gt;&lt;strong&gt;Better Solution:&lt;/strong&gt;&lt;/dt&gt;
    &lt;dd&gt;Patch the application myself…&lt;/dd&gt;
  &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;p&gt;That comes from the developer of &lt;a href=&quot;http://www.culater.net/software/SIMBL/SIMBL.php&quot;&gt;SIMBL&lt;/a&gt;, a program specifically designed for injecting code into existing OS X applications. While this can be a HUGE security risk, it’s also what allows me to build a plugin like &lt;a href=&quot;http://belkadan.com/keystone/&quot;&gt;Keystone&lt;/a&gt; that runs inside Safari. In this case, I decided to make a plugin that ran inside PressAndHold.app. In a fit of grandeur I called it MagicPressAndHold.&lt;/p&gt;

&lt;p&gt;The actual implementation of that plug-in wasn’t so hard. Using the &lt;a href=&quot;http://www.codethecode.com/projects/class-dump/&quot;&gt;&lt;code&gt;class-dump&lt;/code&gt;&lt;/a&gt; utility (and, uh, breaking Apple’s Terms of Service a bit), I found that there’s a single method that serves as the entry point for accented character suggestions: &lt;code&gt;-&lt;abbr title=&quot;- (NSString *)accentsStringForString:(NSString *)input language:(NSString *)languageCode;&quot;&gt;accentsStringForString:language:&lt;/abbr&gt;&lt;/code&gt;. So I just had to inject my own implementation of that method to gain control over the set of variant characters.&lt;/p&gt;

&lt;p&gt;&lt;!-- The above uses explicit &lt;code&gt;&lt;/code&gt; tags so that the text inside is still valid for abbreviations. Yay for Kramdown. --&gt;&lt;/p&gt;

&lt;p&gt;I ran into a slight snag because PressAndHold.app doesn’t run like a normal Mac OS X application. Instead, it’s launched by the text input system as necessary and doesn’t show up in NSWorkspace’s usual “application launched” notifications. That means SIMBL doesn’t get a chance to look at it. Manually sending SIMBL’s “please load me” AppleEvent to PressAndHold.app did the trick.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;applescript&quot;&gt;&lt;span class=&quot;k&quot;&gt;tell&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;application&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;PressAndHold&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;inject&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;SIMBL&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;into&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Snow&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Leopard&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And it worked!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://belkadan.com/blog/2012/04/Keyboard-Adventures/magic-press-and-hold.png&quot; alt=&quot;The variants for &amp;quot;i&amp;quot; match the four tones of Mandarin Chinese.&quot; class=&quot;bordered&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Of course, this isn’t so useful yet. There’s no preferences UI, and it isn’t automatically loaded. But it’s a working proof-of-concept, and the code is &lt;a href=&quot;https://github.com/belkadan/MagicPressAndHold&quot;&gt;available on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;postscript-chronology&quot;&gt;Postscript: Chronology&lt;/h3&gt;

&lt;p&gt;The progression from “custom keyboard layout” to “hacking into a system plugin for the text input system” may have seemed pretty overwhelming already, but the true story is even less of a straight line. In truth, I first heard about the press-and-hold feature when everyone started complaining about it. Like I said, when I first upgraded to Lion myself, I thought it was pretty cool.&lt;/p&gt;

&lt;p&gt;Then I spent months never using it, and not just because I was on a custom keyboard layout. I just forgot about it.&lt;/p&gt;

&lt;p&gt;A few days ago I came across it again on someone’s blog, made it to the &lt;a href=&quot;http://apple.stackexchange.com/a/44928&quot;&gt;StackOverflow post&lt;/a&gt; I mentioned already, and found about about PressAndHold.app. What was different about my custom keyboard layout from Apple’s? Unfortunately, Apple’s keyboard layouts aren’t stored in a user-readable format anymore, so I couldn’t do much more than check the output of Ukelele. I tried saying my keyboard layout was a Roman keyboard instead of Unicode, but that had no effect. I tried using a bundle instead of just the &lt;code&gt;keylayout&lt;/code&gt; file, but &lt;em&gt;that&lt;/em&gt; had no effect. So I started hacking into PressAndHold.app to try to figure out what was going on.&lt;/p&gt;

&lt;p&gt;(That was fun. Pro tip: don’t attach a text-based debugger to a text input method — you lose all ability to type after a few seconds. Not sure why the delay.)&lt;/p&gt;

&lt;p&gt;I started hacking together MagicPressAndHold, taking my usual approach of simply logging arguments and return values in the methods I wanted to edit. In this case, that was &lt;code&gt;-accentsStringForString:language:&lt;/code&gt;…and I noticed that my Dvorak IPA keyboard layout had no language (an empty string). What was up with that?&lt;/p&gt;

&lt;p&gt;I sort of took it for granted at first that &lt;code&gt;keylayout&lt;/code&gt; files can’t have associated languages, since there’s nothing in the XML file that indicates a language. So my first pass was to &lt;a href=&quot;https://github.com/belkadan/MagicPressAndHold/commit/6ef2d2d2680068e79d3443d06f46795cd4c5401e&quot;&gt;assume the system language&lt;/a&gt; if there isn’t one associated with the keyboard, and &lt;em&gt;that worked.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Why continue? Well, it was an accident. I started looking ahead to how I could have the variant characters be different depending on which keyboard was active. And the way you find out which keyboard is active is by using the Text Input Source Services.&lt;/p&gt;

&lt;p&gt;And &lt;em&gt;that’s&lt;/em&gt; where the “secret” language keys are documented. So after getting MagicPressAndHold in a working state, I backtracked to my keyboard layout bundle, trying a number of experiments in the configuration. What I didn’t realize is that most of them don’t take effect unless you log out and back in again, but the simplest thing—a &lt;code&gt;TISIntendedLanguage&lt;/code&gt; of &lt;code&gt;&quot;en&quot;&lt;/code&gt;—did work in the end.&lt;/p&gt;

&lt;p&gt;And MagicPressAndHold? Well, without a good way to inject itself into PressAndHold (which quits after not being used for a while, and launches again on demand), it’s probably going to remain an experiment, and I don’t plan to do any serious work on it.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:example&quot;&gt;
      &lt;p&gt;For example, typing Option-R gives me retroflex variants of /t/, /d/, and /s/.&lt;a href=&quot;#fnref:example&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:palettes&quot;&gt;
      &lt;p&gt;There are actually &lt;em&gt;two&lt;/em&gt; other kinds of input to the Cocoa text system: “ink” inputs, which allow you to, say, &lt;em&gt;draw&lt;/em&gt; a Chinese character instead of typing its Romanization, and “palette” inputs, which include the Keyboard Viewer and Character Picker palettes. You can create these too, but that’s beyond the scope of this post.&lt;a href=&quot;#fnref:palettes&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:belkadan.com,2007:dealloc/freebies/Mail-Aliases/</id>
		<title type="text">Mail Aliases</title>

		<link rel="alternate" href="http://belkadan.com/blog/freebies/Mail-Aliases/" />

		<published>2012-04-10T00:00:00-04:00</published>
		

		
			<updated>2012-04-10T00:00:00-04:00</updated>
		

		
			<category term="freebies" scheme="http://belkadan.com/blog/categories" label="Freebies" />
		

		
			<category term="mailapp" scheme="http://belkadan.com/blog/tags" label="Mail.app" />
		

		<summary type="html">&lt;p&gt;Even though I’m the developer of &lt;a href=&quot;http://belkadan.com/webmailer&quot;&gt;Webmailer&lt;/a&gt;, I mostly use Apple Mail for e-mail these days. Sometimes it’s nice to be able to send e-mails using different identities; for Belkadan Software tech support I’m “Jordy Rose”, but for school-related communication I’d prefer my name to show up as “Jordan Rose” and use my school e-mail address. Mail supports this, but doesn’t actually have a preference for it without creating separate accounts.&lt;sup id=&quot;fnref:names&quot;&gt;&lt;a href=&quot;#fn:names&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; You have to edit the settings file manually.&lt;/p&gt;

&lt;p&gt;So I wrote &lt;strong&gt;&lt;a href=&quot;https://github.com/downloads/belkadan/Mail-Aliases/Mail%20Aliases.zip&quot;&gt;Mail Aliases&lt;/a&gt;&lt;/strong&gt;, a little app that can edit this data for you.</summary>
		<content type="html">&lt;p&gt;Even though I’m the developer of &lt;a href=&quot;http://belkadan.com/webmailer&quot;&gt;Webmailer&lt;/a&gt;, I mostly use Apple Mail for e-mail these days. Sometimes it’s nice to be able to send e-mails using different identities; for Belkadan Software tech support I’m “Jordy Rose”, but for school-related communication I’d prefer my name to show up as “Jordan Rose” and use my school e-mail address. Mail supports this, but doesn’t actually have a preference for it without creating separate accounts.&lt;sup id=&quot;fnref:names&quot;&gt;&lt;a href=&quot;#fn:names&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; You have to edit the settings file manually.&lt;/p&gt;

&lt;p&gt;So I wrote &lt;strong&gt;&lt;a href=&quot;https://github.com/downloads/belkadan/Mail-Aliases/Mail%20Aliases.zip&quot;&gt;Mail Aliases&lt;/a&gt;&lt;/strong&gt;, a little app that can edit this data for you.&lt;!--more--&gt; It’s not particularly polished, but it might come in handy for some of you. Note that this comes WITHOUT WARRANTY, so if you’re at all worried, back up your Mail account settings in &lt;code&gt;~/Library/Mail/V2/MailData/Accounts.plist&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Mail Aliases has been tested on Mac OS X v10.7 (Lion), but should work with 10.6 (Snow Leopard) as well. You can check out the source &lt;a href=&quot;https://github.com/belkadan/Mail-Aliases&quot;&gt;on Github&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;By the way, if you wondering why the blog has been silent so long, there are two reasons. First, I’ve been moving away from Cocoa development into lower-level stuff, specifically contributing to &lt;a href=&quot;http://clang.llvm.org&quot;&gt;Clang&lt;/a&gt; in my free time. (Don’t worry, Webmailer and Keystone are still being supported!) Second, I spent several months after graduation teaching English abroad, and during that time I tried to stay away from software for a while. I’m hoping to start posting again soon about nifty programming things I come across, but (like every other blog on the internet) no promises!&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:names&quot;&gt;
      &lt;p&gt;You can have multiple e-mail addresses associated with a single account by separating them with commas, but you can only have one “Full Name” per account.&lt;a href=&quot;#fnref:names&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:belkadan.com,2007:dealloc/freebies/Header-Anchors/</id>
		<title type="text">Header Anchors: A Safari Extension</title>

		<link rel="alternate" href="http://belkadan.com/blog/freebies/Header-Anchors/" />

		<published>2011-08-14T00:00:00-04:00</published>
		

		
			<updated>2011-08-14T00:00:00-04:00</updated>
		

		
			<category term="freebies" scheme="http://belkadan.com/blog/categories" label="Freebies" />
		

		
			<category term="safari" scheme="http://belkadan.com/blog/tags" label="Safari" />
		
			<category term="safari-extensions" scheme="http://belkadan.com/blog/tags" label="Safari extensions" />
		

		<summary type="html">&lt;p&gt;&lt;img src=&quot;http://belkadan.com/blog/freebies/Header-Anchors/icon&quot; alt=&quot;&quot; class=&quot;alignleft&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Ever want to link to a specific part of a web page? Well-designed web pages have &lt;em&gt;anchors&lt;/em&gt; at the start of each section, which can serve as targets of links.&lt;sup id=&quot;fnref:name&quot;&gt;&lt;a href=&quot;#fn:name&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; You’ve probably seen URLs that end in &lt;code&gt;#something&lt;/code&gt;; that &lt;em&gt;fragment identifier&lt;/em&gt;, or “frag-id”, refers to a specific anchor on the page.&lt;/p&gt;

&lt;p&gt;What’s annoying, though, is that even if an author includes them, they’re hard to discover. Sometimes there’s a table of contents, sometimes not. What I really wanted was a way to just click on the nearest header and grab the link. So I wrote “Header Anchors”.</summary>
		<content type="html">&lt;p&gt;&lt;img src=&quot;http://belkadan.com/blog/freebies/Header-Anchors/icon&quot; alt=&quot;&quot; class=&quot;alignleft&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Ever want to link to a specific part of a web page? Well-designed web pages have &lt;em&gt;anchors&lt;/em&gt; at the start of each section, which can serve as targets of links.&lt;sup id=&quot;fnref:name&quot;&gt;&lt;a href=&quot;#fn:name&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; You’ve probably seen URLs that end in &lt;code&gt;#something&lt;/code&gt;; that &lt;em&gt;fragment identifier&lt;/em&gt;, or “frag-id”, refers to a specific anchor on the page.&lt;/p&gt;

&lt;p&gt;What’s annoying, though, is that even if an author includes them, they’re hard to discover. Sometimes there’s a table of contents, sometimes not. What I really wanted was a way to just click on the nearest header and grab the link. So I wrote “Header Anchors”.&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Header Anchors is a &lt;a href=&quot;https://extensions.apple.com/&quot;&gt;Safari extension&lt;/a&gt; which turns any header into a link if it has an associated anchor. It won’t look like a link (that’s my preferred design choice) but if you mouse over a header that has an anchor, the cursor will change to the usual “click here” pointer. You can then grab or copy the URL as usual.&lt;/p&gt;

&lt;p&gt;The quickest way to test this is to go to &lt;a href=&quot;http://en.wikipedia.org/wiki/Never_Gonna_Give_You_Up&quot;&gt;a Wikipedia article&lt;/a&gt;. Each section on the page has a header (such as “References”) which has an associated anchor. With Header Anchors installed, clicking on “References” will show you the URL that refers to that part of the page in your location bar.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://belkadan.com/blog/freebies/Header-Anchors/HeaderAnchors-1.0.safariextz&quot;&gt;Download Header Anchors&lt;/a&gt;. Requires Safari 5.&lt;/p&gt;

&lt;p&gt;Header Anchors will never access personal data, other than any preferences it might have in the future. Header Anchors will never access the internet; it does all its work in the browser.&lt;/p&gt;

&lt;p&gt;You can check out the source &lt;a href=&quot;https://github.com/belkadan/HeaderAnchors.safariextension&quot;&gt;on Github&lt;/a&gt;. If anyone wants to port this to Chrome or Firefox, be my guest…and please tell me about it, so I can link to your port!&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:name&quot;&gt;
      &lt;p&gt;The term “anchor” is why the HTML tag for linking is called &lt;code&gt;a&lt;/code&gt;; its other use was to introduce an anchor on the page. Today I think the standard way to do that is just to use an &lt;code&gt;id&lt;/code&gt; attribute.&lt;a href=&quot;#fnref:name&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</content>
	</entry>
	
	<entry>
		<id>tag:belkadan.com,2007:dealloc/2011/08/Little-Big-Details/</id>
		<title type="text">&quot;Little Big Details&quot;</title>

		<link rel="alternate" href="http://belkadan.com/blog/2011/08/Little-Big-Details/" />

		<published>2011-08-10T00:00:00-04:00</published>
		

		
			<updated>2011-08-10T00:00:00-04:00</updated>
		

		

		
			<category term="user-experience" scheme="http://belkadan.com/blog/tags" label="User experience" />
		

		<summary type="html">&lt;p&gt;I just discovered the site &lt;a href=&quot;http://littlebigdetails.com&quot;&gt;Little Big Details&lt;/a&gt; (via a UI designer’s Twitter feed…only I can’t remember whose).&lt;/p&gt;

&lt;p&gt;Yes, yes, a thousand times yes. These sorts of details are what separate functional software from &lt;em&gt;good&lt;/em&gt; software. This is the sort of thing I was talking about in “&lt;a href=&quot;http://belkadan.com/blog/2011/07/Priorities&quot;&gt;Priorities&lt;/a&gt;”. This is something that I think Apple gets right more than most people, but something which is good for everyone.&lt;/p&gt;

&lt;p&gt;Whenever there’s a place in your program where you can leave things as they are, or make things easier on the user, and you’re not on a deadline, the right choice is always &lt;em&gt;make things easier on the user&lt;/em&gt;.</summary>
		<content type="html">&lt;p&gt;I just discovered the site &lt;a href=&quot;http://littlebigdetails.com&quot;&gt;Little Big Details&lt;/a&gt; (via a UI designer’s Twitter feed…only I can’t remember whose).&lt;/p&gt;

&lt;p&gt;Yes, yes, a thousand times yes. These sorts of details are what separate functional software from &lt;em&gt;good&lt;/em&gt; software. This is the sort of thing I was talking about in “&lt;a href=&quot;http://belkadan.com/blog/2011/07/Priorities&quot;&gt;Priorities&lt;/a&gt;”. This is something that I think Apple gets right more than most people, but something which is good for everyone.&lt;/p&gt;

&lt;p&gt;Whenever there’s a place in your program where you can leave things as they are, or make things easier on the user, and you’re not on a deadline, the right choice is always &lt;em&gt;make things easier on the user&lt;/em&gt;.&lt;!--more--&gt;&lt;/p&gt;

</content>
	</entry>
	
	<entry>
		<id>tag:belkadan.com,2007:dealloc/2011/07/Weak-Linking/</id>
		<title type="text">Weak Linking</title>

		<link rel="alternate" href="http://belkadan.com/blog/2011/07/Weak-Linking/" />

		<published>2011-07-29T00:00:00-04:00</published>
		

		
			<updated>2011-07-29T00:00:00-04:00</updated>
		

		

		
			<category term="compilers" scheme="http://belkadan.com/blog/tags" label="Compilers" />
		
			<category term="linking" scheme="http://belkadan.com/blog/tags" label="Linking" />
		

		<summary type="html">&lt;p&gt;When you compile a program that uses external libraries or frameworks, the last step (or a step near the end, at least) is to hook up all of the functions, etc. you use in your program to their implementations in the libraries. This is called “linking”.&lt;sup id=&quot;fnref:simplified&quot;&gt;&lt;a href=&quot;#fn:simplified&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;A while back, Apple realized that when they added new features to their frameworks (usually with the release of each new OS version), people might want to take advantage of them, but remain backwards-compatible with old OSs. So they added a feature called &lt;a href=&quot;http://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html#//apple_ref/doc/uid/20002378-106633-CJBGFCAC&quot;&gt;weak linking&lt;/a&gt;. Apple wasn’t the first to realize that…&lt;/p&gt;</summary>
		<content type="html">&lt;p&gt;When you compile a program that uses external libraries or frameworks, the last step (or a step near the end, at least) is to hook up all of the functions, etc. you use in your program to their implementations in the libraries. This is called “linking”.&lt;sup id=&quot;fnref:simplified&quot;&gt;&lt;a href=&quot;#fn:simplified&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;A while back, Apple realized that when they added new features to their frameworks (usually with the release of each new OS version), people might want to take advantage of them, but remain backwards-compatible with old OSs. So they added a feature called &lt;a href=&quot;http://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html#//apple_ref/doc/uid/20002378-106633-CJBGFCAC&quot;&gt;weak linking&lt;/a&gt;. Apple wasn’t the first to realize that this might be a problem, and I don’t know if they were the first to come up with weak linking. (Heck, it wasn’t even their first time; apparently there was a similar capability in Mac OS Classic’s Code Fragment Manager.) But that’s where I heard about it first.&lt;/p&gt;

&lt;p&gt;Here’s the way you use it (taken from Apple’s guide at the link above):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If a weakly linked symbol is not available in a framework, the linker sets the address of the symbol to &lt;code&gt;NULL&lt;/code&gt;. You can check this address in your code using code similar to the following:&lt;/p&gt;

  &lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyWeakLinkedFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weak_import&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyWeakLinkedFunction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyWeakLinkedFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

  &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When checking for the existence of a symbol, you must explicitly compare it to &lt;code&gt;NULL&lt;/code&gt; or &lt;code&gt;nil&lt;/code&gt; in your code. You cannot use the negation operator ( &lt;code&gt;!&lt;/code&gt; ) to negate the address of the symbol.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’m not sure why that last note is in there; I’m pretty sure it would work anyway with a modern GCC or Clang. But I haven’t tested it.&lt;/p&gt;

&lt;p&gt;Anyway, pretty cool, right? If you want to take advantage of something that only exists on 10.6 (say, &lt;a href=&quot;http://developer.apple.com/library/mac/documentation/cocoa/conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html#//apple_ref/doc/uid/TP30001163-CH24-SW1&quot;&gt;associated objects&lt;/a&gt;) you can do that without sacrificing backwards compatibility.&lt;/p&gt;

&lt;p&gt;Functions are all well and good, but what about other symbols, though? Webmailer uses the &lt;a href=&quot;http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSImageNameStatusAvailable&quot;&gt;&lt;code&gt;NSImageNameStatusAvailable&lt;/code&gt;&lt;/a&gt; constant to show the standard “online” image from iChat, using it to show which destination is active. I was trying to use it like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;NSImage&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activeImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// No need to redeclare it as weak-linked: AvailabilityMacros.h takes care of it.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSImageNameStatusAvailable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;activeImage&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSImage&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;imageNamed:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSImageNameStatusAvailable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Fall back to an image in our bundle.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;activeImage&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSImage&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;initWithContentsOfFile:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bundle&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;pathForImageResource:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&amp;quot;active&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And it wasn’t working. Despite my check to see if the image name was &lt;code&gt;NULL&lt;/code&gt;, it still seemed to be unconditionally loading the image.&lt;/p&gt;

&lt;p&gt;See the problem?&lt;/p&gt;

&lt;p&gt;Let’s look at the instructions again:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If a weakly linked symbol is not available in a framework, the linker sets the &lt;strong&gt;address&lt;/strong&gt; of the symbol to &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OH! Right. You need to be able to tell the difference between a variable whose &lt;em&gt;value&lt;/em&gt; is &lt;code&gt;NULL&lt;/code&gt;, and a variable that doesn’t actually exist at all. For a function pointer, this doesn’t matter: when you say the name of a function like &lt;code&gt;objc_getAssociatedObject&lt;/code&gt;, the compiler treats it like &lt;code&gt;&amp;amp;objc_getAssociatedObject&lt;/code&gt;. But that’s not true for &lt;code&gt;NSImageNameStatusAvailable&lt;/code&gt;, which is just a global constant, a pointer to an NSString.&lt;/p&gt;

&lt;p&gt;Here’s what I should have written:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSImageNameStatusAvailable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And indeed that works. Must be careful in the future!&lt;/p&gt;

&lt;p&gt;By the way, Greg Parker has a good article on &lt;a href=&quot;http://www.sealiesoftware.com/blog/archive/2009/09/09/objc_explain_Weak-import_classes.html&quot;&gt;weakly-linked &lt;em&gt;classes&lt;/em&gt;&lt;/a&gt;, but since this only works back to the first version of the framework that was &lt;em&gt;compiled&lt;/em&gt; with weakly-linked classes, you can only use them with Mac OS X v10.7+ or iOS 3.1+. *sigh* Before that, you’ll have to use the old way of &lt;code&gt;NSClassFromString&lt;/code&gt; to see if a class is available at runtime.&lt;/p&gt;

&lt;h3 id=&quot;postscript-how-does-it-work&quot;&gt;Postscript: How does it work?&lt;/h3&gt;

&lt;p&gt;In regular code, you know that it’s not possible for the address of a variable to be &lt;code&gt;NULL&lt;/code&gt;. Clearly, if the variable exists, it lives somewhere in memory, and that address is &lt;em&gt;guaranteed&lt;/em&gt; not to be &lt;code&gt;0&lt;/code&gt; by the C standard.&lt;sup id=&quot;fnref:null&quot;&gt;&lt;a href=&quot;#fn:null&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; But with weakly-linked symbols, &lt;em&gt;the variable might not exist,&lt;/em&gt; and so it’s actually quite appropriate that its “address” might be &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How does it work? It’s basically like a C++ reference: a weakly-linked symbol &lt;code&gt;X&lt;/code&gt; actually has a “shadow” symbol with a name like &lt;code&gt;X$non_lazy_ptr&lt;/code&gt;. Any time you refer to &lt;code&gt;X&lt;/code&gt;, the compiler replaces it internally with &lt;code&gt;*X$non_lazy_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And how does &lt;code&gt;X$non_lazy_ptr&lt;/code&gt; get its value? When you load your bundle, there are two secret functions called &lt;code&gt;dyld_stub_binding_helper&lt;/code&gt; and &lt;code&gt;__dyld_func_lookup&lt;/code&gt;. My (weak) understanding is that the first one will go through all of your dynamically-linked symbols and use the second one to find the correct addresses to “fill in” the “shadow pointers”.&lt;/p&gt;

&lt;p&gt;It’s basically a very small form of self-modifying code, but it makes weak linking possible. Pretty cool, huh?&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:simplified&quot;&gt;

      &lt;p&gt;Of course, this is greatly simplified from what a linker really does. If you’re a CS major, you’ve probably learned at least a little about linkers, and if not, well, chances are you won’t be needing to write your own any time soon. If you’re still looking for an explanation, &lt;a href=&quot;http://thecoffeedesk.com/news/index.php/2009/06/01/how-linker-works/&quot;&gt;here’s one&lt;/a&gt; that I found on the first page of Google results for “what is a linker”.&lt;a href=&quot;#fnref:simplified&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:null&quot;&gt;

      &lt;p&gt;Well, actually, that might not be true. What’s guaranteed is that if you cast the integer &lt;code&gt;0&lt;/code&gt; to a pointer value, you get the null pointer, and that the null pointer does not point to any valid object, and that the null pointer has a conditional value of &lt;code&gt;false&lt;/code&gt;. But I don’t think it says that the &lt;em&gt;representation&lt;/em&gt; of a null pointer has to be &lt;code&gt;0&lt;/code&gt;, which means that doing a C++ &lt;code&gt;reinterpret_cast&lt;/code&gt; on a &lt;code&gt;0&lt;/code&gt; could conceivably give you a valid pointer. I think.&lt;/p&gt;

      &lt;p&gt;In practice, pretty much everyone uses &lt;code&gt;0&lt;/code&gt; as the representation of null pointers.&lt;a href=&quot;#fnref:null&quot; rel=&quot;reference&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</content>
	</entry>
	
	
</feed>


