Wednesday, November 20, 2019

#1 2007-04-30 09:39:24 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Icon in menubar - a working solution [NSStatusItem]

Hi all,

I've managed to get an icon in the menubar using some simple Cocoa. It does almost all that I want, except I can't seem to get the menu to work...

EDIT: updated the script taking into account the comments and corrections from this thread!!!


Here are the steps needed:

1. Add in your Xcode project a new file Cocoa, Objective-C Class and name it for this purpose "Menulet". This should create two files: Menulet.h and Menulet.m

2. In Menulet.h replace the existing code with:

Applescript:

#import <Cocoa/Cocoa.h>


@interface Menulet : NSObject {
}
+ (void)showMenuIcon;
@end

3. In Menulet.m replace the existing code with:

Applescript:

#import "Menulet.h"


@implementation Menulet

+ (void)showMenuIcon
{
NSStatusItem *statusItem;
       NSMenu        *theMenu;
statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength]
retain];
    [statusItem setImage:[NSImage imageNamed:@"idle"]];
[statusItem setHighlightMode:YES];
[statusItem setTitle:[NSString
stringWithString:@""]];
[statusItem setEnabled:YES];
[statusItem setToolTip:@""];

       theMenu = [[NSMenu alloc] initWithTitle:@""];
       [theMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"Q"];
       [statusItem setMenu:theMenu];
       [theMenu release];
}
@end

3. Add a small image (I used size 17 by 17 and named it idle.tif) to your project (easiest is to drag it into IB)

4. In your AppleScript you can now activate the menu bar icon by using:

Applescript:

call method "showMenuIcon" of class "Menulet"

Build and run...

Being a complete idiot with Cocoa I may have ignored some of its rules, so if anyone can correct this I would be grateful.

Last edited by Stadsman (2007-05-05 07:52:51 pm)

Offline

 

#2 2007-05-04 03:09:25 am

Captainhams
Member
Registered: 2007-04-25
Posts: 116

Re: Icon in menubar - a working solution [NSStatusItem]

This is great work! big_smile
But i cant help you with your problem, could you give us more information? the menubar works fine for me!

Last edited by Captainhams (2007-05-04 03:11:32 am)

Offline

 

#3 2007-05-04 09:52:35 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

The menu bar should work fine as in the script. It''s just that I want to add selection items linked to a script.

Offline

 

#4 2007-05-04 08:54:33 pm

Captainhams
Member
Registered: 2007-04-25
Posts: 116

Re: Icon in menubar - a working solution [NSStatusItem]

Yes, i see your problem sad
Do you know of a way that a script could change part of the menubar coding? like for example something showing the status and in the script it has a function that calls it and changes it?

Cool thing anyway tongue

Offline

 

#5 2007-05-05 02:57:28 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Guys,

3. Add a small image (I used size 17 by 17 and named it idle.tif) to your project (easiest is to drag it into IB)


Having Never use Xcode, and having no clue what so ever of what I am doing.
I wanted to see what this does.

I made an applescript project. Made the new file.Changed the .h and .m file, But where do I put this icon, and how do I tell the app where to look for it.?

Cheers

Last edited by mark hunte (2007-05-05 02:57:54 am)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#6 2007-05-05 04:45:59 am

Captainhams
Member
Registered: 2007-04-25
Posts: 116

Re: Icon in menubar - a working solution [NSStatusItem]

you just need to drag any small image named idle to the "Resources" folder in your Xcode project window
no problem neutral

Offline

 

#7 2007-05-05 05:23:49 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Cheers Captainhams, I did that at a guess, as that's where I see all the images in an app package, But the icon does not show in the menu?


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#8 2007-05-05 08:51:26 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

Try the following:

When in Xcode double click "MainMenu.nib (English)", this should open the program Interface Builder with your interface (which is not really used for the above purpose). In the newly opened window "MainMenu.nib (English)" you should see several tabs on top (Instances, Classes, Images, etc.). Click on the 'Images' tab. If you see your image file you should be ok, if not drag it into this window. I used a TIFF file, I don't know whether it works with other file formats.

Hope this works for you,

S.

Last edited by Stadsman (2007-05-05 08:59:26 am)

Offline

 

#9 2007-05-05 10:31:04 am

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Icon in menubar - a working solution [NSStatusItem]

Stadsman wrote:

... menulet.h ...

[code]@interface menulet : NSObject {
    NSStatusItem *statusItem;
}
+ (void)showMenuIcon;
@end[/code]


I moved `NSStatusItem *statusItem;` to menulet.m, because I received warnings while building.

Side note: I believe it's proper to capitalize class names (e.g. Menulet instead of menulet).

Edit: This does work for me. Thanks for sharing, Stadsman. cool

Offline

 

#10 2007-05-05 11:16:25 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Bruce Phillips wrote:

Thanks for sharing, Stadsman. cool


After having received so much help from this forum, I'm happy to give a little back... smile

Offline

 

#11 2007-05-05 01:24:45 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

I did not get this working,
So I tried this Menulet introduction.
Which I think is the same type of thing this thread is about, (noob talking)

Please let me know if I should start this as a new thread.


I got most of it working, but I am stuck with the drop down menu from the icon.

Click "Build and Go" again to see the newest results. You will only see the icon in the menubar. Click on this to expand down your new menu. Click on the IP Address to have it update and change the menu item.


I can not tell if my Ipmenulet is connected to the NSMenu1 which I suspect is the problem.
It say's

Next we connect the menu to our class. Control left-click and drag from the IPMenulet instantiation to the new NSMenu1 and connect theMenu data member of the class


I build and Go - no errors.

But the menu does not drop down from the icon??

Also I changed the Ip url to "http://www.whatismyip.org" as the one provided did not work that well.
I could not build withou errors with (dealloc:) in, so I took it out.


Thanks

Last edited by mark hunte (2007-05-05 01:25:17 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#12 2007-05-05 02:00:43 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

Your link is one of the sources I based my script on. Being a total cocoa noob I managed to get various scripts together to get what I wanted tongue

Next we connect the menu to our class. Control left-click and drag from the IPMenulet instantiation to the new NSMenu1 and connect theMenu data member of the class


You have to make sure you click on 'connect' in the NSMenuInspector window (bottom right button).

Offline

 

#13 2007-05-05 03:02:56 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,
Your link is one of the sources I based my script on. Being a total cocoa noob I managed to get various scripts together to get what I wanted


Thats spooky  big_smile


When I drag from the   Ipmenulet is connected to the NSMenu1 I do not seem to get anything show up in the inspector to select and connect to???

what is it supposed to say?


Thanks


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#14 2007-05-05 03:59:49 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

Hopefully this screenshot explains it clearly...

Offline

 

#15 2007-05-05 04:10:22 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Thanks Stadsman.

I do not get that.

I must have type something wrong some where, Most likely my omission  of (dealloc :)

I did add a Outlet (myOutlet) manually and change the name to theMenu, I then set the id to NSMenu
Did the drag thing, selected connect.

But still no menu.

Is it possible for you to post the .m and .h  files for this so I can compare.

Thanks


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#16 2007-05-05 04:43:37 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

Here they are, I also had to comment out (dealloc:) to get rid of the warning but it seems not to make a difference.

IPMenulet.h:

Applescript:

#import <Cocoa/Cocoa.h>


@interface IPMenulet : NSObject {
NSStatusItem *statusItem;
   IBOutlet NSMenu *theMenu;
   NSMenuItem *ipMenuItem;
   NSImage *menuIcon;
}

-(IBAction)updateIPAddress:(id)sender;
@end

IPMenulet.m:

Applescript:

#import "IPMenulet.h"


@implementation IPMenulet
-(void)dealloc
{
[statusItem release];
[super dealloc];
}
- (void)awakeFromNib
{
statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength]
retain];
[statusItem setHighlightMode:YES];
[statusItem setEnabled:YES];
[statusItem setToolTip:@"IPMenulet"];

// [statusItem setAction:@selector(updateIPAddress:)];
// [statusItem setTarget:self];
[statusItem setMenu:theMenu];
ipMenuItem = [[NSMenuItem alloc] initWithTitle:@"0.0.0.0"
action:@selector(updateIPAddress:) keyEquivalent:@""];
    [ipMenuItem setTarget:self];
   [theMenu insertItem:ipMenuItem atIndex:1];
   NSBundle *bundle = [NSBundle bundleForClass:[self class]];
   NSString *path = [bundle pathForResource:@"IPMenuIcon" ofType:@"tif"];
   menuIcon= [[NSImage alloc] initWithContentsOfFile:path];

   [statusItem setTitle:[NSString stringWithString:@""]];

   [statusItem setImage:menuIcon];

//(dealloc:)
   [menuIcon release];
}
-(IBAction)updateIPAddress:(id)sender
{
NSString *ipAddr = [NSString stringWithContentsOfURL:
[NSURL URLWithString:
@"http://www.whatismyip.org"]];
if(ipAddr != NULL)
[ipMenuItem setTitle:
[NSString stringWithString:ipAddr]];
}
@end

Offline

 

#17 2007-05-05 06:01:48 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Thank You.
I had three issues.

First I put

Applescript:

#import <Cocoa/Cocoa.h>

@interface IPMenulet : NSObject {
NSStatusItem *statusItem;
}
-(IBAction)updateIPAddress:(id)sender;
IBOutlet NSMenu *theMenu;
NSMenuItem *ipMenuItem;
NSImage *menuIcon;
@end

As you can see the icon menu is outside the function (that may sound like I know what I am talking about, don't be fooled. smile, when I put it inside I got the correct info in the inspector.

I then found I had

Applescript:

[statusItem setMenu:theMenu];
ipMenuItem = [[NSMenuItem alloc] initWithTitle:@"0.0.0.0"
action:@selector(updateIPAddress:) keyEquivalent:@""];
[ipMenuItem setTarget:self];
[theMenu insertItem:ipMenuItem atIndex:1];

Above

Applescript:

statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength]
retain];
[statusItem setHighlightMode:YES];
[statusItem setEnabled:YES];
[statusItem setToolTip:@"IPMenulet"];

// [statusItem setAction:@selector(updateIPAddress:)];
// [statusItem setTarget:self];

Once I change these the menu came up but was greyed out.

Found  a missing "d" in

Applescript:

action:@selector(updateIPAddress:) keyEquivalent:@""];

which fixed that big_smile

It is annoying that it compiles with no errors when there are, but I guess thats the nature of the beast.
I also notice that the intro url doe not really help in the structure of how the script should be laid out.

Thanks you for your help.

mark

Last edited by mark hunte (2007-05-05 06:02:23 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#18 2007-05-05 06:14:02 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Glad I could be of help, Mark.

Now another question for the Cocoa savvy:

Can I replace the 'NULL' statement in the following line with an action to terminate the app?

Applescript:

[theMenu addItemWithTitle:@"This application is active!" action:NULL keyEquivalent:@""];

I tried various variations, such as 'NSApplication terminate', 'NSApp terminate', 'NSTerminateNow', etc, but all to no avail... sad

Offline

 

#19 2007-05-05 06:22:52 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Ha, a new Google search did the trick:

Applescript:

[theMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];

Offline

 

#20 2007-05-05 06:32:39 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Stadsman,
Do you know any good links to show me how I attach an applescript script to the menu.
Also my applescript opens up in Script Editor? I can not find a pref to keep it in xcode??

thanks again

mark


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#21 2007-05-05 06:41:16 pm

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

Do you want an AppleScript connected to the Menulet menu? Or to do that within a Xcode project. The first I don't know, the second one is set in Xcode using 'on choose menu item'.

To make an AppleScript with Xcode you have to select >New Project>AppleScript Application. Xcode can't work with just the AppleScript. Once you are in Xcode, you can double click the Applescript file within Xcode which then opens its own AS editor.

S.

Edit: Check out the Task List example is Xcode's ApleScript Studio examples for setting menu items.

Last edited by Stadsman (2007-05-05 06:45:36 pm)

Offline

 

#22 2007-05-06 04:24:22 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Do you want an AppleScript connected to the Menulet menu?


Thats the one,

Thanks I will have a look there.

Mark


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#23 2007-05-06 04:31:02 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Mark,

That example is not for adding menu items to the menulet. There is a Apple Technical Note that describes the howto, but I can't get it working. What I did manage is to get an AppleScript using 'on should quit theObject'. If you then use the 'terminate:' action the script embedded in 'on should quit theObject' gets called. It works but is not a proper solution.

I also found this link, but also couldn't get it to work.

Also found this which looks promising, haven't tried it yet...

Last edited by Stadsman (2007-05-06 04:36:29 am)

Offline

 

#24 2007-05-06 05:10:38 am

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Icon in menubar - a working solution [NSStatusItem]

Hi Stadsman,
I will have a look at does links, thanks

Because I wanted the app to be dock-less, I had a play in adding a 'Quit' menu item at the bottom.
it could not be easier.
(from the intro url I added this [code]<key>LSUIElement</key>
    <string>1</string>[/code]
in the info.plist

In the MainMenu.nib

select (in this case) the 'External Ip' item and while holding down the option key drag it down to create a new item
Rename it 'Quit'
while holding down the ctrl key, drag it to the first responder class.
In the inspector, connect it to the 'terminate'

save

thats it


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#25 2007-05-06 05:24:36 am

Stadsman
Member
From:: Amsterdam
Registered: 2006-09-27
Posts: 156

Re: Icon in menubar - a working solution [NSStatusItem]

Mark, could you email me a copy of the IPMenulet project? Somehow I'm unable to get it going again and I can't find my error.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)