{"id":137,"date":"2006-05-28T17:14:16","date_gmt":"2006-05-29T00:14:16","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/137\/pain-in-the-nib"},"modified":"2006-05-28T17:42:56","modified_gmt":"2006-05-29T00:42:56","slug":"pain-in-the-nib","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/137\/pain-in-the-nib","title":{"rendered":"Pain in the Nib"},"content":{"rendered":"<style type=\"text\/css\"><!-- .caption { border-style:dashed; border-width:1px; border-color:#BBBBBB; margin-left:20px; padding:10px;}--><\/style>\n<p>\nAnybody who&#8217;s spent time with Apple&#8217;s Interface Builder (IB) application knows that it&#8217;s an amazingly intuitive, powerful tool for designing user interfaces. It&#8217;s also a major pain in the ass.\n<\/p>\n<p>\nWhile Xcode has improved by leaps and bounds over the past few years, IB has struggled to keep up. For the most part, it seems that changes to IB have been the bare minimum that would placate the masses (and management). IB had to support Carbon, so they tacked on Carbon support. IB had to support Cocoa Bindings, so they added a new inspector pane. There have been some really cool improvements such as the &#8220;Compatibility Checking,&#8221; but the number of frustrating shortcomings leave me always hoping that a major renovation will soon come.\n<\/p>\n<p>\nMy problems with IB wouldn&#8217;t be nearly so bad if there were viable workarounds. Many of my criticisms stem from its inability to effectively edit multiple items at once. Many times I find myself repeatedly setting the same properties on an item, or typing highly similarly but mildly deviating bindings into a key value field. The problems I face in IB all the time are the kinds of problems I would have long ago automated, if only there was an easy way.\n<\/p>\n<p>\nNibtool is the command-line counterpart to IB, and new programmers on the Mac often assume that it offers exactly the kind of flexibility I have been wishing for. Here we have a tool that dumps a text version of the objects, classes, hierarchy, and connections in a nib file. Hallelujah! Once I dump it to a text file, I can run some shell script or regular expression replacement on the file and pop it back into the nib, right? Nope, sorry! The man page for nibtool lists only one bug, and it&#8217;s a big one:\n<\/p>\n<p><div class=\"caption\">You cannot regenerate a nib file using the output of nibtool.<\/div>\n<\/p>\n<p>\nThe fact that this is listed as a bug has always given me hope that it would one day be fixed. Unfortunately, it appears to be getting fixed at the same pace as all the other bugs and quirks of IB. But when Michael McCracken posted <a href=\"http:\/\/michael-mccracken.net\/wp\/?p=46\">this entry<\/a> describing a happy discovery in the latest Xcode release notes, I shared his cautious optimism. Nibtool has been updated, and it includes a new pair of options for &#8220;importing&#8221; and &#8220;exporting&#8221; object properties from and to an existing nib file:\n<\/p>\n<p><div class=\"caption\">Nibtool can extract properties into a plist format that can be edited and then reimported into the nib using the new &#8211;export (-e) and &#8211;import (-i) flags.<\/div>\n<\/p>\n<p>\nIs this it? The holy grail? I have played around with the new nibtool a little bit, and all I can say is <em>we&#8217;re closer<\/em>, but this is by no means an easy or complete mechanism for manipulating the contents of a nib file. The release notes and man page are so vague that it&#8217;s difficult to even figure out how to use the new options. I have mostly deciphered the new behavior, and hopefully can clarify things for you by paraphrasing what the new options do.\n<\/p>\n<p>\nIn each case, the effects of the tool are only to manipulate the <em>instance objects<\/em> in the particular nib file. The functionality is therefore useless for adjusting things like connections, hierarchy, etc. Think of the nibtool import and export functions as shorthand for &#8220;iterate the objects in my nib and do keyed value reading or writing on them.&#8221; I think one of the confusions is that the PLISTFILE argument in each case is an <em>input<\/em> file to nibtool. Output is always to the standard output.\n<\/p>\n<p>\n<strong>nibtool -e<\/strong> &#8211; Takes as input a special-format plist file that specifies a list of Objective-C class names and associated keypaths for values that should be fetched. For all objects in the nib that are of the specified class, this option produces as output an plist file containing the <em>current<\/em> keyed values for whatever keys you specify in the PLISTFILE.\n<\/p>\n<p>\n<strong>nibtool -i<\/strong> &#8211; Takes as input a plist file <em>of the format produced by nibtool -e<\/em>. For every object specified in the input plist, this option essentially looks up the object in the nib and sets its value as specified in the plist.\n<\/p>\n<p>\nSo in a nutshell, you use nibtool -e to fetch a bunch of values as a text file. Tweak the text file as you see fit, and feed it back in with nibtool -i.  A major drawback to nibtool -e is that if any object you specify doesn&#8217;t respond to the particular key, then the whole deal is called off. So you can&#8217;t for instance ask for &#8220;bounds&#8221; of all &#8220;NSObject&#8221; and hope to get just the objects in your nib that actually have a bounds property. You have to make sure that for whatever class you specify, <em>every instance<\/em> is key-value compliant for the given keypath.\n<\/p>\n<p>\nSo what is the magic format of the first input plist? It&#8217;s not too special, and is documented in the nibtool man page, but it is still clunky enough to limit casual use. You basically cannot use nibtool -e for anything useful unless you&#8217;ve already taken the time to put together a suitable &#8220;input plist&#8221; describing what it is you&#8217;re looking for.  I knew that at the very least I was going to need something to make the input plist easy, so I started with a small python script that produces a plist for the simplest possible fetch: &#8220;given one class name and one keypath, fetch the values for all suitable objects in the nib.&#8221; This is ugly but it&#8217;s my very first python script. Cut me some slack. (Thanks to <a href=\"http:\/\/gusmueller.com\/blog\/\">Gus Mueller<\/a> for telling me about the os.popen() function, and to <a href=\"http:\/\/bdash.net.nz\/\">Mark Rowe<\/a> for teaching me about Python&#8217;s magic triple-quoting).\n<\/p>\n<div class=\"caption\">\n<pre>\n#!\/usr\/bin\/env python\n\nimport sys, os;\n\ndef usage():\n    print &#39;usage: %s <nibfile> <objcclassname> <keypath>&#39; % sys.argv[0]\n    print &#39;produces a PLIST file from <nibfile> suitable for editing&#39;\n    print &#39;keyPath value of all objects of class <objcclassname>&#39;\n        \n# Mild validation of the arguments\nif len(sys.argv) != 4:\n   usage()\n   sys.exit(1)\n\n# Construct a suitable temporary plist for the desired class and keyPath \nnewXML = &#39;&#39;&#39;\\\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>\n&lt;!DOCTYPE plist PUBLIC &quot;-\/\/Apple Computer\/\/DTD PLIST 1.0\/\/EN&quot;\n &quot;http:\/\/www.apple.com\/DTDs\/PropertyList-1.0.dtd&quot;>\n&lt;plist version=&quot;1.0&quot;>\n  &lt;dict>\n    &lt;key>%s&lt;\/key>\n    &lt;array>&lt;string>%s&lt;\/string>&lt;\/array>\n  &lt;\/dict>\n&lt;\/plist>&#39;&#39;&#39; % (sys.argv[2], sys.argv[3])\n\n# write it to a temporary file\nxmlFileName = &#39;\/tmp\/com.red-sweater.pythonClassValueHack.plist&#39;\noutput = open(xmlFileName, &#39;w&#39;)\noutput.write(newXML)\noutput.close()\n\n# now run nibtool feeding it the xml file and pointing it at the nib\nmyResults = os.popen(&#39;\/usr\/bin\/nibtool -e &#39; + xmlFileName + &#39; &#39; + sys.argv[1])\nsys.stdout.writelines(myResults.readlines())\n<\/objcclassname><\/nibfile><\/keypath><\/objcclassname><\/nibfile><\/pre>\n<\/div>\n<p>\nPaste this code into a python text file and run it from the command line. I called my NibValFetch.py. Now when I cruise over to one of my nib files, I can do something like this:\n<\/p>\n<p><div class=\"caption\">\n.\/NibValFetch.py Preferences.nib NSTextField frame > hacking.plist\n<\/div>\n<\/p>\n<p>\nThat is: get all the NSTextFields in Preferences.nib, and ask for their frame. The file &#8220;hacking.plist&#8221; now contains a list of all the NSTextField objects in my nib, along with a text representation of the frame for said field. Now if I wanted to make every text field 2 pixels wider or something, I could write a script to modify the hacking.plist file, and then feed that into nibtool -i.\n<\/p>\n<p>\nI don&#8217;t really have time to delve into this much more right now. I&#8217;m not even sure the new functionality will prove very useful, but I figured that somebody out there will find a good use for it if nudged in the right direction. I hope this introduction to the new functionality clears up what it can and can&#8217;t do, as well as giving you a clue as to how you might go about leveraging it. If you think up any really clever hacks with it, please be sure to share in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anybody who&#8217;s spent time with Apple&#8217;s Interface Builder (IB) application knows that it&#8217;s an amazingly intuitive, powerful tool for designing user interfaces. It&#8217;s also a major pain in the ass. While Xcode has improved by leaps and bounds over the past few years, IB has struggled to keep up. For the most part, it seems [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,20,5],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-hacking","category-xcode"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}