i have a menu in the main menu (menu bar) and it becomes populated in the on opened theObject handler na dit populates and everything fine, but i selected choose menu item for it, but when i click one of the menu items nothing happens and it doesnt do any of the code in the handlers. i did search. thanks.
Are you sure that you selected the correct .applescript file (in the bottom of Inspector window) when you checked the on choose menu box in Interface Builder?
Remember to also use the correct syntax within the handller:
on choose menu item theObject
set cho to name of theObject--Make sure you are looking for the name of the object that is used in the AppleScript portion of the IB Inspector
if cho = "addr_sel" then
GetAddresses()
else if cho = "backup" then
SWback()
else if cho = "mem_update" then
updateSW()
else if cho = "count_mems" then
CountMembers()
else if cho = "moved_mems" then
MovedWithData()
else if cho = "bsa" then
show window "ym"
show window "advance"
end if
end choose menu item
Hope this helps,
yeah, i’m positive i selected the right applescript file, and i put a display dialog in the handler and still nothing happens…
OK, now go back to Interface Builder, and be absolutely certain that the Name in the Name: box at the top of the Inspector window is exactly the same as those in your handler.
Please also double check that in your handler, you are testing for the name of theObject, and not just theObject.
yup its all perfect still nothing…wierd…
That is truly bizarre. I was hoping for something simple, since I did the same thing 3 days ago myself! You might try making a new project and just setting up a single menu bar item and see what happens.
hmm…same thing nothing happens. do you want me to upload the nib file so you can see if i did something wrong or is there something else we can try?
Make sure that you are not connecting the handler to the menu item containing the menu. You must connect the handler to each individual menu item you want to detect. In popup buttons, you can connect the handler to the menu of the button and detect the ‘name of current menu item’ that was selected. In a menu in the main menu bar, this connection is not valid and doesn’t fire. Instead, you must connect each of the menu items manually and use ‘name of the object’ as in your example above. If making this change does not solve the problem, then you’ve either made an error somewhere or you have a corrupted nib file.
j
ohhhh…one problem though, the menu is populated in the script so i cant select the handler for them.
script chooseMenuItem
(* This script object provides the 'choose menu item' handler for menu items that are created programmatically *)
on choose menu item theObject
handleChooseMenuItem(theObject)
end choose menu item
end script
on choose menu item theObject
(* This handler can be connected in IB to hardcoded menu items *)
handleChooseMenuItem(theObject)
end choose menu item
on awake from nib theObject
if name of theObject is "myMenu" then
set newItem to make new menu item at end of menu items of theObject with properties {name:"item2", title:"item2", enabled:true}
(* After you create a menu item programmatically, assign it a script object that handles it's connections *)
set script of newItem to chooseMenuItem
end if
end awake from nib
on handleChooseMenuItem(theObject)
(* This handler actually processes your chosen menu items... put all your code here *)
log ((name of theObject) as string)
end handleChooseMenuItem
in the awake from nib handler i have this
repeat with aMenuItem in theGames
make new menu item at end of menu items of menu "games" of main menu with properties {title:aMenuItem, enabled:true}
end repeat
so how would i so the set script part to set the choose menu item handler for each added item?
please, does anyone know how to do this??
Just like jubo did?
-- make new menu item.
set script of result to chooseMenuItem
but doing it in this way
repeat with aMenuItem in theGames
make new menu item at end of menu items of menu "games" of main menu with properties {title:aMenuItem, enabled:true}
end repeat
its not just one menu item i’m making its several when i put the line you just mentioned in the repeat part it gave me an error saying that variable choosemenuitem (or whatever) is undefinded
and also i’m getting weird errors when compiling this program–last post here: http://bbs.applescript.net/viewtopic.php?id=20712
Does anyone know how to do this?
No one’s responding to you because it seems you’ve managed to annoy us ALL. :mad:
Yes, I do know how to do it, and I posted the solution for you about half way up the page. I’d like to think I know everything, but I won’t make that claim. I do know enough to help you with pretty much all of the questions I’ve seen you ask up to this point, and I know that there are others who could easily furnish the answer. I also warned you that if you continue on the selfish, ignorant course you’re on, you will certainly lose the support you still do have.
The code I posted does work, and it does show how to assign a script object to a menu item. When you say “make new menu item…”, if it successfully creates a menu item it returns a reference to the menu item. If you choose to capture that reference, as I did when I wrote “set newItem to…”, then you can use that reference to assign properties of and manipulate that object. Thus, if you had used these past two weeks (during which all you seem to have done is bumping the post and whining) to do even some simple cut & paste scripting, you may have found this simple solution…
repeat with aMenuItem in theGames
set newItem to make new menu item at end of menu items of theObject with properties {name:aMenuItem, title:aMenuItem, enabled:true}
set script of newItem to chooseMenuItem
end repeat
In my opinion, you shouldn’t even bother asking questions if you’re not capable of listening to or understanding the answer. While I realize that the understanding comes with experience, I think ten days is certainly plenty of time to have spent figuring out this simple task. I handed you perfectly good working code, which you must assume will require at least a little bit of customization to suit your needs. Take some time to think about and try what we post before asking for even more of us.
Is chooseMenuItem
defined in your script?
well in the repeat statement it says this
set script of newItem to chooseMenuItem
then in that applescript file later on it has the sript chooseMenuItem handler. should that be in the handler that the repeat statement is in?
When I was working on a project some time back, I seem to recall finding that script objects should (must?) be defined in the top of your main script in ASStudio scripts. That’s why in my first post, it’s at the top. An it should be all on it’s own… not in any other handlers or subroutines. Try putting it at the top of your script and see if that makes a difference. While I certainly don’t expect you to know this, we should all (myself included) look at this as a lesson in listening to each other. When someone posts a solution, it may not just be the syntax or the commands that are important to the effectiveness of the solution, but it may be the WHOLE configuration of the posted solution that is important. Knowing how to look at someone else’s code and see that there may be more going on than a couple lines of code hacked together is an important concept. When I put together sample code I typically create a whole new project from scratch that does only what the question at hand asks of it. Not only did I provide some code that explained how to solve your problem, I also made sure that ALL of the handlers and supporting code were as they should be, too. When we ask you to put some time and some thought into the solutions we post, it’s not always just in terms of the specific two lines of code you were looking for, but is often on more of a conceptual and general level.
Hopefully we can get at a solution to this issue soon, so we can put this thread to bed.
j