CocoaDev

Edit AllPages

I’d like to write a command-line tool to invoke Cocoa system services – the things usually found in the Services Menu. It seems easy to invoke them using the function

BOOL NSPerformService(NSString *serviceItem, NSPasteboard *pboard)

in the application kit. My problem is in determining which services are available. I’ve scoured the Apple docs but can’t find any way to extract the list of service names. Does anyone know how to do this? Any help appreciated.

Use (NSArray *)CFServiceControllerCopyServicesEntries().

Example code (from DavidMcCabe, not the original questioner). It would be great if somebody could explain the questions in the comments.

The first comment indicates that the specified service has no menu item label, and the commented out code passes the message (@”NSMessage” key refers to a string?) instead of the menu label. The ‘else’ comment says it would be nice to know if the service is enabled, and we might want to get the object refered to by the @”NSSendTypes” key, which is an array holding type names which the service accepts. Now that I think about it, maybe the service system could be wrapped in a class which makes it easier to access and understand this stuff. I just this very minute looked at apple docs “System Services” to find this out, the documentation is a little obtuse, but seems comprehensive… JeremyJurksztowicz


Hi, I’m back. I just whipped this up on a windows notepad clone at work. I looked at the online documentation, but since It’s quitting time in a few minutes I don’t feel like combing through the code now. I have become so dependant on syntax coloring. How did people do it in black and white? There is certainly a better way to do this, but I had fun at work today, so, who cares? -JeremyJurksztowicz

// Copyright (C) Jeremy Jurksztowicz, 2008 // Free to do anything you like with this code, including take credit for it, // but that would be really petty and immature. You’re a nice person, right? /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// enum ServiceInputMechanism { ServiceInput_Standard, ServiceInput_UnixStdio, ServiceInput_MapFile }; /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// NSArray * SystemServices ( ); /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @interface SystemService { NSDictionary * properties; } /////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

@end /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// NSArray * SystemServices ( ) { NSArray * services = (NSArray *)CFServiceControllerCopyServicesEntries(); if(services) { NSMutableArray * result = [NSMutableArray arrayWithCapacity:[services count]];

	NSEnumerator * serviceEnum = [services objectEnumerator];
	NSDictionary * serviceInfo;
	while(serviceInfo = [serviceEnum nextObject])
	{
		SystemService * service = [[[SystemService alloc] init] autorelease];
		service->properties = [serviceInfo retain];
	
		[result addObject:service];
	}
	[services release];
	
	return result;
}
return nil; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @implementation ServiceInfo

@end