Enclosing a word or words with AHK text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
downstairs
Posts: 41
Joined: 17 Aug 2017, 11:42

Enclosing a word or words with AHK text

20 Aug 2017, 05:16

I am curious if this is possible. This will be for coding, say HTML... but could be used elsewhere. Any way to enclose a string to text with text in my AHK macro. I'll give a simple example.

I have this text:
Hello my name is Chris

I want an AHK command of some sort associated with, say "Alt-Shift-B" to do this to the text (Add <b> at the beginning, and add </b> at the end):
<b>Hello my name is Chris</b>

I have no problem selecting the text, and I assume selecting it will be key here. So does AHK have some way of knowing the selected text?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Enclosing a word or words with AHK text

20 Aug 2017, 09:26

Try this:

Code: Select all

!+B:: SendInput, % WrapText(get_Highlight(), "b")       ; bold
!+C:: SendInput, % WrapText(get_Highlight(), "c")       ; inline code
!+D:: SendInput, % WrapText(get_Highlight(), "docs")    ; docs
!+I:: SendInput, % WrapText(get_Highlight(), "i")       ; italics
!+Q:: SendInput, % WrapText(get_Highlight(), "quote")   ; quote
!+U:: SendInput, % WrapText(get_Highlight(), "u")       ; underline



;-------------------------------------------------------------------------------
WrapText(Text, Code) { ; wrap text in BBCode tags
;-------------------------------------------------------------------------------
    Return, "<" Code ">" Text "</" Code ">"
}



;-------------------------------------------------------------------------------
get_Highlight() { ; get highlighted text
;-------------------------------------------------------------------------------
    $_AutoTrim := A_AutoTrim            ; remember current setting
    AutoTrim, Off                       ; keep leading/trailing whitespaces
    ClipStore := ClipboardAll           ; remember current content

    Clipboard =                         ; empty clipboard
    SendInput, ^c                       ; copy highlighted text
    ClipWait, 1                         ; wait for change
    Result := Clipboard                 ; store highlighted text

    Clipboard := ClipStore              ; restore previous content
    AutoTrim, % $_AutoTrim              ; restore previous setting

    Return, Result
}
I hope that helps.
downstairs
Posts: 41
Joined: 17 Aug 2017, 11:42

Re: Enclosing a word or words with AHK text

20 Aug 2017, 09:51

Thanks wolf_II I will try that!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, mcd, NullRefEx, supplementfacts and 128 guests