menu separator / divider

how do you make a menu divider dynamically? i tried the following and it just used the name i gave it (im trying to get it to turn anything with a dash into a divider


if i does not contain "-" then
make new menu item at the end of menu items with properties {name:i, title:i, enabled:true}
else
make new menu item at the end of menu items with properties {separator item:true, enabled:true, title:i}
end if

As far as I can tell, there’s no pure applescript method of creating a separator item. You’ll have to use a custom cocoa class.

Cocoa class named “JBMenu.m” (no .h file required)…

//  JBMenu.m

#import <Cocoa/Cocoa.h>

@interface JBMenu : NSObject
+(void)createSeparatorItem:(NSMenu *)theMenu;
@end

@implementation JBMenu
+(void)createSeparatorItem:(NSMenu *)theMenu {
	[theMenu addItem:[NSMenuItem separatorItem]];
}
@end

Applescript code; creates a test menu in the ‘launched’ handler of the app…

on launched theObject
	(* Add a menu to the main menu *)
	set myMenu to make new menu at end of menus of main menu with properties {name:"myMenu", title:"myMenu"}
	
	(* Add a menu item to the menu *)
	make new menu item at end of menu items of myMenu with properties {name:"Item 1", title:"Item 1", enabled:true}
	
	(* Add a separator Item *)
	set separatorItem to call method "createSeparatorItem:" of class "JBMenu" with parameters {myMenu}
	
	(* Add a menu item to the menu *)
	make new menu item at end of menu items of myMenu with properties {name:"Item 2", title:"Item 2", enabled:true}
end launched

Hope that gets you there…
j

i get several “stray 240” and “stay 310” errors on line 11 and it won’t build…