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

P.S. I'm having another issue too. Under some conditions, if I launch an application using the NSWorkspace method, it will quit (quietly, no crash) as soon as my application resumes normal activity. I'm saving that for another post, unless someone has an "Aha!".

I've also tried using NSAppleScript and Scripting Bridge but have had similar problems.


#import <Cocoa/Cocoa.h>

void LaunchAndSleep(NSString* bundleIdentifier, float seconds) {
    NSLog(@"Launching") ;
    BOOL ok ;
ok = [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:bundleIdentifier
                                                              options:0
additionalEventParamDescriptor:nil launchIdentifier:NULL] ;
    NSLog(@"Launch success = %d", ok) ;
    NSLog(@"Sleep after launch") ;
    usleep(seconds * 1e6) ;

}

void QuitAndSleep(NSString* bundleIdentifier, float seconds) {
    NSLog(@"Quitting") ;
    const char* identifier = [bundleIdentifier UTF8String] ;
    NSAppleEventDescriptor *as ;
as = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplicationBundleID bytes:identifier length:strlen(identifier)];
    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) ;

}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSString* bundleIdentifier = @"com.apple.TextEdit" ;

    while (YES) {
        LaunchAndSleep(bundleIdentifier, 5) ;
        QuitAndSleep(bundleIdentifier, 5) ;
    }

    [pool release] ;
}


_______________________________________________

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