Platforms to show: All Mac Windows Linux Cross-Platform
/MacCocoa/NSStatusItem/Name in menu bar
Required plugins for this example: MBS MacCF Plugin, MBS MacCocoa Plugin, MBS MacBase Plugin, MBS Util Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSStatusItem/Name in menu bar
This example is the version from Sun, 17th Mar 2012.
Project "Name in menu bar.xojo_binary_project"
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
MenuItem AppleAboutthisapplication = "About this application..."
End MenuBar
Class App Inherits Application
EventHandler Sub Close()
if s<>nil then
s.Close
DelayMBS 0.2 // wait for events to flush
s=nil
end if
End EventHandler
EventHandler Sub Open()
dim f as FolderItem
dim period as integer
dim d as NSMenuItemMBS
pref.Load
s=new NSStatusItemMBS
// Create statusitem
call s.CreateMenu
s.HighlightMode=true
s.Title="?"
// create a menu to attach to the statusitem
m=new NSMenuMBS
// Create menu items
VisitWebsiteMenu=new MyCocoamenuitem
VisitWebsiteMenu.CreateMenuItem "Visit website"
VisitWebsiteMenu.ID=2
m.AddItem VisitWebsiteMenu
d=new NSMenuItemMBS
d.CreateSeparator
m.AddItem d
UpdateMenu=new MyCocoamenuitem
UpdateMenu.CreateMenuItem "Update now"
UpdateMenu.ID=3
m.AddItem UpdateMenu
DisplayMenu=new MyCocoamenuitem
DisplayMenu.CreateMenuItem "Short Name"
DisplayMenu.id=10
if pref.ShortName then
DisplayMenu.State=1
else
DisplayMenu.State=0
end if
m.AddItem DisplayMenu
d=new NSMenuItemMBS
d.CreateSeparator
m.AddItem d
QuitMenu=new MyCocoamenuitem
QuitMenu.CreateMenuItem "Quit"
QuitMenu.ID=1
m.AddItem QuitMenu
// attach menu
s.Menu=m
update
End EventHandler
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
quit
Return true
End EventHandler
Protected Function GetLongName() As string
Return systeminformationMBS.username
Exception
Return "?"
End Function
Protected Function GetShortName() As string
Return systeminformationMBS.ShortUsername
Exception
Return "?"
End Function
Sub update()
if s<>nil then
if pref.ShortName then
s.Title=NewCFStringMBS(GetShortName)
else
s.Title=NewCFStringMBS(GetLongName)
end if
end if
Exception
End Sub
Property Private DisplayMenu As myCocoamenuitem
Property QuitMenu As MyCocoamenuitem
Property UpdateMenu As mycocoamenuitem
Property VisitWebsiteMenu As mycocoamenuitem
Property Protected m As NSmenumBS
Property Protected s As NSStatusItemMBS
End Class
Class MyCocoamenuitem Inherits NSMenuItemMBS
EventHandler Sub Action()
Select case id
case 1
quit
case 2
ShowURL "http://www.monkeybreadsoftware.de"
case 3
app.update
case 10
pref.ShortName=not pref.ShortName
if pref.ShortName then
me.State=1
else
me.State=0
end if
app.update
end Select
End EventHandler
Property ID As integer
End Class
Module Pref
Protected Sub Load()
dim p as CFPreferencesMBS
dim o as CFObjectMBS
dim b as CFBooleanMBS
p=new CFPreferencesMBS
o=p.CopyAppValue(NewCFStringMBS("ShortName"),p.kCFPreferencesCurrentApplication)
if o isa CFBooleanMBS then
b=CFBooleanMBS(o)
if b<>nil then
myShortName= b.Value
end if
end if
Exception
End Sub
Private Sub Save(key as CFStringMBS, value as CFObjectMBS)
dim p as CFPreferencesMBS
p=new CFPreferencesMBS
p.SetAppValue key,value,p.kCFPreferencesCurrentApplication
if p.AppSynchronize(p.kCFPreferencesCurrentApplication) then
end if
End Sub
Protected Function ShortName() As boolean
Return myShortName
End Function
Protected Sub ShortName(assigns b as boolean)
dim key as CFStringMBS
dim value as CFObjectMBS
myShortName=b
key=NewCFStringMBS("ShortName")
value=NewCFBooleanMBS(b)
Save key,value
End Sub
Property Private myShortName As boolean
End Module
End Project
The items on this page are in the following plugins: MBS MacCocoa Plugin.