<?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>iVira.name &#187; cocoa</title>
	<atom:link href="http://ivira.name/blog/category/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivira.name/blog</link>
	<description>Mostly about programming, Apple and some random stuff</description>
	<lastBuildDate>Mon, 10 Oct 2011 09:39:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating Finder&#8217;s &#8220;Open With&#8221; Contextual Menu</title>
		<link>http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/</link>
		<comments>http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 15:03:21 +0000</pubDate>
		<dc:creator>vira</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[finder]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ivira.name/blog/?p=196</guid>
		<description><![CDATA[Hello everyone and congrats with spring/Lion/iPad2! I haven&#8217;t written for such a long time and so many things have happened.
Today I want to share with you my experience of creating the analog of &#8220;Open With&#8221; menu, similar to Finder&#8217;s one. I&#8217;ve tried to create the exact copy of this menu: default application, list of other [...]]]></description>
			<content:encoded><![CDATA[<p>Hello everyone and congrats with spring/Lion/iPad2! I haven&#8217;t written for such a long time and so many things have happened.</p>
<div id="attachment_206" class="wp-caption alignright" style="width: 160px"><a href="http://ivira.name/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-13-at-14.31.19.png"><img src="http://ivira.name/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-13-at-14.31.19-150x150.png" alt="Open With Menu" title="Open With Menu" width="150" height="150" class="size-thumbnail wp-image-206" /></a><p class="wp-caption-text">'Open With' Menu</p></div>
<p>Today I want to share with you my experience of creating the analog of &#8220;Open With&#8221; menu, similar to Finder&#8217;s one. I&#8217;ve tried to create the exact copy of this menu: default application, list of other apps and &#8220;Other…&#8221; option. I use this menu in the one of our projects &#8211; <a href="http://macpaw.com/machider">MacHider</a> and you have take a look of how it works.</p>
<p>When I&#8217;ve started to implement the menu, I was a bit surprised that this isn&#8217;t common component…After googling I haven&#8217;t found any ready to use solutions. So here is how I&#8217;ve implemented it <img src='http://ivira.name/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-196"></span><br />
To get the list of apps that can open specified file you can use LSCopyApplicationURLsForURL function. The second parameter to this function is role of the application: is it editor or viewer for this file. Default application could be obtained by calling LSGetApplicationForURL.</p>
<p>The core method in the presented below code is <code>openWithMenuItemForFile</code>. It returns NSMenu for specified file path and you can show this menu whenever and wherever you want. In our app I show it when system calls &#8220;validateMenuItem&#8221; for &#8220;Open With&#8221; menu item. Not the best solution, because user don&#8217;t necessary show this menu but it&#8217;s easy to implement and it works. Also I want to note, that building this menu takes some time. So be careful not to freeze UI.</p>

<div class="wp_syntax"><div class="code"><pre class="objc">&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>openSelectedFileWithApplication<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>appPath
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> &lt;get <span style="color: #0000ff;">file</span> path, e.g. from selected table row&gt;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSWorkspace</span> sharedWorkspace<span style="color: #002200;">&#93;</span> openFile<span style="color: #002200;">:</span>filePath withApplication<span style="color: #002200;">:</span>appPath<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">/// menu item action</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>openWithApplicationSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>sender isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span>@<span style="color: #666666;">&quot;Sender should be nsmenuitem&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #0000ff;">return</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>menuItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender;
    <span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span>appPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>menuItem representedObject<span style="color: #002200;">&#93;</span>;
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>appPath<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span>@<span style="color: #666666;">&quot;Could get app path from nsmenuitem represented object&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #0000ff;">return</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>self openSelectedFileWithApplication<span style="color: #002200;">:</span>appPath<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: #0000ff;">void</span><span style="color: #002200;">&#41;</span>openWithApplicationOtherSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">NSOpenPanel</span> <span style="color: #002200;">*</span>panel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSOpenPanel</span> openPanel<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>panel setAllowsMultipleSelection<span style="color: #002200;">:</span>NO<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>panel setCanChooseFiles<span style="color: #002200;">:</span>YES<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>panel setCanChooseDirectories<span style="color: #002200;">:</span>NO<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>panel beginSheetForDirectory<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot;/Applications&quot;</span>
                                               <span style="color: #0000ff;">file</span><span style="color: #002200;">:</span><span style="color: #0000ff;">nil</span>
                                            types<span style="color: #002200;">:</span><span style="color: #0000ff;">nil</span>
                         modalForWindow<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>mainController window<span style="color: #002200;">&#93;</span>
                            modalDelegate<span style="color: #002200;">:</span>self
                            didEndSelector<span style="color: #002200;">:</span><span style="color: #0000ff;">@selector</span><span style="color: #002200;">&#40;</span>chooseAppSheetClosed<span style="color: #002200;">:</span>returnCode<span style="color: #002200;">:</span>contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
                                 contextInfo<span style="color: #002200;">:</span><span style="color: #0000ff;">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: #0000ff;">void</span><span style="color: #002200;">&#41;</span>chooseAppSheetClosed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSOpenPanel</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>panel returnCode<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #002200;">&#41;</span>code contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>useOptions
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>code <span style="color: #002200;">==</span> NSOKButton<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>panel close<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#91;</span>self openSelectedFileWithApplication<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>panel filename<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">/// construct menu item for app</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>menuItemForOpenWithForApplication<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>appName path<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>appPath
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>newAppItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>newAppItem setTitle<span style="color: #002200;">:</span>appName<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>newAppItem setTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>newAppItem setAction<span style="color: #002200;">:</span><span style="color: #0000ff;">@selector</span><span style="color: #002200;">&#40;</span>openWithApplicationSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>newAppItem setRepresentedObject<span style="color: #002200;">:</span>appPath<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>newAppItem setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSWorkspace</span> sharedWorkspace<span style="color: #002200;">&#93;</span> iconForFile<span style="color: #002200;">:</span>appPath<span style="color: #002200;">&#93;</span> imageByScalingToSize<span style="color: #002200;">:</span>NSMakeSize<span style="color: #002200;">&#40;</span><span style="color: #0000dd;">16</span>, <span style="color: #0000dd;">16</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #0000ff;">return</span> newAppItem;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">/// this method return open with menu for specified file</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSMenu</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>openWithMenuItemForFile<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>filePath
<span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">NSMenu</span> <span style="color: #002200;">*</span>subMenu <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenu</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #0000ff;">NSURL</span> <span style="color: #002200;">*</span>fileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>filePath<span style="color: #002200;">&#93;</span>;
   CFArrayRef cfArrayOfApps <span style="color: #002200;">=</span> LSCopyApplicationURLsForURL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span>fileURL, kLSRolesAll<span style="color: #002200;">&#41;</span>;
    CFIndex maxCount <span style="color: #002200;">=</span> <span style="color: #0000dd;">10</span>;
    <span style="color: #0000ff;">NSMutableSet</span> <span style="color: #002200;">*</span>alreadyAdded <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMutableSet</span> setWithCapacity<span style="color: #002200;">:</span>maxCount<span style="color: #002200;">&#93;</span>;
	<span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>cfArrayOfApps <span style="color: #002200;">!=</span> <span style="color: #0000ff;">nil</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		CFIndex count <span style="color: #002200;">=</span> CFArrayGetCount<span style="color: #002200;">&#40;</span>cfArrayOfApps<span style="color: #002200;">&#41;</span>;
        <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>count &gt; maxCount<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            count <span style="color: #002200;">=</span> maxCount;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #ff0000;">//get and add default app</span>
        CFURLRef defaultApp;
        LSGetApplicationForURL<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span>fileURL, kLSRolesAll, <span style="color: #0000ff;">NULL</span>, <span style="color: #002200;">&amp;</span>defaultApp<span style="color: #002200;">&#41;</span>;
        <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>defaultApp<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span>@<span style="color: #666666;">&quot;There is no default App for %@&quot;</span>, filePath<span style="color: #002200;">&#41;</span>;
            <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>noneItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>noneItem setTitle<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot;&lt;None&gt;&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>noneItem setEnabled<span style="color: #002200;">:</span>NO<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span>noneItem<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>noneItem release<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> separatorItem<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span>defaultAppPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>defaultApp path<span style="color: #002200;">&#93;</span>;
            <span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span>defaultAppName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>defaultAppPath lastPathComponent<span style="color: #002200;">&#93;</span> stringByDeletingPathExtension<span style="color: #002200;">&#93;</span> stringByAppendingString<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot; (default)&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>newAppItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self menuItemForOpenWithForApplication<span style="color: #002200;">:</span>defaultAppName path<span style="color: #002200;">:</span>defaultAppPath<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span>newAppItem<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> separatorItem<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>count <span style="color: #002200;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">int</span> index <span style="color: #002200;">=</span> <span style="color: #0000dd;">0</span>; index &lt; count; <span style="color: #002200;">++</span>index<span style="color: #002200;">&#41;</span>
                <span style="color: #002200;">&#123;</span>
                    <span style="color: #0000ff;">NSURL</span> <span style="color: #002200;">*</span>appURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #0000ff;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>CFArrayGetValueAtIndex<span style="color: #002200;">&#40;</span>cfArrayOfApps, index<span style="color: #002200;">&#41;</span>;
                    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>appURL isFileURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
                    <span style="color: #002200;">&#123;</span>
                        <span style="color: #0000ff;">NSString</span> <span style="color: #002200;">*</span>appName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>appURL path<span style="color: #002200;">&#93;</span> lastPathComponent<span style="color: #002200;">&#93;</span> stringByDeletingPathExtension<span style="color: #002200;">&#93;</span>;
                        <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>alreadyAdded containsObject<span style="color: #002200;">:</span>appName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                            appName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>appName stringByAppendingFormat<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot; (%@)&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSBundle</span> bundleWithPath<span style="color: #002200;">:</span>defaultAppPath<span style="color: #002200;">&#93;</span> infoDictionary<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot;CFBundleVersion&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
                        <span style="color: #002200;">&#125;</span>
                        <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>newAppItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self menuItemForOpenWithForApplication<span style="color: #002200;">:</span>appName path<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>appURL path<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
                        <span style="color: #002200;">&#91;</span>alreadyAdded addObject<span style="color: #002200;">:</span>appName<span style="color: #002200;">&#93;</span>;
                        <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span>newAppItem<span style="color: #002200;">&#93;</span>;
                    <span style="color: #002200;">&#125;</span>
                <span style="color: #002200;">&#125;</span>
                <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> separatorItem<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>            
        <span style="color: #002200;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">NSMenuItem</span> <span style="color: #002200;">*</span>otherAppItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #0000ff;">NSMenuItem</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>otherAppItem setTitle<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot;Other…&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>otherAppItem setTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>otherAppItem setAction<span style="color: #002200;">:</span><span style="color: #0000ff;">@selector</span><span style="color: #002200;">&#40;</span>openWithApplicationOtherSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>subMenu addItem<span style="color: #002200;">:</span>otherAppItem<span style="color: #002200;">&#93;</span>;
&nbsp;
	CFRelease<span style="color: #002200;">&#40;</span>cfArrayOfApps<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #0000ff;">return</span> subMenu;
<span style="color: #002200;">&#125;</span></pre></div></div>

<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://www.facebook.com/share.php?u=http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/' title='Save to Facebook' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/facebook.png' style='width:16px; height:16px;' alt='[Facebook] ' ></a> <a href='http://twitter.com/home/?status=Creating Finder&#8217;s &#8220;Open With&#8221; Contextual Menu+http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/' title='Save to Twitter' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/twitter.png' style='width:16px; height:16px;' alt='[Twitter] ' ></a> <a href='http://www.feedburner.com/fb/a/emailFlare?itemTitle=Creating Finder&#8217;s &#8220;Open With&#8221; Contextual Menu&amp;uri=http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/&amp;loc=en_US' title='Email this to a friend' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/email.png' style='width:16px; height:16px;' alt='[Email] ' ></a>  <a title='See more bookmark and sharing options...' href='http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://ivira.name/blog/2011/03/13/creating-open-with-contextual-menu-as-in-finder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cocoa Tip: Filtering NSArray using NSPredicate</title>
		<link>http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/</link>
		<comments>http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 22:13:35 +0000</pubDate>
		<dc:creator>vira</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ivira.name/blog/?p=160</guid>
		<description><![CDATA[Today I&#8217;ve had a small task in one of my projects &#8211; to get subset of elements from NSArray. I&#8217;ve already started writing all this NSEnumerator&#8217;s stuff (need to support 10.4) when I remembered about NSPredicator. So, instead of iterating over an array and finding elements that satisfy some condition and adding them to some [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve had a small task in one of my projects &#8211; to get subset of elements from NSArray. I&#8217;ve already started writing all this NSEnumerator&#8217;s stuff (need to support 10.4) when I remembered about NSPredicator. So, instead of iterating over an array and finding elements that satisfy some condition and adding them to some output array you just need to create predicate and filter your input array with it. Here is the sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc">NSPredicate <span style="color: #002200;">*</span>predicate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSPredicate predicateWithFormat<span style="color: #002200;">:</span>@<span style="color: #666666;">&quot;storeState == 1&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #0000ff;">NSArray</span>  <span style="color: #002200;">*</span>outputArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>inputArray filteredArrayUsingPredicate<span style="color: #002200;">:</span>predicate<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Predicates language seems to be very powerful and simple. But I can&#8217;t tell you anything about performance, need to investigate.</p>
<p>UPDATE If you want to filter array with regular expressions just use a predicate like this one:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;" lang="objc">[NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp];</pre>
<p>Further reading: <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/index.html">Apple Predicates Programming Guide</a></p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://www.facebook.com/share.php?u=http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/' title='Save to Facebook' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/facebook.png' style='width:16px; height:16px;' alt='[Facebook] ' ></a> <a href='http://twitter.com/home/?status=Cocoa Tip: Filtering NSArray using NSPredicate+http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/' title='Save to Twitter' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/twitter.png' style='width:16px; height:16px;' alt='[Twitter] ' ></a> <a href='http://www.feedburner.com/fb/a/emailFlare?itemTitle=Cocoa Tip: Filtering NSArray using NSPredicate&amp;uri=http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/&amp;loc=en_US' title='Email this to a friend' onclick='target="_blank";' rel='nofollow'><img src='http://ivira.name/blog/wp-content/plugins/bookmarkify/email.png' style='width:16px; height:16px;' alt='[Email] ' ></a>  <a title='See more bookmark and sharing options...' href='http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://ivira.name/blog/2010/01/13/cocoa-tip-filtering-nsarray-using-nspredicate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

