validatemenuitem

How do you script the following objective-c code in ASOC:

I have searched this list high and low and can find no reference to validatemenuitem.

Is nobody using menus in their apps?:slight_smile:

- (BOOL)validateMenuItem:(NSMenuItem *)item
{
	if (([item action] == @selector(newLibrary:) || [item action] == @selector(openLibrary:)) && [libraryData isLibraryOpen])
	{
		return NO;
	}
	if ([item action] == @selector(addFolderToLibrary:))
	{
		return [libraryData isLibraryOpen];
	}
	if ([item action] == @selector(closeLibrary:))
	{
		return[libraryData isLibraryOpen];
	}
	if ([item action] == @selector(toggleColorAdjDrawer:))
	{
		return[libraryData isLibraryOpen];
	}
	return YES;
}

All the best

Terry

validateMenuItem: is deprecated; you’re supposed to use validateUserInterfaceItem: instead. At a guess, you would use something like:

on validateUserInterfaceItem_(anItem)
set theSel to anItem's |action|() as text
if theSel = "addFolderToLibrary:" then
return true
else...

Hi Shane,

It appears is is only deprecated on NSDocument.

Please read this thread:

http://www.cocoabuilder.com/archive/cocoa/207076-validatemenuitem-not-always-being-called.html#207076

I am in the process of trying your suggestion and will report back.

Thanks

Terry

Hi Shane,

I can confirm that both work:

	on validateUserInterfaceItem_(tMenuItem)
		
		set tSel to tMenuItem's |action|() as text
		
		if (tSel = "newAccount:") and (pAccountOpen = true) then
			return false
		end if
		
		return true
	end validateUserInterfaceItem_

or

	on validateMenuItem_(tMenuItem)
		
		set tSel to tMenuItem's |action|() as text
		
		if (tSel = "newAccount:") and (pAccountOpen = true) then
			return false
		end if
		
		return true
	end validateUserInterfaceItem_

Hope this helps.

All the best

Terry

Out of interest, where I was going wrong was not putting the pipes around action.

It’s disappointing that ASOC can’t recognise what you are doing and put the pipes around key words for you.

Regards

Terry

A little more info from the Apple docs with XCode:

All the best

Terry

It’s just using standard AS parsing, o there’s not a lot that can be done.