[Solved] Set app to frontmost app (For app in MAS)

Hey,
I’m developing an app that has a StatusItem menu (next to the clock and stuff…).
From there the apps Preferences window can be shown and that part works fine.
The problem is that the app will stay in the background, I want it to become the frontmost app.
I know MBS has a thing for that but I don’t have those Plugins.

And, is it OK for an app in Mac App Store to set itself as the frontmost app? :slight_smile:

PS. Searched forums, found nothing :slight_smile:

I’ll need to check when I get back to the office but I’ve done this before in a Xojo app - I can’t remember if I used MBS plugins (I try to avoid plugins if at all possible) or if I fudged it by making the Preferences window be a global floating window. I assume you’re using MacOSLib?

I can answer the second part now though; yes, it’s fine to set itself the frontmost app and would be the expected behavior in this case.

Check macoslib, seems to recall something is present for your needs.

[quote=27246:@Gavin Smith]I assume you’re using MacOSLib?
[/quote]
That is correct :wink:
I’ll dig into that a bit too while I wait for you to get back to the office :stuck_out_tongue:

PS.
Please make me stop using smileys, it’s getting ridiculous…

app.FrontMostMBS = true
will also do it.

[quote=27327:@Christian Schmitz]app.FrontMostMBS = true
will also do it.[/quote]
I know, I just can’t afford MBS right now. Would like to though.

Does anyone know if simply doing

App.ExecutableFile.Launch

would get me in trouble when submitting my app to MAS?
That actually works…

You may launch a second copy this way.

[quote=27251:@Albin Kiland]That is correct :wink:
I’ll dig into that a bit too while I wait for you to get back to the office :P[/quote]

I checked and I did indeed do it with a Global Floating window. Like your app, I had an NSStatusItem menu which had a Preferences menu item which simply called Show on the window. This worked fine at the time and still does (even in Mavericks) except if the current frontmost app is a properly full-screen app (i.e. Dock and Menubar hidden). This is due to the use of Spaces - even though the global floating window is now showing, it’s showing in the wrong space. It’s too late tonight but I’ll try to look into it and see if there’s a work around.

Yeah, that just happened.

I’ve looked in macosLib without success and started looking at AppleScript.

As I understand, scripting the Finder is a no-go but do you guys think this simple AppleScript would be accepted by Apple to activate my own app? (Finder isn’t involved?)

activate application "myappname"

That AppleScript does put my app to the front :slight_smile:

On second thought, using AppleScript might be dumb :confused:

Found NSRunningApplications in macosLib.
It’s listing all running apps and activates the app thats chosen in a Popupmenu.

Don’t really know how to use it in my app to only activate my app though.

Changing this code somehow?

    dim theApp as NSRunningApplication = me.RowTag(me.ListIndex)
    dim b as Boolean = theApp.Activate

I’ve tried this, changing me.RowTag(me.listindex) to my app name but that gives me and error:
Expected Cocoa.NSRunningApplication, but got string.
Isn’t it a string thats stored in a RowTag?

    dim theApp as NSRunningApplication = "myappname"
    dim b as Boolean = theApp.Activate

This is what populates the Popupmenu and lists all running apps…

  for each runningApp as NSRunningApplication in  NSWorkspace.RunningApplications
    me.AddRow runningApp.LocalizedName
    me.RowTag(PopupMenu1.ListCount - 1) = runningApp
    me.Icon(PopupMenu1.ListCount - 1) = runningApp.Icon
  next

Macoslib ->

dim ok as boolean = Cocoa.NSRunningApplication.CurrentApplication.Activate

[quote=27727:@Eduardo Gutierrez de Oliveira]Macoslib ->

dim ok as boolean = Cocoa.NSRunningApplication.CurrentApplication.Activate[/quote]
Thank you Eduardo, but it does not seem to work.
If I’m in Finder and use my menu item to run your code nothing happens.
If I’m in my app and do the same I remain in the app but the main window gets inactive.

Any ideas why?

[quote=27730:@Albin Kiland]Thank you Eduardo, but it does not seem to work.
If I’m in Finder and use my menu item to run your code nothing happens.
If I’m in my app and do the same I remain in the app but the main window gets inactive.

Any ideas why?[/quote]

I haven’t tested it. I thought it’d work. Hmm.

Have you tried with the NSApplication.ArrangeInFront method? I’m looking at the source on the web but can’t try it myself.

Also: What did you end up using for the menuitem functionality?

[quote=27731:@Eduardo Gutierrez de Oliveira]I haven’t tested it. I thought it’d work. Hmm.

Have you tried with the NSApplication.ArrangeInFront method? I’m looking at the source on the web but can’t try it myself.

Also: What did you end up using for the menuitem functionality?[/quote]

Haven’t looked at ArrangeInFront but it seems to be doing something else: “//# Arranges windows listed in the Window menu in front of all other windows.
This should not be so difficult :wink:

I use NSStatusBar in macosLib to create the menuitem.

Thank you guys for trying to help out :slight_smile:

In Cocoa, you used to have set activateIgnoringOtherApps (which you’d do on NSApplication.CurrentApplication in this case) - is that exposed in MacOSLib? (Won’t get time to look this weekend I’m afraid)

Have you tried NSWindow.level=0 ?

[quote=27734:@Albin Kiland]Haven’t looked at ArrangeInFront but it seems to be doing something else: “//# Arranges windows listed in the Window menu in front of all other windows.
This should not be so difficult :wink:
[/quote]

It looks like it does what you want from within the application (since it pushes windows in the background or behind other apps to the forefront), I thought it would work, indirectly, when run from a menuitem. But I don’t have one to test on.