home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / ASTcl 1.0 / ASTcl-1.0 / ASTcl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  1.7 KB  |  75 lines

  1. /*
  2.  * ASTcl -- DoAppleScript package for MacTcl 7.5.1
  3.  * This code adds the command "DoAppleScript <script>" to MacTcl.
  4.  *
  5.  * ASTcl.c -- package initialization and command handler
  6.  * Written on 960927.
  7.  * 
  8.  * Copyright (c) 1996 by Theodore C. Belding
  9.  * University of Michigan Program for the Study of Complex Systems
  10.  * <mailto:Ted.Belding@umich.edu>
  11.  * <http://www-personal.engin.umich.edu>
  12.  * 
  13.  * This code is freeware.
  14.  */
  15.  
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. #include "tcl.h"
  20. #include "ASTcl.h" 
  21. #include "ASTclUtils.h"
  22.  
  23. /* maximum length of result string */
  24. #define kMaxResultLen 256
  25.  
  26. static int DoAppleScriptCmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
  27.         
  28. /*
  29.  * Package initialization
  30.  */
  31.   
  32. int ASTcl_Init(Tcl_Interp *interp) {
  33.     /* add DoAppleScript command into Tcl interpreter */
  34.     Tcl_CreateCommand(interp, "DoAppleScript", DoAppleScriptCmd, NULL, NULL);
  35.     
  36.     /* initialize AppleScript OSA component */
  37.     if (OSAScriptInit()) {
  38.         return TCL_ERROR;
  39.     }
  40.     else
  41.         return TCL_OK;    
  42. }
  43.  
  44.  
  45. /*
  46.  * DoAppleScript command handler 
  47.  */
  48.  
  49. static int DoAppleScriptCmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
  50. {
  51.     char* script;
  52.     char* result = calloc(kMaxResultLen, sizeof(char));
  53.     
  54.     /* usage: DoAppleScript <script> */
  55.     if (argc != 2) {
  56.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  57.                 " script\"", (char *) NULL);
  58.         return TCL_ERROR;
  59.     }
  60.     script = argv[1];
  61.     
  62.     /* compile and execute script */
  63.     if (ProcessScript(script, result)) {
  64.         /* get error result */
  65.         Tcl_SetResult(interp, result, TCL_DYNAMIC);
  66.         return TCL_ERROR;
  67.     }
  68.     else {
  69.         /* set result */
  70.         Tcl_SetResult(interp, result, TCL_DYNAMIC);
  71.         return TCL_OK;
  72.     }
  73. }
  74.  
  75.