Dynamic Separator In Popup Menu.

Is it possible to create a separator dynamically in a popup menu? I have searched and no one has a solution.

It takes some Obj-C code to do this.

TWSeparator.h file:

/* TWSeparator */

#import <Cocoa/Cocoa.h>

@interface TWSeparator : NSObject {}
+(void)createSeparatorItem:(NSMenu *)theMenu;
@end


TWSeparator.m file:

#import “TWSeparator.h”

@implementation TWSeparator

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

@end

This is a handler that I used in an application I wrote. “theStrings” is a property list containing all the menu item titles I want for the popup button. Anytime “–” is encountered in the list, a separator item is created.

to BuildPopupMenus(thePopup)
	delete every menu item of menu of thePopup
	repeat with x in theStrings
		if contents of x = "--" then
			tell menu of thePopup
				set s to call method "createSeparatorItem:" of class "TWSeparator" with parameter it
			end tell
		else
			make new menu item at the end of menu items of menu of thePopup with properties {title:x, enabled:true}
		end if
	end repeat
end BuildPopupMenus

Hope this helps,
Brad Bumgarner, CTA

There is a search feature at macscripter, located in the navigation bar at the top of the page, that you should try before posting another duplicate thread. A simple search for “menu separator” turned up two solid hits with this same code…

No wonder I didn’t find those links, I searched for “popup AND separatER”:lol:

Brad Bumgarner, CTA

Thank you very much.

@jobu
The first link is perfect.
http://bbs.applescript.net/viewtopic.php?id=15565