// Getting Files Selected from the Finder
// A code example demonstrating how to get the files
// that the user selected in the Finder before opening
// your application. You must have the High-Level
// Event Aware flag in the application's 'SIZE' resource // set in order for the Apple Event code to work.
// The code that does not use apple events will work
// properly under both System 6 & 7.
// Assumes inclusion of
#include
#include <GestaltEqu.h>
#include <AppleEvents.h>
void InitToolbox(void);
Boolean AppleEventsInstalled (void); handlerRefCon);
handlerRefCon);
handlerRefCon);
void InitToolbox()
{
InitGraf(&thePort); // Standard initialization calls }
{
long result;
// THINK C's MacTraps library provides glue for Gestalt, so // it can be called safely under System 6. If an error is
// returned, then Gestalt for the AppleEvents Selector is // not available (this also means that Apple Events are
// not available)
err = Gestalt ( gestaltAppleEventsAttr, & result); return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
// return TRUE if there is no
// error and the proper bit of
// result is set
}
handlerRefCon)
{
long index,
itemsInList;
// get the direct parameter--a descriptor list--and put it into a docList
&docList);
if (err)
return err;
// check for missing parameters
err = MyGotRequiredParams (theAppleEvent);
if (err)
return err;
// count the number of descriptor records in the list
// now get each descriptor record from the list, coerce the returned
// data to an FSSpec record, and open the associated file printf ("Files to open:\n");
for (index = 1; index <= itemsInList; index++) {
& returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
if (err)
return err;
printf ("%s\n", PtoCstr(myFSS.name));
}
}
handlerRefCon)
{
long index,
itemsInList;
// get the direct parameter--a descriptor list--and put it into a docList
&docList);
if (err)
return err;
// check for missing parameters
err = MyGotRequiredParams (theAppleEvent);
if (err)
return err;
// count the number of descriptor records in the list
// now get each descriptor record from the list, coerce the returned
// data to an FSSpec record, and open the associated file printf ("Files to print:\n");
for (index = 1; index <= itemsInList; index++) {
& returnedType, (Ptr) &myFSS, sizeof(myFSS),
&actualSize);
if (err)
return err;
printf ("%s\n", PtoCstr(myFSS.name));
}
}
handlerRefCon)
{
printf ("No files to print or open\n");
}
{
typeWildCard, & returnedType, nil, 0,
&actualSize);
if (err == errAEDescNotFound) // you got all the required parameters
else if (!err) // you missed a required parameter
else // the call to AEGetAttributePtr failed
return err;
}
main ()
{
short doWhat;
short fileCnt;
short i;
InitToolbox ();
aEvents = AppleEventsInstalled();
if (aEvents) {
MyHandlePDoc,0, FALSE);
while (!gDone) {
switch (theEvent.what) {
case mouseDown:
case kHighLevelEvent:
break;
}
}
}
}
else {
//Get number of files double-clicked on by the user
if (fileCnt > 0) { // if the user selected one or more files
if (doWhat == appOpen) {
printf ("Files to open:\n");
for (i = 1; i <= fileCnt; i++) {
printf ("%s\n", PtoCstr (fileStuff.fName));
}
}
else if (doWhat == appPrint) {
printf ("Files to print:\n");
for (i = 1; i <= fileCnt; i++) {
printf ("%s\n", PtoCstr (fileStuff.fName));
}
}
for (i = 0; i < fileCnt; i++)
}
else
printf ("No files to print or open\n");
}
}