How to add a command to the right-click menu

Kosso keeps asking how to add a right-click menu item that sets an attribute of the headline the cursor is pointing to. Every time he asks, I ask for an example of an attribute, but he never gives me one. So I had to wait until I had an application that required a new attribute before I could come up with a good example.


Suppose you had a post that you wanted to put a title on, but the post isn't long enough to indent a few paragraphs under a main heading (which would give the post a title in the RSS feed). I actually have an application for this, more on that later, perhaps later today. First, you'd have to know how to add a command to the right-click menu; then how to set an attribute from a script.

How to add a command to the right-click menu

1. Choose "Edit right-click menu" in the Tools menu. A window opens, containing an empty outline. In the first line type Say Hey and then click on the wedge to the left to the headline to switch to a bar cursor. Screen shot.

2. Double-click on the wedge to open the script that's linked to the new command. Enter a simple script: dialog.alert ("Hi Mom!"). Screen shot.

A picture named screen3.gif3. Test the script by clicking on the Run button in the script window. You should see a dialog that says hello to your mother. If you don't, stare at the screen shot above and then stare at the script window on your computer until you see how yours is different, and then fix it. This is the trick to learning how to program. The staring part is essential. No matter how skilled you get, you will make dumb mistakes that you won't see until you stare at them some more. No one can help you do this, or even teach you how to do this. For more on the philosophy, see Programmers, 5/7/97.

4. Test the script from the right-click menu by right-clicking on any headline in any outline. The first menu item should be Say Hey. Choose it. Again, a greeting for your mother should appear. (I believe on the Mac, you option-click instead of right-clicking.)

5. Extra credit. Edit the script to read as follows: dialog.alert (op.getlinetext ()). Now try choosing the command, by right-clicking on any headline in any outline. It should echo the text of the headline in a dialog. If it did, good work! If not, stare some more. ;->

How to set an attribute from a script

Okay, now you have successfully added a trivial command to the right-click menu. Now let's add a command that actually does something.

Here's the script.

  local (mytitle)
  op.attributes.getOne ("title", @mytitle)
  if dialog.ask ("Enter a title for this headline:", @mytitle)
    op.attributes.setOne ("title", mytitle)

Try it out, again, by right-clicking on any headline in any outline. Set the title to anything you like. Try it again. Note that it offers, as a default, the original title.

That's it!

# Posted by Dave Winer on 8/2/05; 11:03:21 AM - --