Code: Select all
EnableExplicit
#POPUPMENU = 1
#IMG_ICON = 1
#ITEM_LEN = 24
#NSApplicationActivationPolicyRegular = 0
#NSApplicationActivationPolicyAccessory = 1
#NSApplicationActivationPolicyProhibited = 2
#NSSquareStatusBarItemLength = -2
;------------------------------------------------------------
UsePNGImageDecoder()
Define itemLength.CGFloat = #ITEM_LEN
Global G_QuitNum = 0
Global G_sharedapp.i = CocoaMessage(0,0,"NSApplication sharedApplication")
Global G_workspace.I = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
Global G_status_bar = CocoaMessage(0,0,"NSStatusBar systemStatusBar")
Global G_status_item = CocoaMessage(0,CocoaMessage(0,G_status_bar,"_statusItemWithLength:",#NSSquareStatusBarItemLength,"withPriority:", 2147483647),"retain")
;for app notify
Global G_ncenter = CocoaMessage(0, G_workspace, "notificationCenter")
Global G_appdelegate = CocoaMessage(0, G_sharedapp, "delegate")
Global G_delegateclass = CocoaMessage(0, G_appdelegate, "class")
;set application policy
CocoaMessage(0, G_sharedapp, "setActivationPolicy:", #NSApplicationActivationPolicyAccessory)
Procedure.s GetNSRunningAppName(appobj)
Define lname = CocoaMessage(0,appobj,"localizedName")
If lname
Define lname_str.s = PeekS(CocoaMessage(0, lname, "UTF8String"), -1, #PB_UTF8)
ProcedureReturn lname_str
Else
ProcedureReturn ""
EndIf
EndProcedure
Procedure ReBuildMenu()
Define itemLength.CGFloat = #ITEM_LEN
Define frontapp = CocoaMessage(0, G_workspace, "frontmostApplication")
Define fronticon = CocoaMessage(0, frontapp, "icon")
Protected IconSize.NSSize
IconSize\width = 20 : IconSize\height = 20 : CocoaMessage(0, fronticon, "setSize:@", @IconSize)
If IsMenu(#POPUPMENU)
If IsMenu(#POPUPMENU) : FreeMenu(#POPUPMENU) : EndIf
EndIf
CreatePopupImageMenu(#POPUPMENU)
Define runningapps = CocoaMessage(0,G_workspace,"runningApplications")
Define count = CocoaMessage(0,runningapps,"count")
Define i = 0
Define appcount = 0
For i = 0 To count-1
Define current_app = CocoaMessage(0,runningapps,"objectAtIndex:",i)
If current_app
If CocoaMessage(0,current_app,"activationPolicy") = #NSApplicationActivationPolicyRegular
Define lname = CocoaMessage(0,current_app,"localizedName")
If lname
Define icon = CocoaMessage(0, current_app, "icon")
Define lname_str.s = PeekS(CocoaMessage(0, lname, "UTF8String"), -1, #PB_UTF8)
MenuItem(appcount, lname_str,icon)
appcount + 1
EndIf
EndIf
EndIf
Next
MenuBar()
MenuItem(appcount,"Quit")
G_QuitNum = appcount
CocoaMessage(0,G_status_item,"setHighlightMode:",#YES)
CocoaMessage(0,G_status_item,"setLength:@",@itemLength)
CocoaMessage(0,G_status_item,"setImage:",fronticon)
CocoaMessage(0,G_status_item,"setMenu:",CocoaMessage(0,MenuID(#POPUPMENU),"firstObject"))
EndProcedure
ProcedureC ApplicationActivated(obj, sel, notification)
ReBuildMenu()
EndProcedure
ProcedureC ApplicationSwitched(obj, sel, notification)
EndProcedure
Procedure ActivateApp(menu_num)
ReBuildMenu()
Define runningapps = CocoaMessage(0,G_workspace,"runningApplications")
Define count = CocoaMessage(0,runningapps,"count")
Define i = 0
Define appcount = 0
Define actapp = 0
For i = 0 To count-1
Define current_app = CocoaMessage(0,runningapps,"objectAtIndex:",i)
If current_app
If CocoaMessage(0,current_app,"activationPolicy") = #NSApplicationActivationPolicyRegular
Define appname.s = GetNSRunningAppName(current_app)
If CocoaMessage(0,current_app, "isHidden") = 0 And appname <> "Finder"
CocoaMessage(0,current_app, "hide")
EndIf
If appcount = menu_num
actapp = current_app
EndIf
appcount + 1
EndIf
EndIf
Next
If actapp <> 0
CocoaMessage(0,actapp, "activateWithOptions:",2)
EndIf
EndProcedure
OpenWindow(0,#PB_Ignore,#PB_Ignore,170,40,"statusBar",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible|#PB_Window_Tool )
Define selector = sel_registerName_("ApplicationActivated:")
class_addMethod_(G_delegateclass, selector, @ApplicationActivated(), "v@:@")
CocoaMessage(0, G_ncenter, "addObserver:", G_appdelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
CocoaMessage(0, G_ncenter, "addObserver:", G_appdelegate, "selector:", selector, "name:$", @"NSWorkspaceDidDeactivateApplicationNotification", "object:", #nil)
ReBuildMenu()
Define eventnum = 0
Repeat
Select WaitWindowEvent()
Case #PB_Event_Menu
eventnum = EventMenu()
Select eventnum
Case G_QuitNum
Break
Default
ActivateApp(eventnum)
EndSelect
EndSelect
ForEver
;-------------------EXIT------------------
CocoaMessage(0,G_status_bar,"removeStatusItem:",G_status_item)
FreeMenu(#POPUPMENU)
CocoaMessage(0, G_ncenter, "removeObserver:", G_appdelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
CocoaMessage(0, G_ncenter, "removeObserver:", G_appdelegate, "name:$", @"NSWorkspaceDidDeactivateApplicationNotification", "object:", #nil)