home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connectio…eloper Series 2005 March / Dev.CD Mar 05.iso / What's New / Technical Notes and Q&As / ADC Reference Library / qa / qa2001 / downloads / qa1026.hqx / qa1026 / RunScript.c next >
Encoding:
C/C++ Source or Header  |  2001-04-11  |  5.6 KB  |  133 lines

  1. /*
  2.     File: RunScript.c
  3.     
  4.     Description:
  5.         Simple routines for running AppleScripts inside of applications.  Clients
  6.     should either link with CarbonLib or weak link against AppleScriptLib.
  7.  
  8.     Copyright:
  9.         © Copyright 2001 Apple Computer, Inc. All rights reserved.
  10.     
  11.     Disclaimer:
  12.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  13.         ("Apple") in consideration of your agreement to the following terms, and your
  14.         use, installation, modification or redistribution of this Apple software
  15.         constitutes acceptance of these terms.  If you do not agree with these terms,
  16.         please do not use, install, modify or redistribute this Apple software.
  17.  
  18.         In consideration of your agreement to abide by the following terms, and subject
  19.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  20.         copyrights in this original Apple software (the "Apple Software"), to use,
  21.         reproduce, modify and redistribute the Apple Software, with or without
  22.         modifications, in source and/or binary forms; provided that if you redistribute
  23.         the Apple Software in its entirety and without modifications, you must retain
  24.         this notice and the following text and disclaimers in all such redistributions of
  25.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  26.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  27.         Apple Software without specific prior written permission from Apple.  Except as
  28.         expressly stated in this notice, no other rights or licenses, express or implied,
  29.         are granted by Apple herein, including but not limited to any patent rights that
  30.         may be infringed by your derivative works or by other works in which the Apple
  31.         Software may be incorporated.
  32.  
  33.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  34.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  35.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  37.         COMBINATION WITH YOUR PRODUCTS.
  38.  
  39.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  40.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  41.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  43.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  44.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  45.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46.  
  47.     Change History (most recent first):
  48.         Wed, Apr 10, 2001 -- created
  49. */
  50.  
  51. #include "RunScript.h"
  52.  
  53. #if ! TARGET_API_MAC_CARBON
  54.  
  55. #include <OSA.h>
  56. #include <AppleScript.h>
  57. #include <Gestalt.h>
  58.  
  59. #endif
  60.  
  61. #include <string.h>
  62.  
  63.  
  64.     /* AppleScriptAvailable returns true if AppleScript is available
  65.     and the routines defined herein can be called. */
  66. Boolean AppleScriptAvailable(void) {
  67.     long response;
  68.     if (Gestalt(gestaltAppleScriptAttr, &response) != noErr) response = 0; 
  69.     return ((response & (1<<gestaltAppleScriptPresent)) != 0);
  70. }
  71.  
  72.  
  73.     /* LowRunAppleScript compiles and runs an AppleScript
  74.     provided as text in the buffer pointed to by text.  textLength
  75.     bytes will be compiled from this buffer and run as an AppleScript
  76.     using all of the default environment and execution settings.  If
  77.     resultData is not NULL, then the result returned by the execution
  78.     command will be returned as typeChar in this descriptor record
  79.     (or typeNull if there is no result information).  If the function
  80.     returns errOSAScriptError, then resultData will be set to a
  81.     descriptive error message describing the error (if one is
  82.     available).  */
  83. OSStatus LowRunAppleScript(const void* text, long textLength,
  84.                                     AEDesc *resultData) {
  85.     ComponentInstance theComponent;
  86.     AEDesc scriptTextDesc;
  87.     OSStatus err;
  88.     OSAID scriptID, resultID;
  89.         /* set up locals to a known state */
  90.     theComponent = NULL;
  91.     AECreateDesc(typeNull, NULL, 0, &scriptTextDesc);
  92.     scriptID = kOSANullScript;
  93.     resultID = kOSANullScript;
  94.         /* open the scripting component */
  95.     theComponent = OpenDefaultComponent(kOSAComponentType,
  96.                     typeAppleScript);
  97.     if (theComponent == NULL) { err = paramErr; goto bail; }
  98.         /* put the script text into an aedesc */
  99.     err = AECreateDesc(typeChar, text, textLength, &scriptTextDesc);
  100.     if (err != noErr) goto bail;
  101.         /* compile the script */
  102.     err = OSACompile(theComponent, &scriptTextDesc, 
  103.                     kOSAModeNull, &scriptID);
  104.     if (err != noErr) goto bail;
  105.         /* run the script/get the result */
  106.     err = OSAExecute(theComponent, scriptID, kOSANullScript,
  107.                     kOSAModeNull, &resultID);
  108.     if (resultData != NULL) {
  109.         AECreateDesc(typeNull, NULL, 0, resultData);
  110.         if (err == errOSAScriptError) {
  111.             OSAScriptError(theComponent, kOSAErrorMessage,
  112.                         typeChar, resultData);
  113.         } else if (err == noErr && resultID != kOSANullScript) {
  114.             OSADisplay(theComponent, resultID, typeChar,
  115.                         kOSAModeNull, resultData);
  116.         }
  117.     }
  118. bail:
  119.     AEDisposeDesc(&scriptTextDesc);
  120.     if (scriptID != kOSANullScript) OSADispose(theComponent,  scriptID);
  121.     if (resultID != kOSANullScript) OSADispose(theComponent,  resultID);
  122.     if (theComponent != NULL) CloseComponent(theComponent);
  123.     return err;
  124. }
  125.  
  126.     /* SimpleRunAppleScript compiles and runs the AppleScript in the c-style
  127.     string provided as a parameter.  The result returned indicates the success
  128.     of the operation. */
  129. OSStatus SimpleRunAppleScript(const char* theScript) {
  130.     return LowRunAppleScript(theScript, strlen(theScript), NULL);
  131. }
  132.  
  133.