1
votes

Is there a content awareness feature of AutoHotKey? I need to replace some selected text in my application. Imagine the following CSHTML:

@
{
    ViewBag.Title = "Welcome";
}

<p>Welcome to this page</p>

I have created a very simple script, that (once text has been selected):

  1. Clears clipboard
  2. Send CTRL+C
  3. Send @normalize("
  4. Send CTRL+V
  5. Send ")

That means, that if I select Welcome to this page, it will be replaced with @normalize("Welcome to this page"). That part works perfectly fine.

However, if I want to replace "Welcome" with the same, I would have to select the quotation marks, which speeds down this automation script quite a lot, because I can't simply double click on the word and select it. Now I would have to press and hold, then drag the mouse across the screen until it reaches the last ending quotation mark.

Instead, I need my AutoHotKey script to do something like:

Send CTRL + C;
var caret = currentCaretTextPosition; //made up
IF caret+1 IS "
    Send Delete
IF caret-1 IS "
    Send Backspace
Send @normalize("
Send CTRL+V
Send ")

If that makes sense. Basically transforms "Welcome" into:

  1. ""
  2. "
  3.  
  4. @normalize("
  5. @normalize("Welcome
  6. @normalize("Welcome")

This is very basic content awareness stuff, because it checks the surrounding characters of the caret, then acts upon that.

AutoHotKey script (triggered by CTRL+ALT+SHIFT+X):

^!+x::
clipboard:=""
While clipboard
Sleep 10
While !clipboard
{
    Send ^c
    Sleep 100
}
Sleep 20
Send @normalize("
Sleep 20
Send ^v
Sleep 20
Send ")
return
3

3 Answers

1
votes

This solution will check if there are surrounding quotes and proceed accordingly. It may fail if there is nothing to the right (such as end of file). It does stop if nothing is selected initially. It does not check if what is selected already contains quotes.

^!+x::
clipboard := ""
Send , ^x
ClipWait , 1
If ErrorLevel
    Return
sNormalize := clipboard
clipboard := ""
Send , {right}+{left 2}^c
ClipWait , 1
Send , % ( clipboard = """""" ? "{del}" : "{left}{right}" )
clipboard := "@normalize""" . sNormalize . """)"
Send , ^v
Return
0
votes

I've made a not-so-bulletproof fix, but it unfortunately requires me to use another keybinding, which I'm not a fan of:

^!+z::
clipboard:=""
While clipboard
Sleep 10
While !clipboard
{
    Send ^x
    Sleep 100
}
Sleep 20
Send {Right}
Sleep 5
Send {BackSpace}
Sleep 5
Send {BackSpace}
Send @normalize("
Sleep 20
Send ^v
Sleep 20
Send ")
return

It does what I wanted above, but it's not very bulletproof.

0
votes

Off topic but thought I'd mention it

If you have many of such snippets an alternative could be Lintalist, it is developed in AutoHotkey so you can extend it with your own scripts and plugins. The above script could be replaced with the following snippet - here it uses the selected text in your editor but you can use the clipboard as well:

@
{
    ViewBag.Title = "[[selected]]";
}

<p>[[selected]]</p>

You can group snippets in Bundles per application and/or language, assign hotkeys and search for them.

You can learn more about Lintalist here https://lintalist.github.io/ - available plugins here https://lintalist.github.io/#InteractiveBundleText