Launching another application

Launching another application

Post by Howar » Sun, 15 Aug 2004 01:08:52


Hi,

I can't believe I've spent three hours trying to do something so simple
as to launch another application in OSX! Can someone please give me advice
or a code snippet to do this?

I'm trying to run an application from another application. Both are in
the same folder. (I'm using CodeWarrior 9, and the apps are listed as
"Macintosh PowerPC", which I'm assuming is CFM, right?)

I've currently got code like this:

FSRef Ref;
OSStatus stat = FSPathMakeRef((const unsigned char*)"OtherApp", &Ref, 0);
if (noErr == stat)
{
fileFound = true;
stat = LSOpenFSRef(&Ref, 0);
if (noErr == stat)
fileOpened = true;
}
else
SysBeep(60); // (added this to show it fails on the makeref call)

But, the FSPathMakeRef is failing. The file exists and is in the same
folder as the app I'm running. Is the path wrong? If so, how do I specify
a program in the *current* folder?

I'm tearing my hair out on this. Apple has tons and tons of reference
material, but it's almost impossible to find a simple code example for any
of their API calls!

Please help!

-Howard
 
 
 

Launching another application

Post by Paul » Sun, 15 Aug 2004 02:03:03


simple
advice
in
specify


In general the "*current* folder" is not necessarily where your app is. This
is not a Mac strong point.

To locate a running app you can use the Process Manager or various CFBundle
routines (if your app is packaged.) Bear in mind that packaged applications
think that they are in <package> / Contents / (etc). You will NOT get the
package directory itself without some effort! There is a page about this in
the docs that I can't seem to find it right now. IIRC there is a matrix of
choices based on OS, application type, etc.

I've also done this successfully this w/ LSFindApplicationForInfo. Look at
the Launch Services reference for options & variations. It was a little
arcane to set up but it worked.

Paul

 
 
 

Launching another application

Post by Miro Juris » Sun, 15 Aug 2004 02:45:53

In article <bv6Tc.23601$ XXXX@XXXXX.COM >,



This is wrong; you can simply call GetProcessBundleLocation to get the location
of the bundle (as opposed to the executable within the bundle)

meeroh

--
If this message helped you, consider buying an item
from my wish list: < http://www.yqcomputer.com/ ;
 
 
 

Launching another application

Post by Howar » Sun, 15 Aug 2004 03:04:07


CFBundle
applications
the
location

I'm not using a bundle package, but just a plain, flat, Carbon (CFM)
application.

I've found a way that is working (at least in my initial tests). But I'm
not sure if this is the right way or not:

FSSpec Spec;
OSStatus stat = FSMakeFSSpec( 0, 0, "\pOtherApp", &Spec );
if (noErr == stat)
{
FSRef Ref;
stat = FSpMakeFSRef( &Spec, &Ref );
if (noErr == stat)
{
stat = LSOpenFSRef(&Ref, 0);
...etc.

Is this ok, or can this fail in some case I haven't considered?

-Howard
 
 
 

Launching another application

Post by Paul » Sun, 15 Aug 2004 03:46:48


I made a conscious decision not to list that as a option, believe it or not.
It's not recommended or supported (it "relies on undocumented behavior"),
but it does work and most of us have done it at one time or another. It's
probably safe on OS9 (since I don't expect any updates there) but shaky on X
where the whole file system and API is a moving target.

I'd never encountered GetProcessBundleLocation() before, but then I've
always had to support OS9 too & I tend towards single solutions where
possible. Looks like a good option if you're X only.

Another really off the wall technique that I've actually seen used: save a
text version of an AppleScript (NOT a compiled script!) that launches your
other app, then compile and run it. That forces the OS to locate your
program by name (hopefully there is only one copy.)
 
 
 

Launching another application

Post by knelle » Mon, 16 Aug 2004 00:15:43


I would prefer the following (without error handling).


/*-------------------------*/

ProcessSerialNumber psn;
OSErr err = GetCurrentProcess(&psn);

ProcessInfoRec info;
FSSpec appSpec;

info.processInfoLength = sizeof(info);
info.processName = 0;
info.processAppSpec = & appSpec;
err = GetProcessInformation(&psn, &info);

/* Now you have got a file specifier of your running app
Now make a specifier of the other one.
*/

FSSpec otherAppSpec;
err = FSMakeFSSpec( appSpec.vRefNum, appSpec.parID,
"\pOtherApp", &otherAppSpec);

/* The following launch the other app but works
on older MacOS < 9 and may be a bit cheaper.
No need for LaunchServices and FSRef.
*/

LaunchParamBlockRec pb;
pb.launchBlockID = extendedBlock;
pb.launchEPBLength = extendedBlockLen;
pb.launchFileFlags = launchNoFileFlags; // More?
pb.launchControlFlags = launchNoFileFlags; // More?
pb.launchAppSpec = &otherAppSpec;
pb.launchAppParameters = nil; // More?

err = LaunchApplication(&pb);


/*-------------------------*/


Just my 2 cent.


Kind regrads

Martin
 
 
 

Launching another application

Post by knelle » Mon, 16 Aug 2004 00:31:56

Hi




I forgot: I also would prefer to use its creator for locating
the other app rather then its name (which may be changed by
the user).

There are several ways to do so.
AFAIR there is a useful function in the MoreFiles collection.


Kind regards

Martin
 
 
 

Launching another application

Post by Thomas Eng » Mon, 16 Aug 2004 06:26:51

In article < XXXX@XXXXX.COM >,



Unique creator codes seem to be out of fashion today, especially for
Cocoa apps.
Sometimes the name is unique, sometimes the creator in the LS database,
sometimes the bundle identifier..

Regards,
Tom_E

--
This address is valid in its unmodified form but expires soon.