Implementing a contextual menu

I’ve searched the forum and found this

but the discussion there seems to be different from what I want to do. I want to implement a contextual menu that’ll be invoked by right clicks (or option+left clicks) inside my AppleScript Studio app.

Does anyone have a pointer? Thank you.

It’s easy. In IB, create your new menu. In a window of your NIB, drag an object to which you would like to attach the contextual menu (a text box, table, etc.) Now control-click and drag from the object to the NSMenu you created for the contextual menu. This will create a link between the object and the menu and the menu outlet of the object will be highlighted in the info palette. Finally, double-click the word “menu” in the info palette to formalize the link. That’s it. You can now assign whatever menu items you’d like to the contextual menu and use AppleScript to name and control the menu items and their actions. For most textual elements that you can enter text into in IB, there already are contextual menus for spelling & editing. If you attach your own contextual menu to one of these elements, you menu will be joined with the other menus.

I may not have made this very clear but it really is easy.


This is as clear as it can get, thanks Jon!

Just one note for reference - Scroll View doesn’t like to have menu, it seems.

any way to actually find the context?

for example, if i set a CM up for a table view, is there any way to determine whether the menu has been triggered from a selected data cell in the table view, as opposed to empty space in the table view?

(i can imagine a way to do this using a ‘selection changed’ handler, but i’m wondering if there is a simpler way.)

I guess you can use “right mouse up” event? But I haven’t tried it… (let me know if it works 8) )

Ok this is weird - I thought I solved all the problems but I didn’t.

So adding a contextual menu works fine - until I try to add a “choose menu item” handler to individual menu items. For some reason the handler is not even called (I put a single "display dialog “hi” in the handler but the dialog never showed up).

Here is the code:

property theTable : null

on clicked theObject
	if name of theObject is "cb" then
		set theRow to the selected data row of theTable
		display dialog (contents of data cell "cb" of theRow) as string
	end if
end clicked

on awake from nib theObject
	set theTable to table view "table" of scroll view "table" of window "main"
	set theDS to data source of theTable
	tell theDS
		make new data column at end of data columns of theDS with properties {name:"cb"}
		make new data column at end of data columns of theDS with properties {name:"name"}
		set theRow to make new data row at the end of the data rows
		set contents of data cell "name" of theRow to "Apple"
		set theRow to make new data row at the end of the data rows
		set contents of data cell "name" of theRow to "Banana"
	end tell
end awake from nib

on choose menu item theObject
	display dialog "hi"
end choose menu item

And for the contexual menu there are two items and each of them has linked to choose menu item of the script shown above. What could I do wrong?

The error I got is “NSReceiverEvaluationScriptError: 3 (1)”. The context menu comes up fine when I right click on the table. (if anyone is willing I can put this very simple Xcode project somewhere)


Hmm, I’m getting the same results in Xcode 1.1. I seem to recall this working in earlier versions of Project Builder/IB but I don’t have any examples.


The strange thing is, I remember last nite my code worked. Today I loaded my project and suddenly it stopped working? Or could it be I was dreaming… :frowning:

I found the problem - can you believe this: it’s Little Snitch! I don’t know why (yet), but last nite I installed LS 1.1 and that’s the only thing I did to change my system. I just uninstalled it and now my code is back to normal…

Jon if you have LS running try to uninstall it and see if that helps… strange!

I do have Little Snitch 1.1 installed. Running the uninstaller and restarting didn’t fix my problem, I’m still getting the “NSReceiverEvaluationScriptError: 3 (1)” when the contextual menu item is selected.


I don’t know how Little Snitch might be involved but since it requires admin access to install, maybe some permissions were hosed during the install process. I would repair permissions to see if it makes a difference.

– Rob