home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / Launch creator / launcher.cc next >
Encoding:
C/C++ Source or Header  |  1993-11-02  |  4.0 KB  |  126 lines  |  [TEXT/ALFA]

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *        This is a simple program that, given a file name, launches an
  5.  * application-creator and has it handle the file. The net result is
  6.  * exactly the same as if the user had double-clicked on the file.
  7.  *
  8.  * Synopsis
  9.  *        void open_selection(const char * full_path_name)
  10.  * Where the 'full_path_name' tells the full path name of the file that
  11.  * should be "double-clicked". Though, a relative path name would do,
  12.  * too.
  13.  *
  14.  * The present program achieves the magic by sending an 'Open Selection'
  15.  * event to the Finder. It is significantly based on the FinderEvents
  16.  * stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
  17.  *
  18.  * OpenSelection event requires two parameters:
  19.  *    - alias of the Folder the file resides
  20.  *    - list containing an alias of the file(s) to "double-click" on
  21.  *
  22.  *
  23.  ***********************************************************************
  24.  */
  25.  
  26. /* MacHeaders Included */
  27. #include "myenv.h"
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <AppleEvents.h>
  31. #include <Aliases.h>
  32.  
  33.  
  34.                             // Create an 'open selection' event to be sent to Finder
  35. static AppleEvent create_event_for_finder(void)
  36. {
  37.     const AEEventClass     kAEFinderEvents = 'FNDR';        // Finder event
  38.     const AEEventID     kOpenSelection = 'sope';        // OpenSelection event
  39.     const OSType         kFinderSignature = 'MACS';        // Our destination
  40.     AppleEvent             the_event;
  41.     AEAddressDesc         finder_id;
  42.     
  43.     do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
  44.                        &finder_id) );
  45.  
  46.     do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
  47.                             kAutoGenerateReturnID,
  48.                             kAnyTransactionID,
  49.                             &the_event) );
  50.  
  51.     do_well( AEDisposeDesc(&finder_id) );                    // the_event is created, finder_id can
  52.                                                               // be disposed of now
  53.       
  54.     return the_event;
  55. }
  56.  
  57.                                     // Add the alias of file or path name to the event
  58. static void add_path_name(AppleEvent * the_event_ptr, const char * path_name)
  59. {
  60.     AliasHandle alias;
  61.     
  62.     do_well( NewAliasMinimalFromFullPath(strlen(path_name), (const unsigned char *)path_name, 
  63.                                       "\p", "\p", &alias) );
  64.     HLock((char **)alias);
  65.     do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
  66.                          (**alias).aliasSize) );
  67.  
  68.     HUnlock((char **)alias);
  69.     DisposHandle((char **)alias);
  70. }
  71.  
  72.  
  73.                                     // Create a "selection" item in the event from the
  74.                                     // full file name specified 
  75. static void add_selection(AppleEvent * the_event_ptr, const char * file_name)
  76. {
  77.     AliasHandle alias;
  78.     AEDescList selection_list;
  79.     
  80.     do_well( NewAliasMinimalFromFullPath(strlen(file_name), (const unsigned char *)file_name, 
  81.                                       "\p", "\p", &alias) );
  82.  
  83.     do_well( AECreateList(nil, 0, FALSE, &selection_list) );
  84.     HLock((char **)alias);
  85.     do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias), 
  86.                    (**alias).aliasSize) );
  87.     HUnlock((char **)alias);
  88.     DisposHandle((char **)alias);
  89.  
  90.     do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
  91.     do_well( AEDeleteItem(&selection_list, 1) );
  92.     assert( AEDisposeDesc(&selection_list) == noErr );
  93. }
  94.  
  95.  
  96. //-------------------------------------------------------------------------
  97. //                            Routing module
  98. //
  99.  
  100. void open_selection(const char * full_path_name)
  101.     char path_name[255];                // Path to the file, ends in :
  102.     char * p;
  103.     
  104.     AppleEvent the_event, reply;
  105.     
  106.        
  107.                                         // Getting just a path name from the full
  108.                                         // file name
  109.     assure(strlen(full_path_name) < sizeof(path_name),
  110.            "Full path name is too big");
  111.     if( (p=strrchr(full_path_name,':')) == (char *)0 )
  112.       path_name[0] = '\0';                            // Path name is empty
  113.     else
  114.       strncpy(path_name,full_path_name,p-full_path_name+1);
  115.               
  116.     the_event = create_event_for_finder();
  117.     add_path_name(&the_event,path_name);
  118.     add_selection(&the_event,full_path_name);
  119.     
  120.     do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract, 
  121.                  kAENormalPriority, kAEDefaultTimeout, nil, nil) );
  122.  
  123.     do_well( AEDisposeDesc(&the_event) );
  124.     do_well( AEDisposeDesc(&reply) );
  125. }