Single Task App Switch Menu Code

Mac OSX specific forum
rambodash
User
User
Posts: 21
Joined: Thu Jun 13, 2013 1:00 am

Single Task App Switch Menu Code

Post by rambodash »

sharing the code for attempt to create the classic app switcher. I tried to mimic the behaviour of the classic Mac OS Task switch menu but could not figure out how to hide/close the Finder (when app switching finder will stay in view if window is open) but here it is:

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)

User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Single Task App Switch Menu Code

Post by fsw »

I've never used the classic macOS, so I'm a little bit lost here.
Can you (or someone else) please explain further?
Thanks

I am to provide the public with beneficial shocks.
Alfred Hitshock
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: Single Task App Switch Menu Code

Post by Wolfram »

The Classic MacOS hasn't a dock, it has an menu in the upper right corner to switch between all running programs.
(Program is an old word for App) ;-)
macOS Catalina 10.15.7
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Single Task App Switch Menu Code

Post by fsw »

Wolfram wrote:(Program is an old word for App) ;-)
Yeah, I remember programs.
Used to use programs on DOS 2.11 :mrgreen:
Thanks

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply