Call the SmartWrap™ OSAX

Below is sample source code for calling the SmartWrap OSAX (AppleScript Extension) from your C or C++ program. Be sure the OSAX is installed in your System Folder:Scripting Extensions Folder and that you have rebooted.

Handle h: Input and Output Handle.

Boolean WithQuotes: Specify whether or not quote level should be maintained by the SmartWrap plugin.

SmartWrapWithOSAX Source Code


OSErr SmartWrapWithOSAX(Handle h)
{
  if (NULL == h || NULL == *h) {
    return nilHandleErr;
  }
  
  OSErr err = noErr;
  
  do {

    // Create "target" descriptor for the SmartWrap OSAX.
    AEAddressDesc Target = { 0 };
    {
      // Send this special event to the Finder to get it to load scripting additions.
      // OSAXen are owned by the Finder
      const OSType kWrapSig = 'MACS'; 
      err = ::AECreateDesc(typeApplSignature, &kWrapSig, sizeof(OSType), &Target);
      if (err) { break; }
      
      // Create the apple event with the above descriptor set as target.
      AppleEvent WrapEvent = { 0 };
      err = ::AECreateAppleEvent(kASAppleScriptSuite, kGetAEUT, &Target, kAutoGenerateReturnID, kAnyTransactionID, &WrapEvent);
      if (err) { break; }
      
      // Send the event and get the reply.
      AppleEvent Reply = { 0 };
      err = ::AESend(&WrapEvent, &Reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
      if (err) { break; }
    }
  
    // Check input size.
    long InputSize = ::GetHandleSize(h);
    err = ::MemError();
    if (err) { break; }
    
    // Create the apple event with the above descriptor set as target.
    AppleEvent WrapEvent = { 0 };
    err = ::AECreateAppleEvent('aevt', 'shs9', &Target, kAutoGenerateReturnID, kAnyTransactionID, &WrapEvent);
    if (err) { break; }
    
    // Add input data parameter.
    err = ::AEPutParamPtr(&WrapEvent, keyDirectObject, typeUnicodeText, *h, InputSize);
    if (err) { break; }
    
    // Add key combination boolean parameter.
    UInt32 keyCombo = 0;
    err = ::AEPutParamPtr(&WrapEvent, 'shsQ', typeEnumerated, &keyCombo, sizeof(keyCombo));
    if (err) { break; }
    
    // Send the event and get the reply.
    AppleEvent Reply = { 0 };
    err = ::AESend(&WrapEvent, &Reply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
    if (err) { break; }
    
    // Peek the size of the reply data.
    DescType TypeCode = 0;
    Size     DataSize = 0;
    err = ::AESizeOfParam(&Reply, keyDirectObject, &TypeCode, &DataSize);
    if (err) { break; }
    
    // Resize to receive reply data.
    ::SetHandleSize(h, DataSize);
    err = ::MemError();
    if (err) { break; }
    
    // Move the data from the reply to the output handle.
    Size ResultSize = 0;
    err = ::AEGetParamPtr(&Reply, keyDirectObject, typeUnicodeText, &TypeCode, *h, DataSize, &ResultSize);
    if (err) { break; }
  
  } while (false);
  
  return err;
}