I'm trying to make an app work as a service in Snow Leopard. I think I've followed the instructions in the Apple Service Implementation Guide, and it *almost* works. I can fire up TextEdit, type some text, highlight it with the mouse, and invoke my app via either the TextEdit->Services menu or a key sequence. My app gets launched and the proper instance method gets called. The problem is that the method can't get the contents of the pasteboard.
The relevant code fragment, mostly stolen from the Guide, follows. I added a several debugging lines, but they haven't enlightened me.
Code:
- (void)lookupTerm:(NSPasteboard *)pboard
userData:(NSString *)userData error:(NSString **)error {
NSLog(@"lookupTerm: called (Cool!)\n");
// Test for strings on the pasteboard.
NSArray *classes = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
NSString *pname = [pboard name];
NSLog(@"lookupTerm: pname = \"%@\"\n", pname);
NSArray *parray = [pboard pasteboardItems];
NSLog(@"lookupTerm: parray count = %d\n", [parray count]);
NSLog(@"lookupTerm: classes count = %d\n", [classes count]);
NSLog(@"lookupTerm: options count = %d\n", [options count]);
NSLog(@"lookupTerm: classes[0] = %@%\n", classes[0]);
if (![pboard canReadObjectForClasses:classes options:options]) {
*error = NSLocalizedString(@"Error: lookupTerm couldn't lookup text.",
@"couldn't get string from pasteboard");
NSLog(@"lookupTerm: couldn't get string from pasteboard\n");
return;
}
<more code here>
When I test it as described above, this appears in my Console:
lookupTerm: called (Cool!)
lookupTerm: pname = "CFPasteboardUnique-22474b5480df"
lookupTerm: parray count = 0
lookupTerm: classes count = 1
lookupTerm: options count = 0
lookupTerm: classes[0] = NSCFArray
lookupTerm: couldn't get string from pasteboard
FWIW, here's the relevant part of my Info.plist file:
Code:
<key>NSServices</key>
<array>
<dict>
<key>NSMessage</key>
<string>lookupTerm</string>
<key>NSPortName</key>
<string>PetesLookup</string>
<key>NSKeyEquivalent</key>
<dict>
<key>Key equivalent (with command and shift)</key>
<string>7</string>
</dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Pete's lookup</string>
</dict>
</dict>
</array>
The "Testing" section of the Apple Services Implementation Guide suggests using NSDebugServices, so I did, and invoked my service, and I saw:
Code:
$ /Applications/TextEdit.app/Contents/MacOS/TextEdit -NSDebugServices edu.ucar.PetesLookup
2010-02-09 14:05:29.588 TextEdit[26940:903] NSDebugServices=edu.ucar.PetesLookup
Pete's lookup (edu.ucar.PetesLookup) is explicitly enabled in the services menu and enabled in the context menu, in preferences.
Pete's lookup (edu.ucar.PetesLookup) has a custom key equivalent: <NSKeyboardShortcut: 0x1004e0a10 (??7)>.
Pete's lookup (edu.ucar.PetesLookup) is explicitly enabled in the services menu and enabled in the context menu, in preferences.
Pete's lookup (edu.ucar.PetesLookup) has a custom key equivalent: <NSKeyboardShortcut: 0x115111270 (??7)>.
Pete's lookup (edu.ucar.PetesLookup) has been approved by default, as it does not contain any required context.
$
The first 3 lines were output when TextEdit started. The last 3 lines appeared when I highlighted some text in TextEdit and hit Shift-Command-7. It all seems good to me, but my app still can't get the contents of the pasteboard.
I'm not sure how to debug this further. Help!
-- Pete