I have it to the point where the image file I pass to `TestMyMenu` appears in the system menu bar for a brief second and then disappears. I assume that is because all of the objects I create vanish once `TestMyMenu()` finishes executing. I'm hoping that someone with more experience than I in this area can review the code I have so far and do the following:
1) Correct mistakes in my use of ObjcId vs ObjcRetainedId.
2) Suggest the correct way to manage the variables containing references to `NSStatusBar`, `NSStatusItem`, `NSStatusBarButton`, `NSMenu`, `NSMenuItem`, and `NSImage`. I'm guessing that `NSStatusItem` and `NSMenu` need to be script local variables. I don't even have an educated guess about the others.
Once I can get this basic test functioning so that it is persistent then I will go back and flesh out a proper API.
Thanks in advance.
Code: Select all
library com.livecode.extensions.devore.macstatusmenu
	use com.livecode.foreign
	use com.livecode.objc
	metadata title is "Mac Status Menu"
	metadata author is "Trevor DeVore"
	metadata version is "0.1.0"
	/*
	Pointer
	CBool
	CUInt
	CInt
	CFloat
	CDouble
	ZStringNative
	ObjcId
	ObjcRetainedId
	ObjcAutoreleasedId
	-1 = NSVariableStatusItemLength
	-2 = NSSquareStatusItemLength
	*/
	private foreign handler NSSystemStatusBar() returns ObjcId binds to "objc:NSStatusBar.+systemStatusBar"
	private foreign handler NSStatusItemCreate(in pStatusBar as ObjcId, in pLength as CFloat) returns ObjcId binds to "objc:NSStatusBar.-statusItemWithLength:"
	private foreign handler NSStatusItemButton(in pObj as ObjcId) returns ObjcId binds to "objc:NSStatusItem.button"
	private foreign handler NSStatusItemSetTitle(in pObj as ObjcId, in pTitle as ObjcId) returns nothing binds to "objc:NSStatusItem.-setTitle:"
	private foreign handler NSStatusItemSetHighlightMode(in pObj as ObjcId, in pBool as CBool) returns nothing binds to "objc:NSStatusItem.-setHighlightMode:"
	private foreign handler NSStatusItemSetMenu(in pObj as ObjcId, in pMenuObj as ObjcId) returns nothing binds to "objc:NSStatusItem.-setMenu:"
	private foreign handler NSStatusBarButtonSetTitle(in pObj as ObjcId, in pTitle as ObjcId) returns nothing binds to "objc:NSStatusBarButton.-setTitle:"
	private foreign handler NSStatusBarButtonSetImage(in pObj as ObjcId, in pImage as ObjcId) returns nothing binds to "objc:NSStatusBarButton.-setImage:"
	private foreign handler NSImageAlloc() returns ObjcRetainedId binds to "objc:NSImage.+alloc"
	private foreign handler NSImageCreateFromFile(in pObj as ObjcId, in pFilename as ObjcId) returns ObjcId binds to "objc:NSImage.-initByReferencingFile:"
	private foreign handler NSMenuAlloc() returns ObjcRetainedId binds to "objc:NSMenu.+alloc"
	private foreign handler NSMenuCreate(in pObj as ObjcId, pTitle as ObjcId) returns ObjcId binds to "objc:NSMenu.-initWithTitle:"
	private foreign handler NSMenuAddItem(in pObj as ObjcId, pItem as ObjcId) returns nothing binds to "objc:NSMenu.-addItem:"
	private foreign handler NSMenuNumberOfItems(in pObj as ObjcId) returns CInt binds to "objc:NSMenu.-numberOfItems"
	private foreign handler NSMenuItemAlloc() returns ObjcRetainedId binds to "objc:NSMenuItem.+alloc"
	private foreign handler NSMenuItemCreate(in pObj as ObjcId, pTitle as ObjcId, pAction as optional ObjcId, pKeyEquivalent as ObjcId) returns ObjcId binds to "objc:NSMenuItem.-initWithTitle:action:keyEquivalent:"
	public handler TestMyMenu(in pImagePath as String)
		unsafe
			// Menu
			variable tMenu as ObjcObject
			put NSMenuAlloc() into tMenu
			put NSMenuCreate(tMenu, StringToNSString("My Menu")) into tMenu
			// Menu Item
			variable tMenuItem as ObjcObject
			put NSMenuItemAlloc() into tMenuItem
			put NSMenuItemCreate(tMenuItem, StringToNSString("Option 1"), nothing, StringToNSString("")) into tMenuItem
			NSMenuAddItem(tMenu, tMenuItem)
			variable tCount as Integer
			put NSMenuNumberOfItems(tMenu) into tCount
			log tCount
			// Status Bar
			variable statusBar as ObjcObject
			variable tItem as ObjcObject
			put NSSystemStatusBar() into statusBar
			put NSStatusItemCreate(statusBar, -2) into tItem
			NSStatusItemSetTitle(tItem, StringToNSString("Testing"))
			NSStatusItemSetHighlightMode(tItem, true)
			NSStatusItemSetMenu(tItem, tMenu)
			variable tImage as ObjcObject
			put NSImageAlloc() into tImage
			put NSImageCreateFromFile(tImage, StringToNSString(pImagePath)) into tImage
			// Status Item
			variable tStatusItemButton as ObjcObject
			put NSStatusItemButton(tItem) into tStatusItemButton
			NSStatusBarButtonSetImage(tStatusItemButton, tImage)
			NSStatusBarButtonSetTitle(tStatusItemButton, StringToNSString("My Button!"))
		end unsafe
	end handler
end library

