Solved

Launch Application using AppleEvents

Posted on 1997-07-03
2
310 Views
Last Modified: 2013-12-26
I used Apple's sample code to launch applications and it works with one problem. I used it to launch Simpletext but it does not create a new window for me.

Can someone please tell me how I can get it to work. :<
Please show source code for this :-)

Thanks for any help
MacDude
0
Comment
Question by:MacDude
2 Comments
 

Author Comment

by:MacDude
Comment Utility
Edited text of question
0
 
LVL 1

Accepted Solution

by:
Paul B earned 200 total points
Comment Utility
If you want an application to open a specific document you should send it an open document AppleEvent.
If just want to launch an application with not document you should send an open application AppleEvent.
The open application AppleEvent tells the application to create a default window.
For TeachText, it should create an untitled window for you.

Here is code send an open application AppelEvent:

OSErr SendOpenApplicationToFile(FSSpec *docFSSpecPtr);
OSErr BuildOpenApplicationEvent(ProcessSerialNumber *targetPSN, AppleEvent *oappAppleEvent);
OSErr GetProcessFromFileSpec(const FSSpec * docFSSpecPtr, ProcessSerialNumber* currPSN);


OSErr SendOpenApplicationToFile(FSSpec *docFSSpecPtr)
{
      AppleEvent theAppleEvent, theReplyEvent;
      ProcessSerialNumber targetPSN;
      OSErr err;

      theAppleEvent.dataHandle = nil;

      err = GetProcessFromFileSpec(docFSSpecPtr, &targetPSN);
      if (!err) {
            err = BuildOpenApplicationEvent(&targetPSN, &theAppleEvent);
            if (!err)
                  err = AESend(&theAppleEvent,
                                          &theReplyEvent,
                                          kAENoReply,
                                          kAENormalPriority,
                                          kAEDefaultTimeout,
                                          nil,
                                          nil);
      }
      if (theAppleEvent.dataHandle != nil)  
            (void) AEDisposeDesc(&theAppleEvent);
      return err;
}

OSErr BuildOpenApplicationEvent(ProcessSerialNumber *targetPSN, AppleEvent *oappAppleEvent)
{
      OSErr                  err;
      AppleEvent            theAppleEvent;
      AEDesc                  targetAddrDesc;

      // to simplify cleanup, ensure that handles are nil to start
      targetAddrDesc.dataHandle      = nil;
      theAppleEvent.dataHandle      = nil;

      // create an address descriptor based on the serial number of
      // the target process
      err = AECreateDesc(typeProcessSerialNumber, (Ptr) targetPSN,
            sizeof(ProcessSerialNumber), &targetAddrDesc);
      if (err != noErr) goto Bail;

      
      // now make the 'oapp' AppleEvent descriptor
      err = AECreateAppleEvent(kCoreEventClass, kAEOpenApplication,
            &targetAddrDesc, kAutoGenerateReturnID, kAnyTransactionID,
            &theAppleEvent);
      if (err != noErr) goto Bail;
      
      *oappAppleEvent = theAppleEvent;

Bail:
      // dispose of everything that was allocated, except the return AE
      if (targetAddrDesc.dataHandle != nil)  (void) AEDisposeDesc(&targetAddrDesc);
      
      return err;

}

OSErr GetProcessFromFileSpec(const FSSpec * docFSSpecPtr, ProcessSerialNumber* currPSN)
{
      ProcessInfoRec currProcessInfo;
      FSSpec applicationSpec;
      short err;

      currPSN->lowLongOfPSN = kNoProcess;
      currPSN->highLongOfPSN = 0;

      currProcessInfo.processInfoLength = sizeof(ProcessInfoRec);
      currProcessInfo.processName = nil;
      currProcessInfo.processAppSpec = &applicationSpec;

      while (GetNextProcess(currPSN) == noErr) {
            if (GetProcessInformation(currPSN, &currProcessInfo) == noErr) {
                  if (
                        (applicationSpec.vRefNum == docFSSpecPtr->vRefNum) &&
                        (applicationSpec.parID == docFSSpecPtr->parID) &&
                        EqualString(applicationSpec.name, docFSSpecPtr->name, false, false)
                        ) {
                        return noErr;
                  }
            }
      }
      return fnfErr;
}

Good Luck,
Paul Baxter
pbaxter@assistivetech.com




0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now