Applescript - Connection is invalid I have an application for mac and I use the following applescript code (exported as an AS app) to start that app periodically:

on idle
    tell application "System Events"
        tell application "MyApp"
            run
        end tell
        return 7
    end tell
end idle


I use [NSApp terminate:self] in MyApp to terminate itself after it finished its work.

The problems is that the AS script displays a "Connection is invalid" error message when it tries to run MyApp the second time and onward. The error does not show up in the first launch.

I'm guessing [NSApp terminate:self] is causing the conflict with the AS somehow, but why?
In the end I used NSTimer to automate my code.

Rather then using the applescript to launch the app periodically, I have the app run constantly and use NSTimer within my app to fire a specific method from time to time.

[NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO];


This way the app is never killed and does not have to be restarted.