Please Help! with Dock Menu

Hello,

Im working in xcode 2.1 making an applescript application and am trying to implement a dock icon menu. I’m able to create the nsmenu and attach it to the file’s owner and add menu items. Problem is when i attach a script to the menu item and build it, on selecting a menu item i get NSReciverEvaluationScriptError: 3 (1).

example= I’m trying to open the users document folder

on choose menu item theObject
tell application “Finder”
open folder “Documents” of home
activate
end tell
end choose menu item

on update menu item theObject
return true
end update menu item

I’m not having much luck on finding info for (on update menu item theObject) all i have found says return a true or false value and add a script. I dont need to update the menu item so im not sure what to put there. When you create a new script from within interface builder it creates the (on update menu item theObject) automatically. No matter how i configure options in interfacebuilder say in attributes for nsmenu selecting or not (auto enable menu items) and for the nsmenu item applescript event handler menu (choose menu item) and (update menu item) still get the same NSReciverEvaluationScriptError: 3 (1).

Greetings, wiredless… and welcome to macscripter.

I am assuming that you’ve dragged an nsmenu object from the palette in IB, and then connected it to your application/File’s Owner object as it’s dockMenu? Dock menu’s have been a source of frustration for scripters for some time. You’re not necessarily doing anything wrong, the connection to stand-alone menus is just buggy and error-prone. Even if you seem to have everything set up correctly, that connection is not honored and you will likely ALWAYS get that error.

You have a couple options to get around this. If you’re obj-c savvy, then you can program your whole menu in obj-c and it will work that way.

If you’re looking for a simpler, applescript method, try the following…
I’d recommend creating a new window to serve as a home to only your dock menu. Create a new window, and make sure it’s “visible at launch time” flag is NOT checked. This will ensure that the window is loaded at launch time, but does not ever show up on screen. Next, drag a popup button into the window. This will serve as your dock menu. Set up the popup button’s menu however you want it, and make all of the connections you normally would to your AS code. The last step is setting the popup button as the dock menu. In IB, in your MainMenu.nib window under instances, switch to outline view mode by clicking on the outline button (the one with the lines, just above the right scroll bar). Navigate through the object hierarchy and find your popup button, and then click on it’s disclosure triangle to expose the NSMenu object hidden inside of it. Then, drag from your NSApplication object down to the NSMenu in the popup button, and “dockMenu” should become a valid option for you in the connections pane of the info window. Select ‘dockMenu’ if it’s not the selected connection already, and then click ‘Connect’. Now save and rebuild the project and it should have your popup button’s menu in the dock menu, with no errors or problems. :cool:

It is very important that although the window the popup button is in need never be shown, it can never be released (closed). ‘Closing’ a window is different than ‘hiding’ it, where a close command will remove it from the loaded objects memory and hiding it pretty much just moves it off screen. If the window is closed, either by it’s close button or programmatically, it’s connections will no longer be honored and the menu items in the dock will no longer respond. By using a dedicated window for this purpose, you can be relatively sure that the window will always be in memory, and unless you somehow leave a method for it to be closed it’s connections should be reliable. If you decide to use a window that the user has access to closing, make sure to issue a “hide” command to it rather than a close command whenever you want it to “go away”, otherwise it will be released.

Depending on the type of menu functions you want to provide, you may find that having the popup button set as a pulldown rather than a popup type is more appropriate. If set as a pulldown type, the items will not have a checkmark next to them in the dock menu showing which is currently selected. Remember also that a pulldown menu has a “hidden” menu item 1 for the title item, too, which WILL show up in the dock menu.

Good luck, and let me know if you need any further help or clarification…
j

Thanks Jobu,

I implemented the menu as you described but still getting the same error. It seems I’m not alone and many many others are having the same problem with a dock menu trying to trigger apple scripts. The menu its self done in a window as a popup or pull down works great. I’m not much of a coder just someone with an idea thats been driving me nuts for way to long. I started this project a few months before tiger came out and shelved it because the app would be best done in a dock menu as opposed to a window. The window part of my app works like a charm. With Xcode 2.1 i figured maybe something had changed and it would work now. when i first started the project i was able to get the menu to work on one build then the next it would die. Then the next half the menu would work and the other would not. At any rate i feel better now knowing I’m not alone with the problem.

If you’re still getting errors, you may have corrupted the nib file somewhere along the way. We have seen a high occurence of corrupted nibs when trying to add dock menus to projects, so you may well have an interface that will continue to throw errors regardless of what you do to try to fix it.

I didn’t see any problems when using the method I described in my earlier post. The problems most people have stem from a problem in every version of xcode where users try to get applescript code to execute when connected to the menu items in “orphan” menu’s (my term :P), i.e. menu’s not inside any windows but rather dragged into the nib by themselves. Apple’s own documentation describes this as a method of providing a dock menu, but execution of the applescript code is not supported using this particular method. If you were to program your actions in cocoa methods using obj-c, you’d have no problems. Menu’s in another view, such as a content view for a window or drawer, should work fine, as long as that view is within the window server memory and has not been released.

Are you sure you’re still getting the #3 error, or has it changed to a #4 error? This would indicate your references are incorrect, rather than your connection invalid. If you’d like to email me a copy of your project, I’d be happy to take a look, because I don’t think you should still be seeing this behavior… jobu10000(at)yahoo(dot)com.

j

Hi everybody,
I’m a newbie, so forgive me if I’m not very terminology-safe :wink:
I think, I can follow what wiredless has done, I had the same experience. But , then I found what was wrong and got it working! Wiredless, am I correct to believe, that you connected each menuitem to applescript handlers? I did so, and had the same error than you! Until, I connected the MENU itself to the applescript handler, and disconnected the single menuitems.

So, as far as I understand, the dockmenu in applescript studio applications can be handled only through the menu-events and not the menuitem-events. Having said this, I can also understand what jobu is telling us. In the interface builder, a STANDALONE MENU is able only to send “awake from nib”-events to Applescript. So, it is not possible to connect a standalone menu to an applescript studio application’s dockmenu. At least, as far as I understand.

I can get jobu’s workaround working, but it is clear, that this workaround limits the capabilities of the dockmenu (e.g. no submenu possible!). So, my question at this point is, what would be the path through a solution for the dockmenu in plain Cocoa? I’m not Cocoa-safe, but I can’t either see how I would have to connect this to the applescript studio application?
Do I have to write a controller for the dockmenu using cocoa which then sends signals to my applescript? Where should that controller be? Has anyone a rough idea through this? Just curiosity …

Thomas