RunProgram - How to give focus to a launched app?

Mac OSX specific forum
MBall
User
User
Posts: 12
Joined: Wed Dec 18, 2019 11:33 pm

RunProgram - How to give focus to a launched app?

Post by MBall »

Hi,

When I use RunProgram on OSX, the program that is launched always has background focus.
On Windows Launched applications seem to always get foreground / active focus.

So on OSX, Is there a way to give an application foreground / active focus when I use RunProgram. or is there a way to give an application focus from purebasic once the application is started with RunProgram.


thanks in advance

Martin
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: RunProgram - How to give focus to a launched app?

Post by mk-soft »

I need little time... I'm not Cocoa Profi

Code: Select all

#NSApplicationActivateAllWindows = 1 << 0
#NSApplicationActivateIgnoringOtherApps = 1 << 1

Procedure ShowNamedWindow(Name.s, IsThread = #False) ;  ; Result = Count of Windows
  Protected RunningApps.i, RunningAppsCount.i, RunningApp.i, AppName.s, i, cnt, Pool
  
  If IsThread
    Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  EndIf
  
  RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
  RunningAppsCount = CocoaMessage(0, RunningApps, "count")
  i = 0
  While i < RunningAppsCount
    RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
    AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
    If Name = AppName
      CocoaMessage(0, RunningApp, "activateWithOptions:", #NSApplicationActivateIgnoringOtherApps)
      cnt + 1
    EndIf
    i + 1
  Wend
  
  If IsThread
    CocoaMessage(0, Pool, "release")
  EndIf
  
  ProcedureReturn cnt
EndProcedure

ShowNamedWindow("Safari")
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply