Here is an outline for making and sending a quit apple event to the finder. I haven't tried to compile or execute this, and it may have errors, but it is the basic outline. Executing a script in a file is about the same amount of code.
Here is the function that will quit the finder
OSErr quitFinder(void)
{
ProcessSerialNumber psn;
AppleEvent theQuitEvent;
OSErr result;
//1. get the process id of the program you want to quit, using the finder's type and creator. return result in psn
result=FindAProcess('FNDR','MACS',&psn);
if (result==noErr)
{
//2. make the apple event using the psn. return the result in theQuitEvent
result=MakeQuitEvent(&theQuitEvent,psn);
if (result==noErr)
{
//3. send the event.
result=sendQuitEvent(&theQuitEvent)
}
else
return result;
}
else
return result;
}
Here are the three functions
// FindAProcess-- checks all running processes to see if there is one running that uses
// the type and creator we are looking for. processSN is returned
OSErr FindAProcess(OSType typeToFind, OSType creatorToFind,
ProcessSerialNumberPtr processSN)
{
ProcessInfoRec tempInfo;
FSSpec procSpec;
Str31 processName;
OSErr myErr = noErr;
/* nul out the PSN so we're starting at the beginning of the list */
processSN->lowLongOfPSN = kNoProcess;
processSN->highLongOfPSN = kNoProcess;
/* initialize the process information record */
tempInfo.processInfoLength = sizeof(ProcessInfoRec);
tempInfo.processName = processName;
tempInfo.processAppSpec = &procSpec;
/* loop through all the processes until we */
/* 1) find the process we want */
/* 2) error out because of some reason (usually, no more processes */
do {
myErr = GetNextProcess(processSN);
if (myErr == noErr)
GetProcessInformation(processSN, &tempInfo);
}while ((tempInfo.processSignature != creatorToFind ||
tempInfo.processType != typeToFind) ||
myErr != noErr);
return(myErr);
}
You also need to make an event. This one will create your appleevent and
return it in the container you pass in as theAppleEvent. It needs to know
the process serial number of the program you want to quit
OSErr MakeQuitEvent(AppleEvent* theAppleEvent,ProcessSerialNumber psn)
{
OSErr myErr;
AEDescList fileAliasListDesc;
AEAddressDesc targetAddress;
OSErr ignoreErr;
myErr=AECreateDesc(typeProcessSerialNumber, &psn,
sizeof(ProcessSerialNumber),&targetAddress);
if (myErr!=noErr)
{
doError("Making Target Descriptor for Program to Quit",myErr);
return(myErr);
}
myErr = AECreateAppleEvent(kCoreEventClass, kAEQuit,
&targetAddress, kAutoGenerateReturnID,
kAnyTransactionID, theAppleEvent);
if (myErr!=noErr)
{
doError("Creating the Quit Event",myErr);
ignoreErr=AEDisposeDesc(&targetAddress);
return(myErr);
}
return(noErr);
}
OSErr sendQuitEvent(AppleEvent *theQuitEvent)
{
OSErr myErr;
AppleEvent reply;
myErr=AESend(&theAppleEvent, &reply, kAENoReply + kAENeverInteract,
kAENormalPriority, 240, nil, nil);
if (myErr!=noErr) /*Apple event successfully sent*/
doError("Sending the Quit Event",myErr);
return myErr;
}