home *** CD-ROM | disk | FTP | other *** search
- /*
- ***********************************************************************
- *
- * This is a simple program that, given a file name, launches an
- * application-creator and has it handle the file. The net result is
- * exactly the same as if the user had double-clicked on the file.
- *
- * Synopsis
- * void open_selection(const char * full_path_name)
- * Where the 'full_path_name' tells the full path name of the file that
- * should be "double-clicked". Though, a relative path name would do,
- * too.
- *
- * The present program achieves the magic by sending an 'Open Selection'
- * event to the Finder. It is significantly based on the FinderEvents
- * stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
- *
- * OpenSelection event requires two parameters:
- * - alias of the Folder the file resides
- * - list containing an alias of the file(s) to "double-click" on
- *
- *
- ***********************************************************************
- */
-
- /* MacHeaders Included */
- #include "myenv.h"
- #include <string.h>
- #include <stdarg.h>
- #include <AppleEvents.h>
- #include <Aliases.h>
-
-
- // Create an 'open selection' event to be sent to Finder
- static AppleEvent create_event_for_finder(void)
- {
- const AEEventClass kAEFinderEvents = 'FNDR'; // Finder event
- const AEEventID kOpenSelection = 'sope'; // OpenSelection event
- const OSType kFinderSignature = 'MACS'; // Our destination
- AppleEvent the_event;
- AEAddressDesc finder_id;
-
- do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
- &finder_id) );
-
- do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &the_event) );
-
- do_well( AEDisposeDesc(&finder_id) ); // the_event is created, finder_id can
- // be disposed of now
-
- return the_event;
- }
-
- // Add the alias of file or path name to the event
- static void add_path_name(AppleEvent * the_event_ptr, const char * path_name)
- {
- AliasHandle alias;
-
- do_well( NewAliasMinimalFromFullPath(strlen(path_name), (const unsigned char *)path_name,
- "\p", "\p", &alias) );
- HLock((char **)alias);
- do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
- (**alias).aliasSize) );
-
- HUnlock((char **)alias);
- DisposHandle((char **)alias);
- }
-
-
- // Create a "selection" item in the event from the
- // full file name specified
- static void add_selection(AppleEvent * the_event_ptr, const char * file_name)
- {
- AliasHandle alias;
- AEDescList selection_list;
-
- do_well( NewAliasMinimalFromFullPath(strlen(file_name), (const unsigned char *)file_name,
- "\p", "\p", &alias) );
-
- do_well( AECreateList(nil, 0, FALSE, &selection_list) );
- HLock((char **)alias);
- do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias),
- (**alias).aliasSize) );
- HUnlock((char **)alias);
- DisposHandle((char **)alias);
-
- do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
- do_well( AEDeleteItem(&selection_list, 1) );
- assert( AEDisposeDesc(&selection_list) == noErr );
- }
-
-
- //-------------------------------------------------------------------------
- // Routing module
- //
-
- void open_selection(const char * full_path_name)
- {
- char path_name[255]; // Path to the file, ends in :
- char * p;
-
- AppleEvent the_event, reply;
-
-
- // Getting just a path name from the full
- // file name
- assure(strlen(full_path_name) < sizeof(path_name),
- "Full path name is too big");
- if( (p=strrchr(full_path_name,':')) == (char *)0 )
- path_name[0] = '\0'; // Path name is empty
- else
- strncpy(path_name,full_path_name,p-full_path_name+1);
-
- the_event = create_event_for_finder();
- add_path_name(&the_event,path_name);
- add_selection(&the_event,full_path_name);
-
- do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract,
- kAENormalPriority, kAEDefaultTimeout, nil, nil) );
-
- do_well( AEDisposeDesc(&the_event) );
- do_well( AEDisposeDesc(&reply) );
- }