NSWorkspace also has a notification
NSWorkspaceDidActivateApplicationNotification
It's a bit more complicated to use but allows you to get notified when the active application changes.
Code: Select all
EnableExplicit
; *** import the required functions ***
ImportC ""
sel_registerName(str.p-ascii)
class_addMethod(class, selector, imp, types.p-ascii)
EndImport
; *** required variables ***
Global notificationCenter = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "notificationCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")
Define selector
; *** Active application changed callback ***
ProcedureC ApplicationActivated(obj, sel, notification)
Protected RunningApp = CocoaMessage(0, CocoaMessage(0, notification, "userInfo"), "objectForKey:$", @"NSWorkspaceApplicationKey")
Protected AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
Protected IconSize.NSSize, Icon = CocoaMessage(0, RunningApp, "icon")
IconSize\width = 64 : IconSize\height = 64 : CocoaMessage(0, Icon, "setSize:@", @IconSize)
SetGadgetState(0, Icon)
SetGadgetText(1, AppName)
EndProcedure
; *** main code ***
If OpenWindow(0, 0, 0, 320, 84, "Active application demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StickyWindow(0, 1)
ImageGadget(0, 10, 10, 64, 64, 0)
StringGadget(1, 84, 20, 226, 20, "", #PB_String_ReadOnly)
selector = sel_registerName("ApplicationActivated:")
class_addMethod(delegateClass, selector, @ApplicationActivated(), "v@:@")
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
PB 5.40+
Code: Select all
EnableExplicit
; *** required variables ***
Global notificationCenter = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "notificationCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")
Define selector
; *** Active application changed callback ***
ProcedureC ApplicationActivated(obj, sel, notification)
Protected RunningApp = CocoaMessage(0, CocoaMessage(0, notification, "userInfo"), "objectForKey:$", @"NSWorkspaceApplicationKey")
Protected AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
Protected IconSize.NSSize, Icon = CocoaMessage(0, RunningApp, "icon")
IconSize\width = 64 : IconSize\height = 64 : CocoaMessage(0, Icon, "setSize:@", @IconSize)
SetGadgetState(0, Icon)
SetGadgetText(1, AppName)
EndProcedure
; *** main code ***
If OpenWindow(0, 0, 0, 320, 84, "Active application demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StickyWindow(0, 1)
ImageGadget(0, 10, 10, 64, 64, 0)
StringGadget(1, 84, 20, 226, 20, "", #PB_String_ReadOnly)
selector = sel_registerName_("ApplicationActivated:")
class_addMethod_(delegateClass, selector, @ApplicationActivated(), "v@:@")
CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)