macoslib NSStatusItem?

Hi,
I am experimenting with the excellent macoslib, and would like to make a simple modification.

I have the following code in the NSStatusItem’s open event, which creates an NSStatusItem with one option:

[code] dim bar as NSStatusBar = NSStatusBar.SystemStatusBar
item1 = bar.CreateStatusItem(NSStatusBar.NSVariableStatusItemLength, AddressOf StatusItemHandler)
item1.title = “MyApp”
item1.highlightMode = True
item1.Image = NSImage.StatusAvailable
item1.AlternateImage = NSImage.StatusPartiallyAvailable
item1.ToolTip = “This is a NSStatusItem”

dim mmenu1 as new NSMenu
call mmenu1.Append(“Option 1”)

item1.menu = mmenu1[/code]

And the following code in the StatusItemHandler method, which appends the selected item into a text area:

TextArea1.appendText "Selected menu """+hitItem.Title+""""+endOfLine

Could someone please tell me what I need to change in order to have NO items in the NSStatusItem, and have the method execute when I click on the actual NSStatusItem icon in the OS X menubar (as opposed to item 1)?

I obviously have to delete the call mmenu1.Append(“Option 1”) line of code, but am not sure how or where I put the required modification?

Thanks in advance.

I think* highlightMode needs to be false. I don’t know how you handle the click action with Xojo.

AlternateImage is used for the clicked icon though, I don’t know what PartiallyAvailable means; just a heads up that AlternateImage isn’t an indeterminate state.

*From old memory, haven’t checked, researched, or even opened my NSStatusItem xcode project.

Not able to edit post, but I just remembered what highlightMode was for.
It’s the automatic inverted state (blue background for the status item and displaying AlternateImage)

I’ve done this…
You’ll need to set

item1.Action=Cocoa.NSSelectorFromString("menuAction:")

I don’t think there’s more to it, but I could be mistaken… I think the item1.menu needs to be set to nil also? hmm

I think he wants a non-menu status item.

Yes - A non-menu status item.
Basically just the icon in the OS X menubar, and when clicked on, opens a window.

Right, you have to set the menu to nil, then set the action for the statusItem rather than a menu. The actionDelegate will fire when you click the status icon.

Jim,
do you mean like this:

[code]dim bar as NSStatusBar = NSStatusBar.SystemStatusBar
item1 = bar.CreateStatusItem(NSStatusBar.NSVariableStatusItemLength, AddressOf StatusItemHandler)
item1.title = “MyApp”
item1.highlightMode = True
item1.Image = NSImage.StatusAvailable
item1.AlternateImage = NSImage.StatusPartiallyAvailable
item1.ToolTip = “This is a NSStatusItem”

dim mmenu1 as new NSMenu
item1.menu = nil
item1.Action=Cocoa.NSSelectorFromString(“menuAction:”)[/code]

I have a project that does this. Let me see if I can find it and clean up the code.

Right… you don’t need to create a menu though. This line is not needed…

[quote]dim mmenu1 as new NSMenu
[/quote]

did that work?

Ok,
I have changed my code to that below:

[code]dim bar as NSStatusBar = NSStatusBar.SystemStatusBar
item1 = bar.CreateStatusItem(NSStatusBar.NSVariableStatusItemLength, AddressOf StatusItemHandler)
item1.title = “MyApp”
item1.highlightMode = True
item1.Image = NSImage.StatusAvailable
item1.AlternateImage = NSImage.StatusPartiallyAvailable
item1.ToolTip = “This is a NSStatusItem”

item1.menu = nil
item1.Action=Cocoa.NSSelectorFromString(“menuAction:”)[/code]

But - I have not tried this yet because I have no idea how to edit this part of the code - b[/b], in order to open the window?

Here is a link to the cleaned up project:
https://www.dropbox.com/s/xpgwzkjfct04ifj/StatusItemView.xojo_binary_project

and the accompanying (boring) application image:
https://www.dropbox.com/s/up97bq2yeppbe5v/applicationImage%20.tiff

Thank you very much Jason - I will take a look at that.
Much appreciated.

Jim,
if you could still let me know what I change in my post above - that would also be appreciated.

Nice to find different ways of achieving the same outcome :slight_smile:

In your StatusItemHandler method, you can just do

myWindow.show

Thanks.

Just so that I have fully understood what is happening here:
Does (“menuAction:”) refer to the StatusItemHandler code?

UPDATE - just tried that and nothing happens when you click on the NSStatusItem :frowning:

The code in the Window1 open event is as follows:

dim bar as NSStatusBar = NSStatusBar.SystemStatusBar item1 = bar.CreateStatusItem(NSStatusBar.NSVariableStatusItemLength, AddressOf StatusItemHandler) item1.title = "MyApp" item1.highlightMode = True item1.Image = NSImage.StatusAvailable item1.AlternateImage = NSImage.StatusPartiallyAvailable item1.ToolTip = "This is a NSStatusItem" item1.menu = nil item1.Action=Cocoa.NSSelectorFromString("menuAction:")

And this code is in the StatusItemHandler method (I decided to simply close the main window, instead of opening another):

Window1.close

Um, I think I may have altered my copy of macoslib… I’m gonna check it out…

yes… I added a line

Target=target_id

to action.set in the NSStausItem class… to allow the menuless statusitem to be clickable

I thought there might be more to it. I was planning on submitting the change to the repository, but haven’t done it.

Thanks Jim - that worked :slight_smile:

One final question:
Is it acceptable to leave the StatusItem in the OS X menubar after my app has quit, and then if the StatusItem is clicked, use an if statement to determine if my app is open, and if not - open it?

Or is there some reason this would not work / should not be performed?

Thank you.

The statusItem has to be owned by a process, could be your app, or a helper app. In either case the StatusItem will close when the owner quits. If you create it from a helper app, the helper could be made to relaunch at every login and only open the main app when the user clicks the StatusItem. That’s how iChat does it and seems to be the preferred solution if that’s what you’re looking to do.

That’s a bit beyond me at the moment.
I will start of with the StatusItem closing when my app closes.

I may then look into how to make a helper app start upon login, (which only opens my app when clicked on).

Thanks.