{"id":1451,"date":"2009-04-29T22:42:23","date_gmt":"2009-04-30T01:42:23","guid":{"rendered":"http:\/\/brockerhoff.net\/bb\/viewtopic.php?p=2694"},"modified":"2010-05-16T19:23:42","modified_gmt":"2010-05-16T22:23:42","slug":"cocoa-musings-pt-6","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2009\/04\/29\/cocoa-musings-pt-6\/","title":{"rendered":"Cocoa musings pt.6"},"content":{"rendered":"<p>Also, yikes. 26 days without posting! I plead temporary insanity brought on by tax filing time, and <a href=\"http:\/\/twitter.com\/rbrockerhoff\">Twitter<\/a> &#8211; the latter being a more convenient outlet for short links and thoughts.<\/p>\n<p>Anyway, taxes are filed and we&#8217;re now having a short working vacation in the hills of Petr\u00f3polis, an old town north of Rio de Janeiro. Dorinha is taking a short <a href=\"http:\/\/www.littleengland.com.br\">English immersion course<\/a> (excellent BTW), and I&#8217;m coding again, yay!<\/p>\n<p>I&#8217;m patching up some loose ends in <a href=\"\/klicko\">Klicko<\/a> in preparation to cloning its preference panel for the next version of <a href=\"\/quay\">Quay<\/a>, as I&#8217;ve mentioned before. While doing that, I&#8217;m also trying to refactor my code into a tighter and more readable form. Some of that might be interesting&#8230;<\/p>\n<p>For instance, the automatic update checker has a dialog button to &#8220;Open System Preferences&#8221; and this should go to the Klicko preference panel. Now, System Preferences may already be running but with another panel selected; in any event, the Klicko panel should be opened and ready for the user to see update details. There are several ways to accomplish this.<\/p>\n<p>Most people probably will consider, at first, writing an AppleScript to open System Preferences and then select the Klicko preferences panel. This is unnecessarily complex, and I&#8217;ve looked at several solutions. The simplest one-liner to do so from Cocoa would be:<\/p>\n<pre><code>[[NSWorkspace sharedWorkspace] openFile:@\"\/full\/path\/to\/my.prefPane\"];<\/code><\/pre>\n<p>There&#8217;s a non-obvious down-side to that: NSPreferencePane is generic and may be implemented by other apps for their preference plug-ins. Someone&#8217;s application might use it <em>and<\/em> declare .prefPane in its Info.plist. This would in my opinion be a mistake, in that double-clicking or running the code above might (or not) open that other app instead of System Preferences!<\/p>\n<p>The solution I finally hit upon uses Launch Services to open the correct application with the preference panel, like this:<\/p>\n<pre><code>FSRef ref;\r\nif (LSFindApplicationForInfo(0, CFSTR(\"com.apple.systempreferences\"), NULL, &amp;ref, NULL)==noErr) {\r\n\u00a0 \u00a0LSApplicationParameters parms = {0,kLSLaunchDefaults,&amp;ref,NULL,NULL,NULL,NULL};\r\n\u00a0 \u00a0NSArray* args = [NSArray arrayWithObject:[NSURL fileURLWithPath:@\"\/full\/path\/to\/my.prefPane\"]];\r\n\u00a0 \u00a0if (LSOpenURLsWithRole((CFArrayRef)args, kLSRolesAll, NULL, &amp;parms, NULL, 0)==noErr) {\r\n\u00a0 \u00a0\u00a0 \u00a0\/\/ success!\r\n\u00a0 \u00a0}\r\n}<\/code><\/pre>\n<p>This code first finds the System Preferences app by its bundle ID, and makes a FSRef for it. The FSRef is then pointed to from the LSApplicationParameters structure, and passed to LSOpenURLsWithRole; this will run System Preferences if it&#8217;s not already running, and tell it to open the panel.<\/p>\n<p>It&#8217;s tempting to pass the panel&#8217;s path as an argument inside of LSApplicationParameters. This does indeed work if System Preferences is not already running, but unfortunately it&#8217;s ignored if it is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Also, yikes. 26 days without posting! I plead temporary insanity brought on by tax filing time, and Twitter &#8211; the latter being a more convenient outlet for short links and thoughts. Anyway, taxes are filed and we&#8217;re now having a short working vacation in the hills of Petr\u00f3polis, an old town north of Rio de [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,19,8],"tags":[26,28,27],"class_list":["post-1451","post","type-post","status-publish","format-standard","hentry","category-dev","category-software","category-travel","tag-cocoa","tag-klicko","tag-quay"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-np","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=1451"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1451\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=1451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=1451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=1451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}