home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Examples / AppKit / SimpleService / ServiceTest.m < prev    next >
Encoding:
Text File  |  1997-01-13  |  2.3 KB  |  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
  21.  userData:(NSString *)data
  22.  error:(NSString **)error
  23.  {
  24.     NSString *pboardString;
  25.     NSArray *types;
  26.  
  27.     types = [pboard types];
  28.  
  29.     if (![types containsObject:NSStringPboardType] || !(pboardString = [pboard stringForType:NSStringPboardType])) {
  30.         *error = NSLocalizedString(@"Error: Pasteboard doesn't contain a string.",
  31.                    @"Pasteboard couldn't give string.");
  32.         return;
  33.     }
  34.  
  35.  
  36.     if (![[NSWorkspace sharedWorkspace] openFile:pboardString]) {
  37.         *error = [NSString stringWithFormat:NSLocalizedString(@"Error: Couldn't open file %@.",
  38.                    @"Couldn't perform service operation."), pboardString];
  39.         return;
  40.     }
  41.  
  42.     return;
  43. }
  44.  
  45. /* This service returns a value; simply the capitalized version of the provided string...
  46. */
  47. - (void)doCapitalizeService:(NSPasteboard *)pboard
  48.  userData:(NSString *)data
  49.  error:(NSString **)error
  50.  {
  51.  
  52.     NSString *pboardString;
  53.     NSString *newString;
  54.     NSArray *types;
  55.  
  56.     types = [pboard types];
  57.  
  58.     if (![types containsObject:NSStringPboardType] || !(pboardString = [pboard stringForType:NSStringPboardType])) {
  59.         *error = NSLocalizedString(@"Error: Pasteboard doesn't contain a string.",
  60.                    @"Pasteboard couldn't give string.");
  61.         return;
  62.     }
  63.  
  64.  
  65.     newString = [pboardString capitalizedString];
  66.  
  67.     if (!newString) {
  68.         *error = NSLocalizedString(@"Error: Couldn't capitalize string %@.",
  69.                    @"Couldn't perform service operation.");
  70.         return;
  71.     }
  72.  
  73.     /* We now return the capitalized string... */
  74.     types = [NSArray arrayWithObject:NSStringPboardType];
  75.     [pboard declareTypes:types owner:nil];
  76.     [pboard setString:newString forType:NSStringPboardType];
  77.  
  78.     return;
  79. }
  80.  
  81. @end
  82.