Platforms to show: All Mac Windows Linux Cross-Platform
/MacOSX/Accessibility services/Control Menu
Required plugins for this example: MBS MacCF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacOSX/Accessibility services/Control Menu
This example is the version from Thu, 6th Feb 2013.
Project "Control Menu.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
if PressMenu("File","Test") then
'ok
end if
End EventHandler
End Control
EventHandler Sub EnableMenuItems()
FileTest.Enable
End EventHandler
EventHandler Function KeyDown(Key As String) As Boolean
if asc(Key)=32 then
if OpenMenu("File") then
'ok
end if
Return true
end if
End EventHandler
EventHandler Sub Open()
End EventHandler
Function FileTest() As Boolean
MsgBox "Menu entry File:Test selected."
Return true
End Function
Function AXUIElement(v as cfobjectMBS) As axuIElementMBS
dim e as AXUIElementMBS
if v<>Nil then
if v.Type=AccessibilityMBS.kAXUIElementMBSTypeID then
e=new AXUIElementMBS
e.Handle=v.Handle
e.RetainObject
e.RetainObject
Return e
end if
end if
End Function
Function CFString(c as cfobjectMBS) As cfstringMBS
dim s as CFStringMBS
if c<>Nil then
// that is the new easy way
if c isa CFStringMBS then
Return CFStringMBS(c)
end if
// if it fails we have a strange CFObject containing a CFString without the plugins knowing that
if c.Type=kCFStringMBSTypeID then
s=new CFStringMBS
s.Handle=c.Handle
s.RetainObject
Return s
end if
end if
End Function
Function OpenMenu(menuname as string) As boolean
dim e as AXUIElementMBS
dim applicationElement,MenubarElement,mie,fmie,ffmie as AXUIElementMBS
dim ca1,ca2,ca3 as CFArrayMBS
dim c1,i1,c2,i2,c3,i3 as integer
dim co as CFObjectMBS
dim cs as CFStringMBS
// get frontmost application
e=AccessibilityMBS.SystemWideAXUIElement
co=e.AttributeValue(AccessibilityMBS.kAXFocusedApplicationAttribute)
applicationElement=AXUIElement(co)
// get menubar
co=applicationElement.AttributeValue(AccessibilityMBS.kAXMenuBarAttribute)
MenubarElement=AXUIElement(co)
// get items in menubar
c1=MenubarElement.GetAttributeValueCount(AccessibilityMBS.kAXChildrenAttribute)
ca1=MenubarElement.AttributeValues(AccessibilityMBS.kAXChildrenAttribute,0,c1)
c1=Ca1.Count-1
for i1=0 to c1
co=ca1.Item(i1)
mie=AXUIElement(co)
co=mie.AttributeValue(AccessibilityMBS.kAXTitleAttribute)
cs=cfstring(co)
if cs<>nil and cs.str=menuname then
'We found the menu!
'CFShowMBS mie.ActionNames
mie.PerformAction AccessibilityMBS.kAXPickAction
// Strange, but a menubar items seems to be able to have several menus
c2=mie.GetAttributeValueCount(AccessibilityMBS.kAXChildrenAttribute)
ca2=mie.AttributeValues(AccessibilityMBS.kAXChildrenAttribute,0,c2)
c2=Ca2.Count-1
for i2=0 to c2
co=ca2.Item(i2)
fmie=AXUIElement(co)
fmie.PerformAction AccessibilityMBS.kAXPickAction
'CFShowMBS fmie.ActionNames
Return true
next
exit
end if
next
Exception
End Function
Function PressMenu(menuname as string, menuitem as string) As boolean
dim e as AXUIElementMBS
dim applicationElement,MenubarElement,mie,fmie,ffmie as AXUIElementMBS
dim ca1,ca2,ca3 as CFArrayMBS
dim c1,i1,c2,i2,c3,i3 as integer
dim co as CFObjectMBS
dim cs as CFStringMBS
// get frontmost application
e=AccessibilityMBS.SystemWideAXUIElement
co=e.AttributeValue(AccessibilityMBS.kAXFocusedApplicationAttribute)
applicationElement=AXUIElement(co)
CFShowMBS NewCFStringMBS("Application Element:")
CFShowMBS applicationElement
// get menubar
co=applicationElement.AttributeValue(AccessibilityMBS.kAXMenuBarAttribute)
MenubarElement=AXUIElement(co)
CFShowMBS NewCFStringMBS("Menubar Element:")
CFShowMBS MenubarElement
// get items in menubar
c1=MenubarElement.GetAttributeValueCount(AccessibilityMBS.kAXChildrenAttribute)
ca1=MenubarElement.AttributeValues(AccessibilityMBS.kAXChildrenAttribute,0,c1)
c1=Ca1.Count-1
for i1=0 to c1
co=ca1.Item(i1)
mie=AXUIElement(co)
co=mie.AttributeValue(AccessibilityMBS.kAXTitleAttribute)
CFShowMBS co
cs=cfstring(co)
if cs<>nil then
if cs.str=menuname then
'We found the menu!
// Strange, but a menubar items seems to be able to have several menus
c2=mie.GetAttributeValueCount(AccessibilityMBS.kAXChildrenAttribute)
ca2=mie.AttributeValues(AccessibilityMBS.kAXChildrenAttribute,0,c2)
c2=Ca2.Count-1
for i2=0 to c2
co=ca2.Item(i2)
fmie=AXUIElement(co)
c3=fmie.GetAttributeValueCount(AccessibilityMBS.kAXChildrenAttribute)
ca3=fmie.AttributeValues(AccessibilityMBS.kAXChildrenAttribute,0,c3)
c3=Ca3.Count-1
for i3=0 to c3
co=ca3.Item(i3)
ffmie=AXUIElement(co)
co=ffmie.AttributeValue(AccessibilityMBS.kAXTitleAttribute)
cs=cfstring(co)
if cs<>nil and cs.str=menuitem then
ffmie.PerformAction(AccessibilityMBS.kAXPressAction)
Return true // success
end if
next
next
exit
end if
end if
next
Exception
End Function
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileTest = "Test"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacOSX Plugin.