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?
#2

How would you integrate this into an action? I made an action using just the script and using three of my actions for the palette. Nothing happens (action fails). I’m not proficient with JS, but do tinker.

Thanks.


#3

I think the function definition is missing above. Try this.

  function showPalette(actions)
  {
  	// 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"])

#4

Aargh! Thanks @sylumer. Goodness knows what went wrong in publishing. :frowning: