[comp.sys.mac.programmer] Finding out What Application is Running

jdm@ut-emx.UUCP (Jim Meiss) (12/28/88)

	In my desk accessory, I need to tell whether I've been opened
under Excel, because Excel always keeps its edit window as the
frontmost window.
	Currently I just check CurApName to see if it includes the
string "Excel". Of course that only works if someone hasn't renamed
Excel to something else (perverse person!).
	Anyway, does anyone have a better idea? I always get nervous
using a low memory global!

	Thanks.
									
				Jim Meiss				
		 		jdm@emx.utexas.edu
				jdm@fusion.utexas.edu

holland@m2.csc.ti.com (Fred Hollander) (12/29/88)

In article <9065@ut-emx.UUCP> jdm@ut-emx.UUCP (Jim Meiss) writes:
>
>	In my desk accessory, I need to tell whether I've been opened
>under Excel, because Excel always keeps its edit window as the
>frontmost window.
>	Currently I just check CurApName to see if it includes the
>string "Excel". Of course that only works if someone hasn't renamed
>Excel to something else (perverse person!).
>	Anyway, does anyone have a better idea? I always get nervous
>using a low memory global!

There's a procedure called GetAppParms (see IM II) that will return 
(among other things) the Finder info.  You can check the creator
for 'XCEL'.

Fred Hollander
Computer Science Center
Texas Instruments, Inc.
holland%ti-csl@csnet-rela
The above statements are my own and not representative of Texas Instruments.

oster@dewey.soe.berkeley.edu (David Phillip Oster) (12/29/88)

In article <9065@ut-emx.UUCP> jdm@ut-emx.UUCP (Jim Meiss) writes:
>	Currently I just check CurApName to see if it includes the
>string "Excel". Of course that only works if someone hasn't renamed
>Excel to something else (perverse person!).

Get the application's resource fork's refNum.
Stack the current refNum
UseResFile(appsRefNum);
Get1Resource('BNDL',...)  (remember, 64kROM macs don't have this call, so
	you'll need to do a GetResource and a HomeResFile to simulate it.)
near the beginning of the BNDL is the application's signature. This is
supposed to be unique for each application (that has an icon anyway) So,
it is a reliable way of seeing which application your are in.

Use the call GetAppParams() to get the application's refnum. It is
documented in the Segment Loader section of Inside Mac.

--- David Phillip Oster            --"When we replace the mouse with a pen,
Arpa: oster@dewey.soe.berkeley.edu --3 button mouse fans will need saxophone
Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu --lessons." - Gasee

tim@hoptoad.uucp (Tim Maroney) (12/29/88)

And of course, under MultiFinder you will always be running under "DA
Handler", never under Excel, unless you're opened with the Option key.
Putting application dependencies into your DA is not recommended.
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"If you vote for clowns, you have no right to complain that only clowns
 make it to the ballot." -- Tim Maroney

lsr@Apple.COM (Larry Rosenstein) (12/29/88)

In article <9065@ut-emx.UUCP> jdm@ut-emx.UUCP (Jim Meiss) writes:
>
>	In my desk accessory, I need to tell whether I've been opened
>under Excel, because Excel always keeps its edit window as the
>frontmost window.

Checking the application signature is better than checking CurApName (since
user's can't change the signature), but there is nothing to say that the
next version of Excel won't have a different signature.  (Aldus and Adobe
have both done this with their respective programs.)

What is it that you are trying to do?  It might be better to check for the
undesirable window configuration, and work from there.  (That way if soem
other application does the same thing, you are still covered.)

		 Larry Rosenstein,  Object Specialist
 Apple Computer, Inc.  20525 Mariani Ave, MS 46-B  Cupertino, CA 95014
	    AppleLink:Rosenstein1    domain:lsr@Apple.COM
		UUCP:{sun,voder,nsc,decwrl}!apple!lsr

jdm@ut-emx.UUCP (Jim Meiss) (12/29/88)

In response to my article Larry Rosenstein writes:
<Checking the application signature is better than checking CurApName (since
<user's can't change the signature), but there is nothing to say that the
<next version of Excel won't have a different signature.  (Aldus and Adobe
<have both done this with their respective programs.)

<What is it that you are trying to do?  It might be better to check for the
<undesirable window configuration, and work from there.  (That way if soem
<other application does the same thing, you are still covered.)

  What I'm trying to do is get the result of a calculation automatically entered
in an applications window. My routine for doing this is: 
-------
Boolean underExcel; 		/* TRUE if CurApplication is Excel*/

Handle DItemHandle(wPtr,item)
windowPtr wPtr;
int item;
{
	/*return handle for dialog item*/
}

Pstr_Length(aStr)
char* aStr;
{
	/*return length of a pascal string*/
}

PostKeys(itemNo)		/*AutoPost of the Result*/
int itemNo;
{
Str255 theText;
int i,len;
long message;
				
	GetIText(DItemHandle(dce->dCtlWindow,itemNo),theText);
	len = pstr_Length(theText);
	
	for(i=1;i<=len;i++)
	{
		message = (long)theText[i];
		PostEvent(keyDown,message);
	}
	
	SendBehind(dce->dCtlWindow,((WindowPeek)dce->dCtlWindow)->nextWindow);
	if(underExcel)
           SendBehind(FrontWindow(),((WindowPeek)FrontWindow())->nextWindow);
}
-------

 I have to do a second SendBehind for Excel because it keeps its small edit
window as the frontmost window, but this window won't accept keydown events. 
You have to make the spreadsheet window frontmost first. I don't know how to
check to see if this kind of window configuration exists in general.  This mght 
happen with other spreadsheet programs, but I only have Excel. Another case when
this could happen is for SuperPaint (and perhaps MacPaint too) which has the
pallette windows frontmost always.  However, this isn't a big problem because I 
don't envision anyone using my calculator for a painting program.
	Finally, this procedure doesn't work under Multifinder unless the option
key is used on launch of the DA. However, I wouldn't know how to tell which of
the running applications should receive the keydown's anyway.
	I'd entertain any suggestions about how to detect such a window 
configuration in general, or if there is something better to do than SendBehind.

				Thanks;
				Jim Meiss
				jdm@emx.utexas.edu

blm@cxsea.UUCP (Brian Matthews) (12/30/88)

Jim Meiss (jdm@ut-emx.UUCP) writes:
|	In my desk accessory, I need to tell whether I've been opened
|under Excel, because Excel always keeps its edit window as the
|frontmost window.
|	Currently I just check CurApName to see if it includes the
|string "Excel". Of course that only works if someone hasn't renamed
|Excel to something else (perverse person!).
|	Anyway, does anyone have a better idea? I always get nervous
|using a low memory global!

Instead of looking for a particular nasty application, why not look for
the particular behaviour instead?  This way you'll work with any
application that behaves that way, regardless of the name.  In this case,
perhaps you could just call FrontWindow, and see if it's yours.

Just a suggestion...


-- 
Brian L. Matthews  blm@cxsea.UUCP   ...{mnetor,uw-beaver!ssc-vax}!cxsea!blm
+1 206 251 6811    Computer X Inc. - a division of Motorola New Enterprises