Simple question about JavaScript Alert

I like working with alerts but have a simple question to help me go further with them. Here’s an example:

let myAlert = new Alert();
myAlert.title = "title of alert pop up";
myAlert.addTextField("this is field 0");
myAlert.addTextField("this is field 1");
myAlert.addCancelAction("Cancel");
myAlert.addAction("Submit");
await myAlert.present();
const entryOne = myAlert.textFieldValue(0);
const entryTwo = myAlert.textFieldValue(1);

If I run this, enter some text in the two text fields and hit ‘Submit’, entryOne & Two are filled.

If I do the same and hit ‘Cancel’, the same happens.

In effect, both of these buttons (as written) just dismiss the Alert and the script continues.

I’d like to stop the script when ‘Cancel’ is pressed, unsurprisingly!

addCancelAction will return the index -1. How do I access that index? Once I know that, a simple if statement should get me what I need.

1 Like

Try this:

let myAlert = new Alert();
myAlert.title = "title of alert pop up";
myAlert.addTextField("this is field 0");
myAlert.addTextField("this is field 1");
myAlert.addCancelAction("Cancel");
myAlert.addAction("Submit");
if (await myAlert.present() == 0)
{
	const entryOne = myAlert.textFieldValue(0);
	console.log(entryOne);
	const entryTwo = myAlert.textFieldValue(1);
	console.log(entryTwo);
}
else console.log("Cancelled");
3 Likes

That is so simple! Annoyed I couldn’t work it out myself!

Though I do think in this respect the Scriptable docs are weaker than those for Drafts.

I wonder if Simon has considered open-sourcing them? I’d love to contribute from the novice’s POV.

1 Like

Drafts generally has some really good documentation.

Open sourcing the docs have come up in the forum before. It’s something I’d love to do but my current workflow for creating the docs aren’t really geared towards it. That’s not necessarily a problem though.

3 Likes