[comp.sys.mac.programmer] Loading Data Files Automatically

kkalmbac@slate.mines.colorado.edu (Kevin) (04/22/91)

	I am using Think C 4.0.2 and writing an application that writes to 
a data file.  I would like to be able to double click on the data file 
(in finder) and the application run and the data file load.
	I have the the bundle, FREF, owner, etc. so the data file comes up
with the desired icon and upon double clicking the application runs.
	My question is how can my application decide if a data file was 
double clicked and which one it was so it can open that data file.
	Either E-Mail or posts are fine with me.

					Thanks in advance,
						Kevin

Kevin Kalmbach
kkalmbac@mines.colorado.edu

dedreb@arco.com (Richard Beecher) (04/23/91)

In article <1991Apr22.153315.35057@slate.mines.colorado.edu>
kkalmbac@slate.mines.colorado.edu (Kevin) writes:
>
>        I am using Think C 4.0.2 and writing an application that writes to 
>a data file.  I would like to be able to double click on the data file 
>(in finder) and the application run and the data file load.
>        I have the the bundle, FREF, owner, etc. so the data file comes up
>with the desired icon and upon double clicking the application runs.
>        My question is how can my application decide if a data file was 
>double clicked and which one it was so it can open that data file.
>        Either E-Mail or posts are fine with me.
>
>                                        Thanks in advance,
>                                                Kevin
>
>Kevin Kalmbach
>kkalmbac@mines.colorado.edu
>

To detect files at program launch time, you will use the Segment Loader
routines.  I highly suggest you read that section in Inside Mac, Volume IV (I
think).  Basically, the Segment Loader includes routines that tell you the
number of files selected at launch time (CountAppFiles).  You then loop over
the number of files returned by CountAppFiles() and process the files, if they
are appropriate for your application.

Early in my program source, I call GetSelectedDocs() to process the files
opened at launch time.  As you can see by the following code, I haven't
fully-implemented this feature yet, but you should be able to get the flavor of
what I am trying to do.  I haven't done it yet, but I bet you could include a
call to GetSelectedDocs() in your main event loop if you are supporting
MultiFinder (in case app4Evt?).  An example of this would be if you program had
already been launched, but the user switches to the Finder and double-clicks on
one of your data files.  Again, I haven't tried this, so I can't assure you it
will work.

Anyway, here's some sample code.  Not much here, but I hope it helps.



/********************** main ********************/

main()
{
	ToolBoxInit();
 .
 . etc...
 .
	WindowInit();
	MenuBarInit();
	.
	GetSelectedDocs();
	.
	MainLoop();
}



/****************** GetSelectedDocs ****************/

void GetSelectedDocs()
{
	int			message, count, i;
	AppFile		theFile;
	
	CountAppFiles( &message, &count );
	
	if (!count) return;
	
	switch (message)
	{
		case appOpen:
			break;
		case appPrint:
			SysBeep(1);      /* I don't support printing */
			return;          /* yet from Finder!!!       */
			break;
	}
	
	for (i=1; i<=count; i++)
	{
		GetAppFiles( i, &theFile );
		
		if ( theFile.fType == 'TEXT' )
		{
			/* if ReadFIle() add this map data to an untitled window */
			/* if an untitled window doesn't exist yet, make one */
			/* Gotta figure out if there are any untitled MAP_WINDOWS still */
			if ( gNumUntitledDocs == 0 )
			{
				CreateWindow( MAP_WINDOW, NIL_STRING );
				/* Attach data to this new window */
			} else
			{
				/* Attach data to an existing untitled window? */
			}
			ClrAppFiles(i);
		} else if ( theFile.fType == 'Emap' )
		{
			CreateWindow( MAP_WINDOW, theFile.fName );
			/* Attach data to this new titled window */
			ClrAppFiles(i);
		} else if ( theFile.fType == 'Egeo' )
		{
			CreateWindow( DATABASE_WINDOW, theFile.fName );
			/* Attach data to this new titled window */
			ClrAppFiles(i);
		}
	}
}

Have Fun!   :-)

-------------------------------------
- Richard Beecher:  dedreb@arco.com -
-------------------------------------