Page 1 of 1

RunProgram - How to give focus to a launched app?

Posted: Sat Dec 28, 2019 1:06 pm
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

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

Posted: Sat Dec 28, 2019 1:58 pm
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")