Create action with prompt text input

I’d like to convert some regular expressions into actions.

I’ve been trying to figure it out using the Draft Script Reference.

Could someone help me get started by helping me identify the different parts I need to include to create the following?

I think then I can get my head around the structure I need to follow to do the others…

The following regex searches for a word at the beginning of a line and deletes that line and proceeding empty lines until the next instance of text.

With this example, I’d like a prompt to come up where I can enter the text which should replace WORD in the ‘do something’

^(WORD$)\s*\n

Using the prompt example on the reference page, I’m guessing I need:

  • to create a text prompt box
  • have a button to push after the text is entered
  • replace the text in the ‘do something’ (I’m not sure what that looks like with the do something line, is it [[WORD]] ?)
  • then it should look for whatever was entered in the text field, and combine it with the ‘do something’

Below is not expected to work, but if someone could teach me how, I’d be very grateful to learn.

let p = Prompt.create();

p.addTextField("textFieldName", "Label", "");

p.addButton("First");

let didSelect = p.show();

let textFieldContents = p.fieldValues["textFieldName"];

if (p.buttonPressed == "First") {

draft.content = draft.content.replace(/^(WORD$)\s*\n /gm, "")

}

You are on the right track. At the end of that script, the value you input in the prompt is stored in the textFieldContents variable, you just need to construct a new regular expression with that value concatenated into the pattern. To this this, built a string to represent the expression, and create the regex object with the constructor, instead of the // literal expression. Like:

// create regex object
const re = new RegExp("/^(" + textFieldContents + " $)\s*\n", "gm")
// use it
if (p.buttonPressed == "First") {
    draft.content = draft.content.replace(re, "")
}

More on regular expression object in Javascript.

Thanks, that was the advice I needed, got it up and running now.