home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Puppeteer1.1 / Source / websterPuppet.m < prev    next >
Encoding:
Text File  |  1994-03-23  |  2.4 KB  |  114 lines

  1. /*
  2.  * Puppeteer 1.1
  3.  *
  4.  * Copyright (c) 1994 Primitive Software Ltd.  All rights reserved.
  5.  *
  6.  * Author: Dave Griffiths <dave@prim.demon.co.uk>
  7.  */
  8.  
  9. #import "Puppeteer.h"
  10.  
  11. void
  12. define(id puppet, char *word)
  13. {
  14.     id speaker = [puppet appSpeaker], pb;
  15.     int msgDelivered, fileOpened, length;
  16.     char *data;
  17.  
  18.     /*
  19.      * Unhide Webster. Things don't work properly if the target application is
  20.      * hidden.
  21.      */
  22.     [speaker selectorRPC:"unhide" paramTypes:""];
  23.  
  24.     /*
  25.      * Make Webster the active application.
  26.      */
  27.     [puppet postActivate:YES];
  28.     
  29.     /*
  30.      * Ask Webster to define the word.
  31.      */
  32.     msgDelivered = [speaker openFile:word ok:&fileOpened]; 
  33.     if (msgDelivered != 0) {
  34.         fprintf(stderr, "Could not send message to Webster, code = %d\n",
  35.             msgDelivered);
  36.         return;
  37.     }
  38.     if (fileOpened == NO) {
  39.         fprintf(stderr, "Webster could not open %s\n", word);
  40.         return;
  41.     }
  42.         
  43.     /*
  44.      * Simulate a click in the lower scroll view.
  45.      */
  46.     [puppet postSingleClick:NX_KEYWINDOW flags:0 x:40.0 y:40.0];
  47.     
  48.     /*
  49.      * Now ask Webster to copy the definition to the pasteboard. This is done by
  50.      * cmd-a (select all) followed by cmd-c (copy).
  51.      */
  52.     [puppet postKeyCode:'a' window:NX_KEYWINDOW flags:NX_COMMANDMASK];
  53.     [puppet postKeyCode:'c' window:NX_KEYWINDOW flags:NX_COMMANDMASK];
  54.     [puppet ping];        // Make sure requests have been processed.
  55.     
  56.     /*
  57.      * Finally, read data from the pasteboard.
  58.      */
  59.     pb = [Pasteboard new];
  60.     [pb types];
  61.     if ([pb readType:NXAsciiPboardType data:&data length:&length]) {
  62.         printf("%s", data);
  63.         [pb deallocatePasteboardData:data length:length];
  64.     } else
  65.         fprintf(stderr, "No data on pasteboard!\n");
  66.  
  67. }
  68.  
  69. void
  70. main(argc, argv)
  71. int argc;
  72. char **argv;
  73. {
  74.     int c, errflg = 0;
  75.     id puppet;
  76.     extern int optind;
  77.  
  78.     while ((c = getopt(argc, argv, "")) != EOF)
  79.     switch (c) {
  80.     case '?':
  81.     default:
  82.         errflg++;
  83.         break;
  84.     }
  85.     if (errflg || !argv[optind]) {
  86.         fprintf(stderr, "Usage: websterPuppet word\n");
  87.         exit(2);
  88.     }
  89.     
  90.     /*
  91.      * Create the Webster puppet, launching Webster if necessary.
  92.      */
  93.     puppet = [Puppeteer connectToApp:"Webster" launch:YES];
  94.     if (!puppet) {
  95.         fprintf(stderr, "Could not connect to Webster\n");
  96.         exit(1);
  97.     }
  98.     
  99.     /*
  100.      * Attach the strings. Webster will then be ready to accept events.
  101.      */    
  102.     [puppet attachStrings];
  103.     
  104.     /*
  105.      * Now call Webster to define the word.
  106.      */
  107.     define(puppet, argv[optind]);
  108.  
  109.     /*
  110.      * Release strings. This is necessary for Webster to continue to respond to
  111.      * real user events.
  112.      */
  113.     [puppet releaseStrings];
  114. }