Help/Hire iOS drafts5 script, single prompt w/ multiple button actions

Hello,

My drafts5 script prompt contains 1 datePicker with 2 buttons.

Button 1 appends the date from datePicker to the draft, THEN runs “zapier script”.
Button 2 immediately runs the “zapier script” (no date logging).

The “zapier script” is a hard coded Zapier Webhook

I currently cannot grasp the buttonPressed == functionality, and how it relates to p.show()

Here is my attempt https://actions.getdrafts.com/a/1Qf

Thank you,
Michael

1 Like

Have a look at the documentation for Prompt: Redirecting…

Specifically:

  • buttonPressed [string]
    • After the show() method is called, this property will contain the name of the button selected by the user.

The code you link to seems to be accessing the property before show and expecting it to contain a number instead of a name.

1 Like

Hi David,

Thanks for your help!

Here is my latest attempt
https://actions.getdrafts.com/a/1Qf

I added the button names inside ButtonPressed, and then moved it after p.show.

It loads the prompt and completes the action, but clicking the buttons just closes the prompt.

Any ideas? Happy to pay ~$25 to complete it with the commented actions.

Much appreciated,
Michael

One issue is that you are adding the optional value parameter to the addButton function, but are then testing for the name of the button. The Drafts script reference for the Prompt object says:

The value parameter is optional and only needed to associate a different value than will be displayed in the button. For example, if you call prompt.addButton("First Button", 1) , after calling prompt.show() if that button is pressed, the prompt.buttonPressed will contain the number value 1 .

Because p.buttonPressed contains the number value 1 ( if “Submit Date” is pressed) or the number value 2 (if “Submit Unscheduled” is pressed), your test expressions both evaluate to false, you therefore end up in the else branch of your if statement. Thus, the prompt cancels without running the code you intend for either case.

You could fix this by deleting the value parameter from the function calls so they read:

 p.addButton("Submit Date");
 p.addButton("Submit Unscheduled");

Alternatively, you could change the test expressions in the if statement to test for the number value 1 or 2, although I think deleting the value parameter is more straightforward in this case.

I didn’t dig further into the code within the if statement blocks, but I hope this helps at least get you to running that code.

1 Like

Wow. Thank you both. This has been a big learning leap for me.

Both buttons now work as intended.

The only challenge now is that the draft “notification” only appears if button 2 is pressed. I could add an alert for button one, but a notification is preferred.

Latest script:
https://actions.getdrafts.com/a/1Qn (sorry, it wont allow me to post as url)

I am not sure what you mean by the “notification”, but I do have a suggestion that might make this script easier to understand, and that’s to move the common code out of each button handler and up to just after the call to show returns. From what I can tell, nearly everything other than the date field is the same, so by sharing that code the script will get smaller and you won’t have to update two places when you make a change. Just a thought.

2 Likes

Although I only looked at it quickly, I think that the reason why you are getting no notification for button 1 is because you have an odd if block beginning at line 54. Basically, if Zapier doesn’t respond with a 200 code, then context.fail(), else context.cancel(). Thus, if Zapier sends back a 200, then it will cancel. Either way, execution of the action will not provide a success notification if button 1 has been pressed.

The documentation for context.cancel() indicates that:

If called, at the end of the script the action will be stopped. No subsequent action steps in the action will run, and the action still stop silently - no notification banners, sounds, etc.

So that’s likely why you don’t get a notification when button 1 is pressed. Zapier succeeds, sends back the 200 and therefore context.cancel() gets called, stopping the action silently.

I am not quite sure of the purpose you intend for that else or the editor.focus() function call following. (I’m not even sure of that function since I don’t see it in the documentation for the Editor object. Maybe you meant editor.activate()? Or maybe I’m not familiar with that function.)

A couple other notes:

I’d consider adding a semicolon to the end of line 22 (s = s.toString(“MM/dd/yyyy”) to insure against trouble down the road.

You might consider structuring the whole if statement like:

if (p.buttonPressed == 1) {
    [do stuff]
} 
else if (p.buttonPressed == 2) {
    [do different stuff]
}
else {
    [fail/cancel/etc.]
}

That way you avoid running the else for button 2 if you select button 1 and don’t otherwise stop execution within the code block for button 1.

(I also agree that refactoring the common code would make this easier to follow.)

Anyway, hope this helps!

1 Like