Determine if an external app is running -or- a document for an external app is open

View: New views
2 Messages — Rating Filter:   Alert me  

Determine if an external app is running -or- a document for an external app is open

by John Love-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The challenge is how to determine if an external app is running *and*, if running, is a specific document for the external running app currently open.  The headache centers on the fact that everything works when the external document is closed, with its external app still running, that is, the external doc registers as not open.  BUT .. when the external app itself has quit, this external app does *not* register as not running.

BTW, I am using appscript which is an external 3rd party library I have brought in.  Even if someone is not familiar with appscript, I believe my problem is not related to appscript .. the reason I say this is because I have used non-appscript approaches with the same negative results.

Another BTW .. all this is taking place in a background Thread, that is, calls to -pingExtDoc are in the secondary Thread.

Reference -pingExtDoc below which 1st calls -pingExtApp .. if the external app has indeed quit, the instance parm = itsFileStatus should be set = kNoExtAppError by -pingExtApp.  In this case, -pingExtDoc simply bypasses all the doc-stuff, with the "damage" already registered via -pingExtApp's setting itsFileStatus = kNoExtAppError.  The background Thread tests itsFileStatus and terminates the background Thread because itsFileStatus is different from kNoError.  Then, we return to the main Thread.  Standard approach, I believe.

Here's where things get very spooky .. see "spooky" at the bottom of -pingExtDoc

=====

First, I determine if the external app itself is running via:

- (void )pingExtApp {
       
        extAppRunning = [itsExtApp isRunning];
       
        // -setFileStatus: calls NSMutableDictionary's setValue:theValueObject forKey:theKey
        extAppRunning ? [self setFileStatus:kNoError] : [self setFileStatus:kNoExtAppError];
       
}

Then, in my external doc method:

- (void )pingExtDoc {
       
        BOOL docActive = NO;

        [self pingExtApp];
        if ([self itsFileStatus] == kNoError) {
                itsExtDoc = [[itsExtApp windows] byName:itsFileName];   // autoreleased
                id appscriptResult = [[itsExtDoc exists] send];
                if (appscriptResult != nil)   docActive = [appscriptResult boolValue];
        }
 /*
        else  [self setFileStatus:kNoExtAppError];    // inside call to -pingExtApp
 */
       
        // Adding this "spooky" code eliminates the problem, other than the fact I'm calling -pingExtApp TWICE?

        if (!docActive) {
                       
                // doc may not be active because the app has quit, so let's find out?
                [self pingExtApp];
                if ([self itsFileStatus] == kNoError)   [self setFileStatus:kNoExtDocError];
             // else  [self setFileStatus:kNoExtAppError];    // from above call to -pingExtApp
        }
     // else docActive, in which case itsFileStatus = kNoError from above call to -pingExtApp

        // end "spooky"

}
=====

Thanks in advance

John Love
Touch the Future! Teach!



_______________________________________________
MacOSX-dev mailing list
MacOSX-dev@...
http://www.omnigroup.com/mailman/listinfo/macosx-dev

Re: Determine if an external app is running -or- a document for an external app is open

by Christiaan Hofman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Nov 10, 2009, at 16:40, John Love wrote:

> The challenge is how to determine if an external app is running *and*, if running, is a specific document for the external running app currently open.  The headache centers on the fact that everything works

What is "everything"? I.e., what do you really want to achieve?

This kind of thing sounds like something you just should never need to do anyway, so I think you have a serious design problem in your app. Think object encapsulation, but then even worse (because we're not even talking about a single program).

> when the external document is closed, with its external app still running, that is, the external doc registers as not open.  BUT .. when the external app itself has quit, this external app does *not* register as not running.

Then your method to determine whether the app is running is simply wrong. You're obviously not really checking whether the app is running. My guess is that you're sending a method to the app that requires the app to be running, that would obviously not be working to check whether it's running. Also, what do you mean by "does not register as not running"? In other words what are you REALLY checking and what result do you REALLY get.

> BTW, I am using appscript which is an external 3rd party library I have brought in.  Even if someone is not familiar with appscript, I believe my problem is not related to appscript .. the reason I say this is because I have used non-appscript approaches with the same negative results.

What non-appscript methods?

>
> Another BTW .. all this is taking place in a background Thread, that is, calls to -pingExtDoc are in the secondary Thread.
>

Are you sure appscript is thread safe? Often these Apple Event and AppleScript based methods are not implemented in a thread safe way.

> Reference -pingExtDoc below which 1st calls -pingExtApp .. if the external app has indeed quit, the instance parm = itsFileStatus should be set = kNoExtAppError by -pingExtApp.  In this case, -pingExtDoc simply bypasses all the doc-stuff, with the "damage" already registered via -pingExtApp's setting itsFileStatus = kNoExtAppError.  The background Thread tests itsFileStatus and terminates the background Thread because itsFileStatus is different from kNoError.  Then, we return to the main Thread.  Standard approach, I believe.
>
> Here's where things get very spooky .. see "spooky" at the bottom of -pingExtDoc
>
> =====
>
> First, I determine if the external app itself is running via:
>
> - (void )pingExtApp {
>
> extAppRunning = [itsExtApp isRunning];
>

What is itsExtApp, and what is -isRunning? I only know of -[NSApplication isRunning], but that only applies to your own app, not an external app.

If you're talkin about appscript methods, then this is not the right mailing list for this question.

> // -setFileStatus: calls NSMutableDictionary's setValue:theValueObject forKey:theKey
> extAppRunning ? [self setFileStatus:kNoError] : [self setFileStatus:kNoExtAppError];
>
> }
>
> Then, in my external doc method:
>
> - (void )pingExtDoc {
>
> BOOL docActive = NO;
>
> [self pingExtApp];
> if ([self itsFileStatus] == kNoError) {
> itsExtDoc = [[itsExtApp windows] byName:itsFileName];   // autoreleased
> id appscriptResult = [[itsExtDoc exists] send];
> if (appscriptResult != nil)   docActive = [appscriptResult boolValue];
> }
> /*
> else  [self setFileStatus:kNoExtAppError];    // inside call to -pingExtApp
> */
>
> // Adding this "spooky" code eliminates the problem, other than the fact I'm calling -pingExtApp TWICE?
>

It's not really clear to me what "the problem" is. Try to formulate it in terms of STR: what exactly do you do in exactly what situation, what does a particular step return, and what did you expect it to return?

> if (!docActive) {
>
> // doc may not be active because the app has quit, so let's find out?
> [self pingExtApp];
> if ([self itsFileStatus] == kNoError)   [self setFileStatus:kNoExtDocError];
>     // else  [self setFileStatus:kNoExtAppError];    // from above call to -pingExtApp
> }
>     // else docActive, in which case itsFileStatus = kNoError from above call to -pingExtApp
>
> // end "spooky"
>
> }
> =====
>
> Thanks in advance
>
> John Love
> Touch the Future! Teach!
>

In short, I think the problem is in appscript, or more precisely your abuse/misundestanding of it. So then ask your question on an appscript related list. If not, then formulate it (much more clearly) without using appscript.

Christiaan

_______________________________________________
MacOSX-dev mailing list
MacOSX-dev@...
http://www.omnigroup.com/mailman/listinfo/macosx-dev