Hey there!
I have a menulet working but i’m trying to dynamically refresh the status item’s title (as well as some menu items) from applescript.
I have tried calling cocoa methods from AS but with no success and can never get the title to change. I know my cocoa call works because if I place it right in the (void)awakeFromNib part of my script (where the NSStatusItem is being constructed) it works just fine.
Can someone help me figure this one out, I’m at a loss here…
Thanks!
Here is my code:
//MyMenu.m
@implementation MyMenu
- (void)awakeFromNib
{
//Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setHighlightMode:YES];
[statusItem setEnabled:YES];
//Sets the tooltip of our NSStatusItem
[statusItem setToolTip:@"My Menu!"];
//[statusItem setTarget:self];
//Sets the title of our NSStatusItem
NSUserDefaults * defaults;
defaults = [NSUserDefaults standardUserDefaults];
NSString *currScene =[defaults stringForKey:@"currTest"] ;
[statusItem setTitle:[NSString stringWithFormat:@" %@", currTest]];
//Used to detect where our files are
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"IPMenuIcon" ofType:@"tif"];
menuIcon= [[NSImage alloc] initWithContentsOfFile:path];
//Sets the images in our NSStatusItem
[statusItem setImage];
[menuIcon release];
//Tells the NSStatusItem what menu to load
[statusItem setMenu:theMenu];
}
-(void)dealloc
{
[[NSUserDefaults standardUserDefaults] synchronize];
[statusItem release];
[super dealloc];
}
-(void)updateMenu
{
//Sets the title of our NSStatusItem
NSUserDefaults *defaults;
defaults = [NSUserDefaults standardUserDefaults];
NSString *currScene =[defaults stringForKey:@"currTest"] ;
[statusItem setTitle:[NSString stringWithFormat:@" %@", currTest]];
//[defaults synchronize];
//NSLog(@"Hello there!");
}
@end
//MyMenu.h
@interface MatteMenu : NSObject {
// Our outlets which allow us to access the interface
IBOutlet NSMenu *theMenu;
// The other stuff
NSStatusItem *statusItem;
NSImage *menuIcon;
}
// Our IBAction which will call the updateMenu2 method when our connected Menu Item is pressed
- (void)updateMenu;
@end
-- The actions for the menu item that would refresh the Menu and NSStatusItem's title
on choose menu item theObject
set object_name to name of theObject as string
if object_name = "menuItem1" then
call method "updateMenu:" of class "MyMenu"
end if