Context-sensitive menu in table view


I have been searching everywhere to figure out how to incorporate a context-sensitive menu within a table view in my current app. For those who don’t know … a context-sensitive menu is a menu that is not attached to the menu bar at the top of the screen but one that shows up by pressing control + left mouse button or by simply clicking the right mouse button.

I know that this is possible in Cocoa by subclassing NSTableView and then overwriting initWith??? and adding menu items like [menu additem: item] as can be seen in Aaron Hillegass’ book “Cocoa Programming for Mac OS X” on pages 335 and 336 for example.

I also know I can mix Cocoa Obj-C with Applescript and have tried it, but to no avail. This is why I code in AppleScript Studio as opposed to Obj-C … because it’s easier :smiley:

Any suggestions are highly appreciated.

  1. Create your table view.
  2. Drag a menu from the objects palatte into your project. Set it up and test that it works before doing any customization.
  3. Double-click on the table view so the table view, NOT the scroll view, is selected. Then, holding the control key, drag from the table view to the menu bar of the menu itself or to the menu’s icon in the instances pane of your nib window. Select the “menu” outlet in the info window, and then click the “connect” button at the bottom to connect the menu to the table view.
  4. Build your project and try control-clicking on the table view…your menu should pop up. Then you can customize the menu.

If you do not see the menu, you probably have the menu attached to the scroll view and not the table view. Try control-clicking on the empty bottom right corner of the scroll view and see if the menu pops up there. While trying to make it work, I also messed with other settings like the editability of the columns and other settings for the table view, which may or may not have ultimately played a role in making this work for me. I did get it to work though, so it is possible.

I have found that providing an applescript name to all of the menu items, and to all of the related objects in the window (like the scroll, table, and the window itself) before making any connections seems to make some of the errors go away. That is purely speculation and may be coincidental…but I continue to do it for good luck. :wink:

There have been other posts here regarding using contextual menus, particularly related to dock menus, but still applicable in some respects. You can sometimes get “…3(1)” errors which appear to be unavoidable and at the whims of the compiler. I have also seen the script fail to receive the choose menu item command from the menu items and throw errors. Oftentimes making a small change to the nib file, saving, and rebuilding can remedy this.

Good luck…

Wow !!! Very cool … thanks a lot jobu.

It now works like a charm :smiley:

Setting up the contextual menu in IB was a matter of minutes and worked right away. I did have the 3(1) error though which took a couple of hours to get rid of. I guess reading a post by jonn8 helped solving it … but I couldn’t REALLY say what the problem is with this error.

Anyway … thanks again. This board is the BEST for AS Studio

hmmm … back to 3(1) again :cry:

I have a follow up question to the one posted above.

While playing with contextual menu’s in the Finder I noticed that when I click the right mouse button in a table view, the row under the cursor gets highlighted automatically, right before the contextual menu opens.

I wonder how this behavior can be replicated in ASStudio.