Javascript function to show a palette / pop up menu of actions


#1

After a discussion in the Slack channel I created a general purpose function to pop up a palette (or menu, if you prefer) of actions. Tap on one and the action runs. Press Cancel and the palette goes away.

The code sample below includes a simple invocation. You can see an array of action names is passed to the showPalette function. (It would be feasible to pass an array that has pairs of items. These would include a label and an actual action name.)

Of course, an action could itself pop up another palette.

And the array of action names could be generated rather than be fixed strings. So it could be dynamic.

  // Create palette
  var p = Prompt.create()

  // Add one button for each of the actions array members
  for(i=0;i<actions.length;i++){
    p.addButton(actions[i])
  }

  // Put up palette
  if(p.show()){
    // Didn't cancel
    var action = Action.find(p.buttonPressed)
    app.queueAction(action, draft);
  }
}

showPalette(["CSV To Markdown Table","Indent","Outdent"])

How To Share A Javascript Function?