【问题标题】:Cocoa finder menu item for folders only仅用于文件夹的 Cocoa finder 菜单项
【发布时间】: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 中也不可见...

我错过了什么?

【问题讨论】:

    标签: objective-c macos cocoa contextmenu 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>
    

    并且该服务将出现在服务列表中的“文件和文件夹”组下(键盘快捷键选项卡)。

    【讨论】:

      猜你喜欢
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      相关资源
      最近更新 更多