Launching/quitting a process


#1

I want iTunes to start when my app starts and want iTunes to quit when my application quits. Is it possible to do this (Window & OS X) ?


#2

You’d probably have to launch a background process helper to monitor the active apps…and launch / quit your app that way

On the Mac at least you can use GetNextProcess, you could also use an appleevent that times out to detect if its running, but GetNextProcess will be more reliable.


ProcessSerialNumber PSN={0,kNoProcess};
	ProcessInfoRec info;
	Str31 processName;
	FSSpec processSpec;
	info.processInfoLength=sizeof(ProcessInfoRec);
	info.processName=processName;
	info.processAppSpec=&processSpec;
	
	OSErr theErr;
	
	while ((theErr=GetNextProcess (&PSN))==noErr && (!(PSN.highLongOfPSN == 0 && PSN.lowLongOfPSN == kNoProcess)))
	{
		theErr=GetProcessInformation(&PSN,&info);
		{
			FSRef location;
			OSStatus theStatus;
			NSString *longProcessName;
			
			theStatus=CopyProcessName(&PSN,(CFStringRef*)&longProcessName);
			// NSLog(@"%@",longProcessName);
		
			
			if ([longProcessName isEqualToString:@"iTunes"])
			{
// Found iTunes
}
	CFRelease(longProcessName);

#3

[size=150][EDIT] [/size] DOh! Sorry, on re-reading the original post, I see that you want to close iTunes when your app closes… I initially read it the other way round! My apologies… the following post will probably be of little use, but I’ll leave it here anyway…

This might help for windows…

I’ve not tested it for a couple of months, so it may need to be tweaked; it’s also perhaps more than you’re after.

It allows you to start an external process, recieve notification of when it finishes, but also recieve the any console output it generates.

It was designed as a means of launching a compiler, so it can be made to function as a custom automatable command line.


#4

Well, Justin posted some code to execute some bits of AppleScript. I think I could manage using that one, because it also allowed me to expand/collapse the iTunes window and set its bounds.
Thanks for the Windows code. I’ll use CreateProcess()