Check if app is active

Maybe an odd question but I need a way to check if my app is active or not?
I would add a timer that checks at interval x if my app is the active app. So when another app is active by the user, it should return false.

Seems easy but it is not. :confused:

BTW I only need this for macOS.

App.FrontMostMBS would work probably on Mac.

Don’t waste time answering these posts…

Ignore this thread and the OP.

[quote=334031:@Michel Bujardet]Don’t waste time answering these posts…

Ignore this thread and the OP.[/quote]

Why? Not sure what you meant with that comment.

[quote=334031:@Michel Bujardet]Don’t waste time answering these posts…
Ignore this thread and the OP.[/quote]

Not sure what makes this question any less worthy of consideration that any other

My guess is that Michel needs applause for every reply he gives. Otherwise you step on his Hobbit feet (read: long toes). :slight_smile: :slight_smile: :slight_smile: (*)
@Michel … thats humour, in case you did not catch that.

Back On-topic.

In fact, the solution Christian gave doesn’t work when you have a window that is Global Floating. app.frontMostMBS always returns True in that case.
So the problem remains. This is really a very hard to find problem. :slight_smile:
Checking if the app is active (not just front most app) seems not very difficult.

Create a property mIsActive As Boolean in App.

Put this in App.Activate:

mIsActive = True

Put this in App.Deactivate:

mIsActive = False

In the timer check the value of mIsActive.

Thanks Eli. Now that’s a neat trick and may actually work.
Will try this asap and let you know the outcome.

Actually I wonder why Xojo has those events but no property for the state.

Have you checked NSApplicationMBS?
It has an isActive property.

@Eli. Unfortunately this doesn’t work when the app has a Global Floating window. The events App.Activate is not triggered.

Will checkout the NSApplicationMBS too.

It does here.

[quote=334065:@Christoph De Vocht]@Eli. Unfortunately this doesn’t work when the app has a Global Floating window. The events App.Activate is not triggered.

Will checkout the NSApplicationMBS too.[/quote]
Seems like the global floating window may be causing a problem as it may be constantly telling the app that it is in the foreground; at which point I would suggest asking the OS for the foremost application and comparing that bundle identifier with yours.

Sorry I don’t know the exact API.

Ah, maybe you mean when the Global Floating Window is activated? Then it is not called. But that this correct, as the application is still not activated. For that use the Global Floating Window’s Activate event. So in your case you need to monitor both events (App.Activate and GlobalFloatingWindow1.Activate).

this seems to work:

[code]dim p1 as new ProcessMBS
dim p2 as new ProcessMBS

p1.GetFrontProcess
p2.GetCurrentProcess

if p1.ProcessID = p2.ProcessID then

label1.Text = “current = front”

else

label1.Text = “current <> front”

end if
[/code]

I made a new small app and it works indeed. For some case it doesn’t in my app. Maybe because the Global Floating window is only shown in full screen. Not sure.

This seems to resolve the problem

[code]dim p1 as new processmbs
dim p2 as new ProcessMBS
p1.GetFrontProcess
p2.GetCurrentProcess

if p1.ProcessID = p2.ProcessID then
Label1.Text = “current = front”
else
label1.Text = “current <> front”
end if[/code]

This correctly return if the Global Floating is active or not.

A BIG thanks to all who helped with this !! Including Michel Bujardet :wink:

On a side note:
I also did looked at NSApplicationMBS
Now this is a very powerful thingy. Glad I learned about this too.