Can I Use the Selection in a Text Area in a Custom HTML Prompt Action?

Before I start to make my first macro with a Custom HTML Prompt action, I'd like to ask whether this action will allow me to evaluate (use) the selection in a text area.

Background: For a given source term in the source segment, I'd like to display the whole, corresponding target segment and ask the user to identify the corresponding target term by selecting it in the text area.

Is this possible?

Yes, but requires a little JavaScript. Getting Selected Text of TextArea Using JavaScript shows what you need but the alert() isn't supported in Keyboard Maestro's implementation of Webkit, which is what Custom HTML Prompt uses.

So instead of alert(), I've added a line to store the selection in a Keyboard Maestro local variable and display that in a notification when the OK button is clicked. Just type some text, make a selection and press OK.

Custom HTML Prompt.kmactions (1.6 KB)

EDIT: I think that I've completely misunderstand what you wrote.

When I import your action and add it to a new macro (as only action), I get this when I run the macro:

When I click the OK button, no window with the selected text is displayed.

My first understanding was that I had to add your action to a form as in the wiki. Hence the misinterpretation below:

Thank you very much!

Could you (or another kind person) please help me with:

  • How to get a variable TargetTerm (e.g. 'Spaß') from the text area to use in a following action?


Test - selection in text area.kmmacros (5.7 KB)

Let's try that again. You should see the selection in a Notification (see the second action, which may have been omitted above:

Get Selection from Textarea.kmmacros (2.9 KB)

So to elaborate a bit, you put whatever you want to display in the section of the HTML code, of course. In my example, the textarea selection variable is called localSelection. Anything selected in the textarea of my example macro will be displayed in the Notification action when you click the OK button.

In your case, you would change the HTML display from my simple textarea to whatever you want (with a textarea somewhere) and rename the variable from localSelection to TargetTerm (which is, however, a persistent global variable) and name the textarea.

Update. I took a look at your macro and folded my example into it for you:

Test - selection in text area.kmmacros (6.0 KB)

That shows what you selected in a Notification.

1 Like

Thank you very much! Perfect.