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

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"])
1 Like

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.

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"])
2 Likes

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

This is great, but how do I pass the result on to the next action?

Iā€™m using setTemplateTag but I get an error if I write:

draft.setTemplateTag(ā€˜journaltitleā€™, p()) or draft.setTemplateTag(ā€˜journaltitleā€™, p) and draft.setTemplateTag(ā€˜journaltitleā€™, p.buttonPressed) What is the correct syntax to set my templatetag to the selection picked during running the action?

I think that perhaps I have to replace the ā€œapp.queactionā€ command with something else to make this work?

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(["journal 1","journal 2","journal 3"])

draft.setTemplateTag("journaltitle", action);

Can you describe in a bit more detail what you want to do. Iā€™m a bit confused over your actions references and the types of changes you look to be trying.

Are you really trying to set data up for a queued action, or are you trying to set data up for a subsequent action step, or within the same action step?

I am writing a script to send drafts to DayOne. I have an existing script which does this using a URL action (DayOne has a URL scheme, but not an x-callback-URL) but it goes to the default journal in DayOne. Instead, I want to pick the appropriate journal from a list. After picking the Journal I want to send that to the URL action in the next step.

My problem is that I donā€™t know how to use the set TemplateTag command (which sends data to another step) with this menu script. How do I send the results of the menu selection action to the templatetag?

From that it just sounds like you want to send to the next action step rather than queue a separate action.

Try this example based on your script - https://actions.getdrafts.com/a/1Xp

The subsequent action step is an insert text as I donā€™t have those journals in Day One. But it inserts the URL you might trigger, so you can adopt a similar approach for your own URL step.

Hope that helps.

Thanks, that works. Only error was that the URL action needed to be:

dayone://post?entry=[[drafts]]&journal=[[journaltitle]]
not
dayone://post?entry={{[[drafts]]}}&journal={{[[journaltitle]]}}

(It was double encoding the spaces.)

Yes, I very much did that on purpose with the example to show you the encoded URL which is as it should be. If you enable the encode option in the URL action you drop the braces. If you donā€™t enable that option, the braces for the encoding you would keep in.

That was also the reason I said you would need to ā€œadopt a similar approachā€ when you used the URL step rather than simply copying and pasting it :wink: