NSAppleScript: buggy?

  • I have the following situation:

    I create a NSAppleScript from a file URL (a not compiled script in this
    case).

    This script is saying:

    tell application lambda

    quit

    end tell

    This scripts runs OK.

    I then create a NSAppleScript from another file URL (still a not
    compiled script)

    This script is saying:

    tell application lambda

    activate

    do something with your life

    end tell

    Here NSAppleScript panics and states:

    NSAppleScriptErrorNumber: -609  (which leads to "No Results Found" on
    the ADC Web site)

    NSAppleScriptErrorBriefMessage: "Connection is invalid."

    NSAppleScriptErrorMessage: "lambda got an error: Connection is invalid."

    NSAppleScriptErrorRange: nil

    If I don't run the QUIT script before, everything works fine.

    This is a bit annoying and I don't see any clever explanation why this
    would fail from a design point of view.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • I've seen this, not in your context but from the apple dev studio. It
    turns out that in some situations calling a quit before an activate
    will cause this IF the script is not compiled.

    Compiled scripts work ok.

    On Aug 4, 2004, at 10:34 AM, Stiphane Sudre wrote:

    > I have the following situation:
    >
    > I create a NSAppleScript from a file URL (a not compiled script in
    > this case).
    >
    > This script is saying:
    >
    > tell application lambda
    >
    > quit
    >
    > end tell
    >
    > This scripts runs OK.
    >
    > I then create a NSAppleScript from another file URL (still a not
    > compiled script)
    >
    > This script is saying:
    >
    > tell application lambda
    >
    > activate
    >
    > do something with your life
    >
    > end tell
    >
    > Here NSAppleScript panics and states:
    >
    > NSAppleScriptErrorNumber: -609  (which leads to "No Results Found" on
    > the ADC Web site)
    >
    > NSAppleScriptErrorBriefMessage: "Connection is invalid."
    >
    > NSAppleScriptErrorMessage: "lambda got an error: Connection is
    > invalid."
    >
    > NSAppleScriptErrorRange: nil
    >
    > If I don't run the QUIT script before, everything works fine.
    >
    > This is a bit annoying and I don't see any clever explanation why this
    > would fail from a design point of view.
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Aug 4, 2004, at 5:56 PM, John Spicer wrote:

    > I've seen this, not in your context but from the apple dev studio. It
    > turns out that in some situations calling a quit before an activate
    > will cause this IF the script is not compiled.
    >
    > Compiled scripts work ok.

    Since the NSAppleScript documentation is saying the script is compiled,
    I would tend to think there's a bug in AppleScript due to bad caching
    for this stupid case.

    If the application is running and you send a quit and then activate,
    the activate fails.

    So my conclusion is that the AppleScript engine is supposing the
    application is still there after the quit and that no human soul would
    think about relaunching it in the same script. Therefore it's using the
    cached connection.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • I think I've found a solution.

    1. I send a quit Apple Event to the application lamba

    2. I find the application lambda using its Creator code, since it's
    been launched, it's in the LS cache...

    3. I launch lambda using Launch Services.

    4. I use a. applescript (not compiled I think compared to .script).

    => It works.

    Another battle won against the machines...

    On Aug 4, 2004, at 5:57 PM, Stiphane Sudre wrote:

    > On Aug 4, 2004, at 5:56 PM, John Spicer wrote:
    >
    >> I've seen this, not in your context but from the apple dev studio. It
    >> turns out that in some situations calling a quit before an activate
    >> will cause this IF the script is not compiled.
    >>
    >> Compiled scripts work ok.
    >
    > Since the NSAppleScript documentation is saying the script is
    > compiled, I would tend to think there's a bug in AppleScript due to
    > bad caching for this stupid case.
    >
    > If the application is running and you send a quit and then activate,
    > the activate fails.
    >
    > So my conclusion is that the AppleScript engine is supposing the
    > application is still there after the quit and that no human soul would
    > think about relaunching it in the same script. Therefore it's using
    > the cached connection.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • on 2004-08-04 11:34 AM, Stiphane Sudre at <ssudre...> wrote:

    > NSAppleScriptErrorBriefMessage: "Connection is invalid."
    >
    > NSAppleScriptErrorMessage: "lambda got an error: Connection is invalid."
    >
    > NSAppleScriptErrorRange: nil
    >
    > If I don't run the QUIT script before, everything works fine.
    >
    > This is a bit annoying and I don't see any clever explanation why this
    > would fail from a design point of view.

    In your real scripts (as opposed to your emailed scripts), are you putting
    the name of the target application in quotes, as in 'tell application
    "lambda"'? (I take it that "lambda" is the actual name of the target
    application.) I assume you are putting it in quotes, since you say the
    scripts work except for the one problem scenario you identify.

    The "connection is invalid" error message means the target application has
    stopped running (i.e., it has quit, whether deliberately or "unexpectedly")
    and a second command is sent to the same target application. The
    "connection" it refers to is the connection to the AppleScript target.

    I say this only as an observation of fact based on my own experience,
    apparently confirmed by your experience. One would suppose that the second
    script's 'tell' statement should relaunch lambda. The fact that it doesn't
    would seem to be a bug.

    You could try working around it by inserting a statement at the beginning of
    the second script telling the Finder to 'open application file "lambda"'
    followed by a 'delay n' command before the second 'tell application
    "lambda"' in order to give lambda time to finish launching. You might also
    try leaving out the Finder 'open' command and just go with the 'delay'
    command, on the theory that you have to give AppleScript time to complete
    the quit operation before sending the second tell. Set 'n' to a largish
    value -- say 10 or 15 seconds -- then, if it works, try reducing the delay
    to suit until it stops working.

    To do some more diagnostics, you could insert 'beep' or 'display dialog'
    commands at strategic points in both scripts, to see whether they are both
    running and, if so, when. In Mac OS X 10.3 and newer, scripts normally queue
    up and are executed in first in, first out order (it was the opposite before
    10.3 which caused all sorts of difficulties). But they don't necessarily
    always wait for one to signal completion before the next starts executing,
    especially, I believe, when quitting or launching applications. For example,
    put a 'beep' immediately before and after the 'quit' in the first script,
    and listen for the second beep -- do you hear it before lambda has finished
    quitting? Put a 'beep' immediately before the 'tell' statement in the second
    script, and run the same test.

    --

    Bill Cheeseman - <wjcheeseman...>
    Quechee Software, Quechee, Vermont, USA
    http://www.quecheesoftware.com

    The AppleScript Sourcebook - http://www.AppleScriptSourcebook.com
    Vermont Recipes - http://www.stepwise.com/Articles/VermontRecipes
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • on 2004-08-04 11:57 AM, Stiphane Sudre at <ssudre...> wrote:

    > If the application is running and you send a quit and then activate,
    > the activate fails.

    Bear in mind that 'activate' does not launch the target application, but
    only brings it to the front. It's the 'tell' statement that performs this
    task, executing an implicit 'run' command. You can also use 'run application
    "lambda"' before (or without) the 'tell' block, just to make it start
    running. The 'run' command will launch and fully initialize it

    Alternatively, you can us the 'launch' command, which will launch the target
    application but leave out some normal startup steps such that, for example,
    script statements addressed to it won't work if they include terminology
    unique to lambda (as opposed to generic AppleScript terminology; the utility
    of launch is that it doesn't present the target application's splash
    screen).

    So, in addition to my separate suggestion that you try relaunching it with a
    Finder 'open application "lambda"' statement, you could try 'run application
    "lambda"' at the beginning of the second script, followed by a 'delay'.

    > So my conclusion is that the AppleScript engine is supposing the
    > application is still there after the quit and that no human soul would
    > think about relaunching it in the same script. Therefore it's using the
    > cached connection.

    This is entirely possible. There is a known, similarly elementary bug in
    -[NSAppleScript executeAndReturnError:], whereby it fails to save script
    properties that are changed during script execution (property values in
    scripts are supposed to be persistent, saved in the script file itself). If
    a very long 'delay' command at the beginning of the second script doesn't
    cure your problem, then this is probably the correct diagnosis.

    And, I just discovered, you can create a similar bug scenario in Script
    Editor. I just compiled and ran a simple script addressed to iTunes. It
    worked. Then I quit iTunes and edited the script (still addressed to
    itunes). When I tried to compile it, I was surprised to get an error message
    saying "The application is not running." When I then launched iTunes from
    the Finder and tried to compile the script again, I was even more surprised
    to get the same error -- even though iTunes was now running.

    --

    Bill Cheeseman - <wjcheeseman...>
    Quechee Software, Quechee, Vermont, USA
    http://www.quecheesoftware.com

    The AppleScript Sourcebook - http://www.AppleScriptSourcebook.com
    Vermont Recipes - http://www.stepwise.com/Articles/VermontRecipes
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.