home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / SimpleService / ServiceTest.m < prev    next >
Text File  |  1997-01-12  |  2KB  |  73 lines

  1. /*
  2.  * Services menu example. This file provides two services; one with no return value
  3.  * (open a file), and the other that takes a string and returns a capitalized version.
  4.  *
  5.  * Author: Ali T. Ozer, NeXT Software, Inc.
  6.  * Written Nov '96.
  7.  *
  8.  * You may freely copy, distribute and reuse the code in this example.
  9.  * NeXT disclaims any warranty of any kind, expressed or implied, as to its
  10.  * fitness for any particular use.
  11.  */
  12.  
  13.  
  14. #import "ServiceTest.h"
  15.  
  16. @implementation ServiceTest
  17.  
  18. /* This is an example of a service which doesn't return a value...
  19. */
  20. - (void)doOpenFileService:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error {
  21.     NSString *pboardString;
  22.     NSArray *types;
  23.  
  24.     types = [pboard types];
  25.  
  26.     if (![types containsObject:NSStringPboardType] || !(pboardString = [pboard stringForType:NSStringPboardType])) {
  27.         *error = NSLocalizedString(@"Error: Pasteboard doesn't contain a string.",
  28.                    @"Pasteboard couldn't give string.");
  29.         return;
  30.     }
  31.  
  32.     if (![[NSWorkspace sharedWorkspace] openFile:pboardString]) {
  33.         *error = [NSString stringWithFormat:NSLocalizedString(@"Error: Couldn't open file %@.",
  34.                    @"Couldn't perform service operation."), pboardString];
  35.         return;
  36.     }
  37.  
  38.     return;
  39. }
  40.  
  41. /* This service returns a value; simply the capitalized version of the provided string...
  42. */
  43. - (void)doCapitalizeService:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error {
  44.     NSString *pboardString;
  45.     NSString *newString;
  46.     NSArray *types;
  47.  
  48.     types = [pboard types];
  49.  
  50.     if (![types containsObject:NSStringPboardType] || !(pboardString = [pboard stringForType:NSStringPboardType])) {
  51.         *error = NSLocalizedString(@"Error: Pasteboard doesn't contain a string.",
  52.                    @"Pasteboard couldn't give string.");
  53.         return;
  54.     }
  55.  
  56.     newString = [pboardString capitalizedString];
  57.  
  58.     if (!newString) {
  59.         *error = NSLocalizedString(@"Error: Couldn't capitalize string %@.",
  60.                    @"Couldn't perform service operation.");
  61.         return;
  62.     }
  63.  
  64.     /* We now return the capitalized string... */
  65.     types = [NSArray arrayWithObject:NSStringPboardType];
  66.     [pboard declareTypes:types owner:nil];
  67.     [pboard setString:newString forType:NSStringPboardType];
  68.  
  69.     return;
  70. }
  71.  
  72. @end
  73.