仅用于文件夹的 Cocoa finder 菜单项
Posted
技术标签:
【中文标题】仅用于文件夹的 Cocoa finder 菜单项【英文标题】:Cocoa finder menu item for folders only 【发布时间】:2013-07-04 14:00:19 【问题描述】:我正在尝试使用服务创建 Finder 上下文菜单项(如此处所述:Writing a Snow Leopard Service for Finder.app)
但是,我希望仅为文件夹添加上下文菜单条目。 每当我将以下代码放入我的 .plist 文件时:
<key>NSServices</key>
<array>
<dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Service Handling Demo</string>
</dict>
<key>NSMessage</key>
<string>handleServices</string> <!-- This specifies the selector -->
<key>NSPortName</key>
<string>Tmp</string> <!-- This is the name of the app -->
<key>NSSendTypes</key>
<array>
<string>NSFilenamesPboardType</string>
</array>
</dict>
</array>
一切正常,我可以在“服务”选项卡(键盘快捷键)中选择我的服务并运行它。
但是,如果我尝试使用文件夹服务:
<key>NSServices</key>
<array>
<dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Service Handling Demo</string>
</dict>
<key>NSMessage</key>
<string>handleServices</string> <!-- This specifies the selector -->
<key>NSPortName</key>
<string>Tmp</string> <!-- This is the name of the app -->
<key>NSSendFileTypes</key>
<array>
<string>public.directory</string>
</array>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
</dict>
</array>
该服务没有出现在键盘快捷方式的菜单中,当然在 finder 中也不可见...
我错过了什么?
【问题讨论】:
【参考方案1】:将以下代码添加到.plist:
<key>NSServices</key>
<array>
<dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Folder Handling Demo</string>
</dict>
<key>NSMessage</key>
<string>handleServices</string> <!-- This specifies the selector -->
<key>NSPortName</key>
<string>Tmp</string> <!-- This is the name of the app -->
<!-- Here we're limiting where the service will appear. -->
<key>NSRequiredContext</key>
<dict>
<key>NSTextContent</key>
<string>FilePath</string>
</dict>
<!-- This service is only really useful from the Finder. So
we want the finder only to send us the URI "public.directory"
which *will* include packages (on the off-chance you want to
see the full package directory name) -->
<key>NSSendFileTypes</key>
<array>
<!-- Check out "System-Declared Uniform Type Identifiers"
in the Apple documentation for the various UTI types.
In this example, all we want is a directory, which is
a super-type to other types (e.g. public.folder) -->
<string>public.folder</string>
</array>
</dict>
并且该服务将出现在服务列表中的“文件和文件夹”组下(键盘快捷键选项卡)。
【讨论】:
以上是关于仅用于文件夹的 Cocoa finder 菜单项的主要内容,如果未能解决你的问题,请参考以下文章
当用户从 finder 打开 Cocoa 应用程序时打开一个窗口,而应用程序都准备好打开