On Jul 7, 2009, at 8:21 AM, Jerry Krinock wrote:

I need to be able to quit and re-launch other applications, and have been getting all kinds of weird behavior. Maybe if someone could explain this one little test case I've isolated, it would be a big help.

The code below attempts to launch TextEdit, wait 5 seconds, quit TextEdit, wait 5 seconds, repeat forever. I get a launch, a quit, and a launch. But when it tries to quit for the second time, AESendMessage returns a -609 "Connection Invalid" error. I imagine this is because it is trying to connect to the application instance that it had quit the first time.

Documention of AESendMessage implies that recent versions of AppleScript will automatically try and reconnect, and that there is no more option to set.

How can I get it to forget the old connection and make a new one?

Thanks,

Jerry



I added similar functionality to one of my apps, and had problems using the bundle id as well. I switched to using the PSN (typeProcessSerialNumber) and that seems to work. You can use this to get the PSN:

void PSNForBundleIdentifier( NSString *bundleIdentifier, ProcessSerialNumber *psn )
{
    OSStatus anErr = noErr;
    ProcessSerialNumber aNum = { kNoProcess, kNoProcess };

    if ( psn == NULL ) return;

    while ( (anErr == noErr) ) {
        anErr = GetNextProcess( &aNum );
        if ( anErr == noErr ) {
CFDictionaryRef procInfo = ProcessInformationCopyDictionary( &aNum, kProcessDictionaryIncludeAllInformationMask ); if ( [[(NSDictionary *)procInfo objectForKey:(NSString *)kCFBundleIdentifierKey] isEqualToString:bundleIdentifier] ) {
                *psn = aNum;
                CFRelease( procInfo );
                break;
            }
            CFRelease( procInfo );
        }
    }
}


And then change your QuitAndSleep function to:

void QuitAndSleep(NSString* bundleIdentifier, float seconds) {
   NSLog(@"Quitting") ;
   NSAppleEventDescriptor *as ;
   ProcessSerialNumber aNum = { kNoProcess, kNoProcess } ;

   PSNForBundleIdentifier( bundleIdentifier, &aNum );

as = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber
                                                       bytes:&aNum
length:sizeof(aNum)];
   NSAppleEventDescriptor *ae ;
ae = [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass eventID:kAEQuitApplication
                                        targetDescriptor:as
returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
   AppleEvent *quitApplicationAppleEventPtr = (AEDesc*)[ae aeDesc];
   if (quitApplicationAppleEventPtr) {
       OSStatus err = AESendMessage(quitApplicationAppleEventPtr,
                                    NULL,
                                    kAENoReply,
                                    kAEDefaultTimeout) ;
       NSLog(@"Quit err = %d", err) ;
   }
   NSLog(@"Sleep after quit") ;
   usleep(seconds * 1e6) ;

}


--------------------------------------
Darkshadow
(aka Michael Nickerson)
http://www.nightproductions.net


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to