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

Does your app still have a menubar, or do you run it with the key “LSUIElement” to make it bg-only?

(Argh, why can’t I edit my just-added (previous) message? No one has added another comment yet!)

So, to answer your other question: Having Apps with a System Icon and being bg-only (LSUIElement) are no problems for the App Store. I maintain iClip in the MAS, which is written in RB, for instance.

Oh, that wasn’t your question. But this: iClip, being bg-only, brings itself to front on occasion, e.g. when it shows a window or dialog. Nothing wrong with that.

You have to reload the page before you get the possibilty to edit your reply.

It isn’t bg-only but it’s main window does not need to be visible all the time, that’s why I have the menu item for quick access to certain functions. Showing the Preferences window is one of them. But it isn’t that usefull if the app ain’t going to show up front :wink:

I’m looking at frontmostMBS currently since there does not seem to be another way :confused:

Nope, how do that work?

I know, and I did.

There is almost always a way with declares for these kind of functions.
The only ones we cannot do with declares are ObjC functions that use the new ObjC-block construct, and operations that require more complex handling involving preemptive threads or needing calls to RB’s Plugin API, which isn’t the case here.

What you need to call seems to be SetFrontProcess, see here: macos - Forcing an LSUIElement "Accessory" application to the front - Stack Overflow

Here’s a function that does this:

[code]Sub BringSelfToFront()
declare function GetCurrentProcess lib CarbonLib (ByRef psn as UInt64) as Integer
declare function SetFrontProcess lib CarbonLib (ByRef psn as UInt64) as Integer

dim psn as UInt64
call GetCurrentProcess (psn)
call SetFrontProcess (psn)
End Sub[/code]

[quote=27911:@Thomas Tempelmann]
Here’s a function that does this:

[code]Sub BringSelfToFront()
declare function GetCurrentProcess lib CarbonLib (ByRef psn as UInt64) as Integer
declare function SetFrontProcess lib CarbonLib (ByRef psn as UInt64) as Integer

dim psn as UInt64
call GetCurrentProcess (psn)
call SetFrontProcess (psn)
End Sub[/code][/quote]

Should be something similar for Cocoa?

Did you try it out?

Just did, works just fine. Thank you!
But can an app in MAS use CarbonLib?

Yes, it can.

Great, finally a working solution :slight_smile:
I’ll keep my eyes on MBS later on since I’ll need it sooner or later anyways.

Thank you all for helping out!
Have a super weekend.

This is very similar to what we use in our apps in the Mac App Store. It should be enough to get you going.

declare function NSClassFromString lib cocoaLibrary ( inName as CFStringRef ) as Ptr declare function sharedApplication lib cocoaLibrary selector "sharedApplication" ( classRef as Ptr ) as Ptr Dim myApp as Ptr = sharedApplication( NSClassFromString( "NSApplication" ) ) declare sub activateIgnoringOtherApps lib cocoaLibrary selector "activateIgnoringOtherApps:" ( appRef as Ptr, flag as boolean ) activateIgnoringOtherApps( myApp, True )

Sam, that works like a charm!
Just changed it a bit “cocoaLibrary” > “Cocoa”.
Thank you for that!

So it seems we’ve got two solutions here:

Cocoa:

  declare function NSClassFromString lib "Cocoa" ( inName as CFStringRef ) as Ptr
  declare function sharedApplication lib "Cocoa" selector "sharedApplication" ( classRef as Ptr ) as Ptr
  Dim myApp as Ptr =  sharedApplication( NSClassFromString( "NSApplication" ) )
  declare sub activateIgnoringOtherApps lib "Cocoa" selector "activateIgnoringOtherApps:" ( appRef as Ptr, flag as boolean )
  activateIgnoringOtherApps( myApp, True )

Carbon:

  Declare function GetCurrentProcess lib CarbonLib (ByRef psn as UInt64) as Integer
  Declare function SetFrontProcess lib CarbonLib (ByRef psn as UInt64) as Integer
  Dim psn as UInt64
  
  Call GetCurrentProcess (psn)
  Call SetFrontProcess (psn)

[quote=28039:@Albin Kiland]Just changed it a bit “cocoaLibrary” > “Cocoa”.
Thank you for that![/quote]
Oops… sorry.

Hi,
Thank you very much, I used before AppleScript but it was long.

In RealStudio (I’m running Snow Leopard), I’ve to write this :
lib CarbonLib -> lib “Carbon”

[code] Declare function GetCurrentProcess lib “Carbon” (ByRef psn as UInt64) as Integer
Declare function SetFrontProcess lib “Carbon” (ByRef psn as UInt64) as Integer
Dim psn as UInt64

  Call GetCurrentProcess (psn)
  Call SetFrontProcess (psn)
  [/code]

Is there a Windows equivalent to this?

TIA Hanspeter

Bump on the Windows equivalent …

The best I have is my RemoteControlMBS.WinBringWindowToTop function in the MBS Plugin.
Brings as far as I know every window to front. (Where window.show often fails)

[quote=61584:@Hanspeter Bleuler]Is there a Windows equivalent to this?
TIA Hanspeter[/quote]

[quote=127837:@Christian Schmitz]The best I have is my RemoteControlMBS.WinBringWindowToTop function in the MBS Plugin.
Brings as far as I know every window to front. (Where window.show often fails)[/quote]

Follow up in https://forum.xojo.com/15571-set-app-to-frontmost-app in the Windows channel.