NSService - items added to the context menu doesn't show up on mac os x

Viewed 537

I wish that the context menu shows services of my app. For this purpose, I wrote the following code.

According to this example: Services Implementation Guide, I created a handlers for my services in AppDelegate class.

- (void)ServiceHandler:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
    NSLog(@"I clicked");
}

- (void)ServiceHandler2:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
    NSLog(@"I clicked");
}

- (void)ServiceHandler3:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
    NSLog(@"I clicked");
}

Then I set up AppDelegate as service provider in applicationDidFinishLaunching: method and installed a services as follows:

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {   
    [NSApp setServicesProvider:self];
    NSUpdateDynamicServices();
}

And then I created all my services in info.plist as follows:

<key>NSServices</key>
    <array>
        <dict>
            <key>NSKeyEquivalent</key>
            <dict>
                <key>default</key>
                <string>E</string>
            </dict>
            <key>NSMenuItem</key>
            <dict>
                <key>default</key>
                <string>Item 1</string>
            </dict>
            <key>NSMessage</key>
            <string>ServiceHandler</string>
            <key>NSPortName</key>
            <string>MyApp</string>
            <key>NSRequiredContext</key>
            <dict>
                <key>NSApplicationIdentifier</key>
                <string>com.apple.finder</string>
            </dict>
            <key>NSReturnTypes</key>
            <array>
                <string>NSStringPboardType</string>
            </array>
            <key>NSSendFileTypes</key>
            <array>
                <string>public.item</string>
            </array>
            <key>NSServiceDescription</key>
            <string>Description of this service</string>
        </dict>
        <dict>
            <key>NSKeyEquivalent</key>
            <dict>
                <key>default</key>
                <string>H</string>
            </dict>
            <key>NSMenuItem</key>
            <dict>
                <key>default</key>
                <string>Item 2</string>
            </dict>
            <key>NSMessage</key>
            <string>ServiceHandler3</string>
            <key>NSPortName</key>
            <string>MyApp</string>
            <key>NSRequiredContext</key>
            <dict>
                <key>NSApplicationIdentifier</key>
                <string>com.apple.finder</string>
            </dict>
            <key>NSReturnTypes</key>
            <array>
                <string>NSStringPboardType</string>
            </array>
            <key>NSSendFileTypes</key>
            <array>
                <string>public.item</string>
            </array>
            <key>NSServiceDescription</key>
            <string>Description of this service</string>
        </dict>
        <dict>
            <key>NSKeyEquivalent</key>
            <dict>
                <key>default</key>
                <string>Z</string>
            </dict>
            <key>NSMenuItem</key>
            <dict>
                <key>default</key>
                <string>Item 3</string>
            </dict>
            <key>NSMessage</key>
            <string>ServiceHandler2</string>
            <key>NSPortName</key>
            <string>MyApp</string>
            <key>NSRequiredContext</key>
            <dict>
                <key>NSApplicationIdentifier</key>
                <string>com.apple.finder</string>
            </dict>
            <key>NSReturnTypes</key>
            <array>
                <string>NSStringPboardType</string>
            </array>
            <key>NSSendFileTypes</key>
            <array>
                <string>com.MyApp.anytype</string>
            </array>
            <key>NSServiceDescription</key>
            <string>Description of this service</string>
        </dict>
    </array>
    <key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.item</string>
            </array>
            <key>UTTypeDescription</key>
            <string>extension for anytype</string>
            <key>UTTypeIconFile</key>
            <string></string>
            <key>UTTypeIdentifier</key>
            <string>com.MyApp.anytype</string>
            <key>UTTypeReferenceURL</key>
            <string>http://anytype.MyApp.com</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>anytype</string>
                </array>
            </dict>
        </dict>
    </array>

I registered my new type of file by:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /path/to/MyApp

Of course I ran this command too:

/System/Library/CoreServices/pbs -update
/System/Library/CoreServices/pbs -flush
/System/Library/CoreServices/pbs -dump_cache

And I tried to debug NSServices by:

/Applications/TextEdit.app/Contents/MacOS/TextEdit -NSDebugServices com.MyApp

The result of this command was ok.

2016-03-14 13:21:16.697 TextEdit[885:6374] NSDebugServices=com.MyApp
Item 1 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.
Item 2 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.
Item 3 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.

Everything is enabled and I can see them in preferences with selected checkboxes, but still this items are not visible in context menu. I really don't know where the problem is. Any Ideas ?

EDIT: I changed every public.item on NSFilenamesPboardType a now is working, but only two of them. I still can't see in context menu service with my own extension. Anybody knows why public.item is wrong ?

EDIT2: Solved. I had a problem with identifier of file extension. I changed it and now it works.

0 Answers
Related