tellboy
December 6, 2010, 8:36pm
#1
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?
- (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...
tellboy
December 7, 2010, 11:56am
#3
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
tellboy
December 7, 2010, 12:09pm
#4
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
tellboy
December 7, 2010, 12:11pm
#5
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
tellboy
December 7, 2010, 12:16pm
#6
A little more info from the Apple docs with XCode:
Choosing validateMenuItem: or validateUserInterfaceItem:
In general, you should use validateUserInterfaceItem: instead of validateMenuItem: since the former will also work for toolbar items which have the same target and action. If, however, there is additional work that you want to do that is specific to a menu item, use validateMenuItem:”for example, validateMenuItem: is also a good place to toggle titles or set state on menu items to make sure they’re always correct.
All the best
Terry
It’s just using standard AS parsing, o there’s not a lot that can be done.