Application Is Always Running

Mac OSX specific forum
User avatar
mk-soft
Always Here
Always Here
Posts: 6554
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Application Is Always Running

Post by mk-soft »

Here's my first tip for mac to prevent an app is started twice

Code: Select all

;-TOP

; Comment   : IsRunning
; Author    : mk-soft
; Version   : v1.01
; Date      : 05/11/2014

Procedure IsRunning(App.s)
  
  Protected RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
  Protected RunningAppsCount = CocoaMessage(0, RunningApps, "count")
  Protected RunningApp, AppName.s
  Protected i, count
  
  While i < RunningAppsCount
    RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
    AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
    If AppName = App
      count + 1
    EndIf
    i + 1
  Wend
  If count > 1
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure

; Test
CompilerIf #PB_Compiler_IsMainFile
  
  If IsRunning("MyApp")
    End
  EndIf
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 100, "MyApp")
    
    Repeat
      event = WaitWindowEvent()
    Until event = #PB_Event_CloseWindow
    
  EndIf
  
CompilerEndIf
GT :wink:
My Projects EventDesigner V3 / ThreadToGUI / OOP-BaseClass / Windows: Module ActiveScript
PB v3.30 / v5.75 - OS Mac Mini - VM Window Pro / Linux Ubuntu
Downloads on my OneDrive