home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / AMT / Sample AMT⁄PE Code / SOURCES / LaunchAndOpen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-05  |  12.5 KB  |  526 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <memory.h>
  3. #include <Packages.h>
  4. #include <Errors.h>
  5. #include <quickdraw.h>
  6. #include <fonts.h>
  7. #include <dialogs.h>
  8. #include <windows.h>
  9. #include <menus.h>
  10. #include <events.h>
  11. #include <OSEvents.h>
  12. #include <Desk.h>
  13. #include <diskinit.h>
  14. #include <OSUtils.h>
  15. #include <resources.h>
  16. #include <toolutils.h>
  17. #include <AppleEvents.h>
  18. #include <EPPC.h>
  19. #include <GestaltEqu.h>
  20. #include <PPCToolbox.h> 
  21. #include <Processes.h>
  22. #include <Aliases.h>
  23. #include <Files.h>
  24. #include <String.h>
  25. #include <Strings.h>
  26. #include <TextEdit.h>
  27. #include <StandardFile.h>
  28.  
  29.  
  30. #include "key.h"
  31.  
  32. //#include "AEPackObject.h"
  33. //#include "AEObjects.h"
  34. //#include "AETypes.h"    
  35.  
  36. #define aeSelectionKeyword         'fsel'
  37. #define aeOpenSelection 'sope'
  38. #define kFinderSig 'FNDR'
  39. #define kSystemType 'MACS'
  40.  
  41. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber* processSN);
  42.  
  43. OSErr OpenSelection(FSSpec* theDocToOpen)
  44. {
  45.     AppleEvent             aeEvent;                                 /* the event to create */
  46.     AEDesc                 myAddressDesc, aeDirDesc, listElem;      /* some descriptors I'll need */
  47.     FSSpec                 dirSpec;                                 /* FSSpec for the 'parent' directory of the file I'm opening */
  48.     AEDesc                 fileList;                                /* my list */
  49.     OSErr                 myErr;                                   /* guess */
  50.     ProcessSerialNumber process;                                 /* This will hold the process serial number of the Finder */
  51.     AliasHandle         DirAlias, FileAlias;                     /* some aliases */
  52.     
  53.     /* go find the Finder's process information, please */
  54.     if (FindAProcess(kFinderSig, kSystemType, &process))
  55.         return(procNotFound);     /* if I can't find the Finder, quit.  Always check this, someone else */
  56.         
  57.     /* Create an address descriptor so the AppleEvent manager knows where to send this event */
  58.     if (myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc))
  59.         return(myErr);
  60.     
  61.     /* Create the empty FinderEvent */
  62.     /* it's a Finder 'FNDR', Open Selection 'sope' event */
  63.     if (myErr = AECreateAppleEvent(kFinderSig, aeOpenSelection, &myAddressDesc, kAutoGenerateReturnID, 
  64.                     kAnyTransactionID, &aeEvent))
  65.         return(myErr);
  66.     
  67.     /* make a FSSpec for the parent folder (see the OpenSeletion description in the AE Registry ) */
  68.     /* using the information in the document FSSpec */
  69.     FSMakeFSSpec(theDocToOpen->vRefNum, theDocToOpen->parID, nil, &dirSpec);
  70.     NewAlias(nil, &dirSpec, &DirAlias);
  71.     
  72.     /* Create alias for file */
  73.     NewAlias(nil, theDocToOpen, &FileAlias);
  74.     /* Create the file  list */
  75.     if (myErr = AECreateList(nil, 0, false, &fileList))
  76.     return(myErr);
  77.     
  78.     /*  create the folder  descriptor */
  79.     HLock((Handle)DirAlias);
  80.     AECreateDesc(typeAlias, (Ptr)*DirAlias, GetHandleSize((Handle)DirAlias), &aeDirDesc);
  81.     HUnlock((Handle)DirAlias);
  82.     DisposHandle((Handle)DirAlias);
  83.     /* put the Directory Desc in the event as the direct object */
  84.     if ((myErr = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr)
  85.     {
  86.         /* done with the desc, kill it */
  87.         AEDisposeDesc(&aeDirDesc);
  88.         
  89.         /*  create the file descriptor and add to aliasList */
  90.         HLock((Handle)FileAlias);
  91.         AECreateDesc(typeAlias, (Ptr)*FileAlias, GetHandleSize((Handle)FileAlias), &listElem);
  92.         HLock((Handle)FileAlias);
  93.         DisposHandle((Handle)FileAlias);
  94.         myErr = AEPutDesc(&fileList, 0, &listElem);
  95.     }
  96.     
  97.     if (myErr)
  98.     return(myErr);
  99.     AEDisposeDesc(&listElem);
  100.     /* Add the file alias list to the event */
  101.     if (myErr = AEPutParamDesc(&aeEvent, aeSelectionKeyword, &fileList))
  102.     return(myErr);
  103.     myErr = AEDisposeDesc(&fileList);
  104.     
  105.     /* And now send the event! */
  106.     myErr = AESend(&aeEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  107.     
  108.     /* and kill the memory used */
  109.     AEDisposeDesc(&aeEvent);
  110. }
  111.  
  112.  
  113. /* This runs through the process list looking for the indicated application */
  114. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumber* processSN)
  115. {
  116.     ProcessInfoRec     tempInfo;
  117.     FSSpec             procSpec;
  118.     Str31             processName;
  119.     OSErr             myErr = noErr;
  120.     
  121.     /* nul out the PSN so we're starting at the beginning of the list */
  122.     processSN->lowLongOfPSN = kNoProcess;
  123.     processSN->highLongOfPSN = kNoProcess;
  124.     
  125.     /* initialize the process information record */
  126.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  127.     tempInfo.processName = processName;
  128.     tempInfo.processAppSpec = &procSpec;
  129.     
  130.     /* loop through all the processes until we */
  131.     /* 1) find the process we want */
  132.     /* 2) error out because of some reason (usually, no more processes */
  133.     
  134.     myErr = GetNextProcess(processSN);
  135.     while (myErr == noErr)
  136.     {
  137.         GetProcessInformation(processSN, &tempInfo);
  138.         p2cstr(processName);
  139.         keyPrint("Process=%s\n", processName);
  140.         
  141.         if (tempInfo.processSignature == creatorToFind && tempInfo.processType == typeToFind)
  142.             break;
  143.             
  144.         keyPrint("Didnt like it\n");
  145.         
  146.         myErr = GetNextProcess(processSN);
  147.     }
  148.         
  149.     return myErr;
  150. }
  151.  
  152.  
  153. OSErr SendOpenDocument(ProcessSerialNumber* tgtAddress, FSSpec* fileSpec)
  154.  {
  155.     SFTypeList                fileType_list= {'TEXT','PDF '};        /* PDF 1.0 = 'TEXT', PDF 1.1 = 'PDF ' */
  156.     AliasHandle             file_alias;    
  157.     AEDesc                    fileDesc;
  158.     AEDescList                the_list;
  159.     AppleEvent                appleEvent, replyEvent;    
  160.     OSErr                    err = noErr;
  161.     Boolean                    isInvisible=false;    
  162.     AEDesc                    myAddressDesc;
  163.     
  164.     /* Init stuff */
  165.     the_list.dataHandle = NULL;
  166.     appleEvent.dataHandle = NULL;    
  167.     
  168.     /* Create an address descriptor so the AppleEvent manager knows where to send this event */
  169.     err = AECreateDesc(typeProcessSerialNumber, (Ptr)tgtAddress, sizeof(ProcessSerialNumber), &myAddressDesc);
  170.     if (err) 
  171.         return err;
  172.     
  173.     /* Create the Apple Event */
  174.     if (err == noErr)
  175.         err = AECreateAppleEvent (kCoreEventClass,
  176.                                   kAEOpenDocuments,
  177.                                   &myAddressDesc,
  178.                                   kAutoGenerateReturnID,
  179.                                   kAnyTransactionID, 
  180.                                   &appleEvent );
  181.         
  182.     /* Create an empty list for the descriptors */
  183.     if (err == noErr) 
  184.         err = AECreateList (NULL, 0, false, &the_list);
  185.  
  186.     /* Create an alias to the file */
  187.     if (err == noErr) {
  188.         err = NewAliasMinimal (fileSpec, &file_alias);
  189.     }
  190.         
  191.     /* Create a descriptor with the alias and put it into the list */
  192.     if (err == noErr) {
  193.         fileDesc.descriptorType = typeAlias;
  194.         fileDesc.dataHandle = (Handle) file_alias;
  195.         err = AEPutDesc (&the_list, 0, &fileDesc);
  196.     }
  197.         
  198.     /* Put the list into the apple event */
  199.     if (err == noErr)
  200.         err = AEPutParamDesc( &appleEvent, keyDirectObject, &the_list );
  201.     
  202.     /* add the invisible attribute */
  203.     err = AEPutParamPtr(&appleEvent, 'nvis', typeBoolean, 
  204.                 (Ptr)&isInvisible, sizeof(isInvisible));
  205.  
  206.     /* Send the event */
  207.     if (err == noErr) {
  208.         err = AESend (&appleEvent, 
  209.                       &replyEvent,
  210.                       kAEWaitReply+kAECanInteract+kAECanSwitchLayer, 
  211.                       kAENormalPriority, 
  212.                       600, 
  213.                       NULL, 
  214.                       NULL);
  215.     }
  216.     
  217.     AEDisposeDesc(&appleEvent);
  218.     AEDisposeDesc(&replyEvent);
  219.     
  220.     return err;
  221. }
  222.  
  223. static OSErr    LaunchApp(char* theApp, ProcessSerialNumber* theSN)
  224. {
  225.     Str255                 path;
  226.     OSErr                 err;
  227.     FSSpec                spec;
  228.     LaunchParamBlockRec    launchPB;
  229.  
  230.     strncpy((char *)path, (char *)theApp, 254);
  231.     path[254] = '\0';
  232.     c2pstr((char *)path);
  233.  
  234.     err = FSMakeFSSpec(0, 0, path, &spec);
  235.     if (err != noErr)
  236.         return err;
  237.     
  238.     launchPB.launchBlockID = extendedBlock;
  239.     launchPB.launchEPBLength = extendedBlockLen;
  240.     launchPB.launchFileFlags = 0;
  241.     launchPB.launchControlFlags = launchContinue + launchDontSwitch + launchNoFileFlags;
  242.     launchPB.launchAppSpec = &spec;
  243.     launchPB.launchAppParameters = nil;
  244.     
  245.     err = LaunchApplication(&launchPB);
  246.     if (err != noErr)
  247.         return err;
  248.         
  249.     *theSN = launchPB.launchProcessSN;
  250.     
  251.     return err;
  252. }
  253.  
  254. #ifdef OUT
  255. int    GotoPage(pageNum)
  256. {
  257.     Str255                 path;
  258.     OSErr                 err;
  259.     FSSpec                spec;
  260.     AppleEvent            appleEvent;
  261.     ProcessSerialNumber    psn;
  262.     AEDesc                 myAddressDesc;
  263. //    long                pageNum;
  264.     
  265.     SysBeep(2);
  266.     
  267.     if (FindAProcess('APPL', 'CARO', &psn))
  268.     {
  269.         keyPrint("Cant find acrobat application\n");
  270.         return -1;
  271.     }
  272.     
  273.     /* Create an address descriptor so the AppleEvent manager knows where to send this event */
  274.     err = AECreateDesc(typeProcessSerialNumber, (Ptr)&psn, sizeof(psn), &myAddressDesc);
  275.     if (err) 
  276.     {
  277.         keyPrint("Error in AECreateDesc = %d\n", err);
  278.         return err;
  279.     }
  280.     
  281.     err = AECreateAppleEvent(kAEAcrobatViewerSuite,
  282.                         kAEGotoPage,
  283.                         &myAddressDesc,
  284.                         kAutoGenerateReturnID,
  285.                         kAnyTransactionID, 
  286.                         &appleEvent);
  287.     if (err)
  288.     {
  289.         keyPrint("Error in AECreateDesc = %d\n", err);
  290.         return err;
  291.     }
  292.     
  293.     err = AEPutParamPtr(&appleEvent, keyAEPageNumber, typeLongInteger, &pageNum, sizeof(pageNum));
  294.     if (err)
  295.     {
  296.         keyPrint("Error in AEPutParamPtr = %d\n", err);
  297.         return err;
  298.     }
  299.     
  300.     /* And now send the event! */
  301.     err = AESend(&appleEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, 
  302.                 kAEDefaultTimeout, nil, nil);
  303.  
  304.     /* and kill the memory used */
  305.     AEDisposeDesc(&appleEvent);
  306.     
  307.     if (err)
  308.         keyPrint("Error in AESend = %d", err);
  309.         
  310.     return err;
  311. }    
  312. #endif
  313.  
  314. void    MyLauncherGotoPage(key* the)
  315. {
  316.     char                theApp[256];
  317.     char                theDoc[256];
  318.     int                    thePageNum;
  319.     EventRecord            dummyEvent;
  320.     OSErr                 err;
  321.     FSSpec                spec;
  322.     ProcessSerialNumber    psn;
  323.     long                count;
  324.     
  325.     thePageNum = keyToInteger(the[ARGUMENT(1)]);
  326.     
  327.     the[RESULT] = keyFalse;
  328.     
  329. //    GotoPage(thePageNum);
  330.         
  331.     return;
  332. }
  333.  
  334. void    MyLauncherOpen(key* the)
  335. {
  336.     char                theApp[256];
  337.     char                theDoc[256];
  338.     int                    thePageNum;
  339.     EventRecord            dummyEvent;
  340.     OSErr                 err;
  341.     FSSpec                spec;
  342.     ProcessSerialNumber    psn;
  343.     long                count;
  344.     
  345. //    strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
  346. //    theApp[254] = '\0';
  347.     
  348.     strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
  349.     theDoc[254] = '\0';
  350.     c2pstr(theDoc);
  351.     
  352. //    thePageNum = keyToInteger(the[ARGUMENT(3)]);
  353.     
  354.     the[RESULT] = keyFalse;
  355.     
  356.     err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
  357.     if (err != noErr)
  358.     {
  359.         keyPrint("Error in FSMakeFSSpec = %d", err);
  360.         return;
  361.     }
  362.     
  363.     err = OpenSelection(&spec);
  364.     if (err != noErr)
  365.     {
  366.         keyPrint("Error in OpenSelection = %d", err);
  367.         return;
  368.     }
  369.     
  370.     the[RESULT] = keyTrue;
  371.     
  372. return;
  373.  
  374.     err = LaunchApp(theApp, &psn);
  375.     if (err != noErr)
  376.     {
  377.         keyPrint("Error in LaunchApp = %d", err);
  378.         return;
  379.     }
  380.         
  381.     err = SendOpenDocument(&psn, &spec);
  382.     if (err != noErr)
  383.     {
  384.         keyPrint("Error in LaunchApp = %d", err);
  385.         return;
  386.     }
  387.         
  388.     // now wait a bit, and then bring the app to the front
  389.     WaitNextEvent(0, &dummyEvent, 180, nil);        // check every second
  390.     
  391.     SetFrontProcess(&psn);
  392.     
  393.     the[RESULT] = keyTrue;
  394. }
  395.  
  396.  
  397.  
  398.  
  399. void    MyChapterOpen(key* the)
  400. {
  401.     char                theApp[256];
  402.     char                theDoc[256];
  403.     int                    thePageNum;
  404.     EventRecord            dummyEvent;
  405.     OSErr                 err;
  406.     FSSpec                spec;
  407.     ProcessSerialNumber    psn;
  408.     long                count;
  409.     
  410. //    strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
  411. //    theApp[254] = '\0';
  412.     
  413.     strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
  414.     theDoc[254] = '\0';
  415.     c2pstr(theDoc);
  416.     
  417. //    thePageNum = keyToInteger(the[ARGUMENT(3)]);
  418.     
  419.     the[RESULT] = keyFalse;
  420.     
  421.     err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
  422.     if (err != noErr)
  423.     {
  424.         keyPrint("Error in FSMakeFSSpec = %d", err);
  425.         return;
  426.     }
  427.     
  428.     err = OpenSelection(&spec);
  429.     if (err != noErr)
  430.     {
  431.         keyPrint("Error in OpenSelection = %d", err);
  432.         return;
  433.     }
  434.     
  435.     the[RESULT] = keyTrue;
  436.     
  437. return;
  438.  
  439.     err = LaunchApp(theApp, &psn);
  440.     if (err != noErr)
  441.     {
  442.         keyPrint("Error in LaunchApp = %d", err);
  443.         return;
  444.     }
  445.         
  446.     err = SendOpenDocument(&psn, &spec);
  447.     if (err != noErr)
  448.     {
  449.         keyPrint("Error in LaunchApp = %d", err);
  450.         return;
  451.     }
  452.         
  453.     // now wait a bit, and then bring the app to the front
  454.     WaitNextEvent(0, &dummyEvent, 180, nil);        // check every second
  455.     
  456.     SetFrontProcess(&psn);
  457.     
  458.     the[RESULT] = keyTrue;
  459. }
  460.  
  461.  
  462.  
  463. void    MyAcroLauncherOpen(key* the)
  464. {
  465.     char                theApp[256];
  466.     char                theDoc[256];
  467.     int                    thePageNum;
  468.     EventRecord            dummyEvent;
  469.     OSErr                 err;
  470.     FSSpec                spec;
  471.     ProcessSerialNumber    psn;
  472.     long                count;
  473.     
  474. //    strncpy(theApp, keyToString(the[ARGUMENT(1)]), 254);
  475. //    theApp[254] = '\0';
  476.     
  477.     strncpy(theDoc, keyToString(the[ARGUMENT(1)]), 254);
  478.     theDoc[254] = '\0';
  479.     c2pstr(theDoc);
  480.     
  481. //    thePageNum = keyToInteger(the[ARGUMENT(3)]);
  482.     
  483.     the[RESULT] = keyFalse;
  484.     
  485.     err = FSMakeFSSpec(0, 0, (unsigned char const *)theDoc, &spec);
  486.     if (err != noErr)
  487.     {
  488.         keyPrint("Error in FSMakeFSSpec = %d", err);
  489.         return;
  490.     }
  491.     
  492.     err = OpenSelection(&spec);
  493.     if (err != noErr)
  494.     {
  495.         keyPrint("Error in OpenSelection = %d", err);
  496.         return;
  497.     }
  498.     
  499.     the[RESULT] = keyTrue;
  500.     
  501. return;
  502.  
  503.     err = LaunchApp(theApp, &psn);
  504.     if (err != noErr)
  505.     {
  506.         keyPrint("Error in LaunchApp = %d", err);
  507.         return;
  508.     }
  509.         
  510.     err = SendOpenDocument(&psn, &spec);
  511.     if (err != noErr)
  512.     {
  513.         keyPrint("Error in LaunchApp = %d", err);
  514.         return;
  515.     }
  516.         
  517.     // now wait a bit, and then bring the app to the front
  518.     WaitNextEvent(0, &dummyEvent, 180, nil);        // check every second
  519.     
  520.     SetFrontProcess(&psn);
  521.     
  522.     the[RESULT] = keyTrue;
  523. }
  524.  
  525.  
  526.