home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / 7Edit 3.1 / Sources / SVAECopy.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  2.7 KB  |  126 lines  |  [TEXT/CWIE]

  1. // SVAECopy.c
  2. //
  3. // 7Edit 3.1d1. Original version by Jon Lansdell and Nigel Humphreys.
  4. // 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1995, all rights reserved.
  6.  
  7. #include "SVAECopy.h"
  8.  
  9. #include "SVEditAEUtils.h"
  10. #include "SVEditWindow.h"        // for DPtrFromWindowPtr()
  11.  
  12. #include "SVAESelect.h"
  13.  
  14. #include <Scrap.h>
  15.  
  16.  
  17. #pragma segment AppleEvent
  18.  
  19. // Handle a copy to scrap e.g 'copy last word of document 1'
  20. // Note that 'copy last word of document 1 to end of document 2' is a kAEClone event
  21.      
  22. pascal OSErr    DoCopy(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  23. {
  24. #pragma unused (reply, refcon)
  25.  
  26.     AEDesc        directObj = {typeNull, NULL};
  27.     TextToken    aTextToken;
  28.     short        ignore;
  29.     OSErr        err;
  30.  
  31.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  32.     // If we get an error here it just means that they haven't supplied a reference to
  33.     // an object to copy - so copy the current section instead.
  34.     
  35.     if (directObj.descriptorType != typeNull)
  36.         err = CopyDesc(&directObj);
  37.     else
  38.     {            // Just copy the selection of the front window
  39.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  40.         if (noErr != err) goto done;
  41.         
  42.         err = CopyTextToken(&aTextToken);
  43.     }
  44.  
  45. done:    
  46.     if (directObj.dataHandle)
  47.         AEDisposeDesc(&directObj);
  48.         
  49.     return(err);
  50. } // DoCopy
  51.  
  52.  
  53. OSErr    CopyTextToken(TextToken* theToken)
  54. {
  55.     WindowPtr        aWindow;
  56.     DPtr            docPtr;
  57.     OSErr            err;
  58.     
  59.     aWindow = theToken->tokenWindow;
  60.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  61.     
  62.     if (! aWindow || ! docPtr)
  63.         return(errAENoSuchObject);
  64.  
  65.                     // Set this tokens selection
  66.     err = SelectTextToken(theToken);
  67.     if (noErr != err) goto done;
  68.  
  69.     err = (OSErr)ZeroScrap();
  70.     TECopy(docPtr->theText);     
  71.     
  72. done:
  73.     return(err);
  74. }
  75.  
  76. OSErr    CopyTextDesc(AEDesc* textDesc)
  77. {
  78.     TextToken        aTextToken;
  79.     Size            actualSize;
  80.     OSErr            err;
  81.  
  82.     if (typeMyText != textDesc->descriptorType)
  83.         return(errAETypeError);
  84.         
  85.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  86.  
  87.     err = CopyTextToken(&aTextToken);
  88.     
  89.     return(err);
  90. }
  91.  
  92. OSErr    CopyDesc(AEDesc* aDesc)
  93. {
  94.     AEDesc        copyDesc = {typeNull, NULL},
  95.                 textDesc = {typeNull, NULL};
  96.     OSErr        err;
  97.     
  98.     if (typeObjectSpecifier == aDesc->descriptorType)
  99.         err = AEResolve(aDesc, kAEIDoMinimum, ©Desc);
  100.     else
  101.         err = AEDuplicateDesc(aDesc, ©Desc);
  102.         
  103.     if (noErr != err) goto done;
  104.     
  105.     switch (copyDesc.descriptorType)
  106.     {
  107.         case typeAEList:
  108.             err = errAETypeError;
  109.             // We can't handle copying more than one item to the scrap
  110.             break;
  111.             
  112.         default:
  113.             err = AECoerceDesc(©Desc, typeMyText, &textDesc);
  114.             if (noErr != err) goto done;
  115.             err = CopyTextDesc(&textDesc);
  116.     }
  117.     
  118. done:
  119.     if (copyDesc.dataHandle)
  120.         AEDisposeDesc(©Desc);
  121.     if (textDesc.dataHandle)
  122.         AEDisposeDesc(&textDesc);
  123.     
  124.     return(err);
  125. }
  126.