Wrap Text In Quotes in-line using AHK v2 Menu() & keys

Post your working scripts, libraries and tools.
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Wrap Text In Quotes in-line using AHK v2 Menu() & keys

18 Dec 2023, 13:02

I find myself wanting to enclose words and numbers in different types of quotation marks, especially when working on a AutoHotkey script :)
Taking inspiration from two old AHK v1 scripts from here and here, I rewrote my chimeric mess of a AHK v1 script into AHK v2.

Code: Select all

#Requires AutoHotkey v2.0

; Wrap Text In Quotes keys
#HotIf not WinActive("ahk_exe mpc-hc.exe") ; disable below in apps that don't use it or have conflicts

!1::EncQuote("`'","`'")         ; enclose in single quotes '' - ' U+0027 : APOSTROPHE
!2::EncQuote(Chr(34),Chr(34))   ; enclose in double quotes "" - " U+0022 : QUOTATION MARK
!3::EncQuote("(",")")           ; enclose in round breackets ()
!4::EncQuote("[","]")           ; enclose in square brackets []
!5::EncQuote("{{}","{}}")       ; enclose in flower brackets {}
!6::EncQuote(Chr(96),Chr(96))   ; enclose in accent/backtick ``
!7::EncQuote("%","%")           ; enclose in percentage sign %%
!8::EncQuote("‘","’")           ; enclose in single quotes ‘’ - ‘ U+2018 LEFT & ’ U+2019 RIGHT SINGLE QUOTATION MARK {single turned comma & comma quotation mark}
!9::EncQuote("“","”")           ; enclose in double quotes “” - “ U+201C LEFT & ” U+201D RIGHT DOUBLE QUOTATION MARK {double turned comma & comma quotation mark}
!0::EncQuote("","")             ; remove above quotes

!q:: {
WrapQuotesMenu := Menu()
WrapQuotesMenu.Delete
WrapQuotesMenu.Add("&1  `'  Single Quotes"    ,WrapQuotesFunc)
WrapQuotesMenu.Add("&2  `"  Double Quotes"    ,WrapQuotesFunc) ;"
WrapQuotesMenu.Add("&3  (  Round Breackets"   ,WrapQuotesFunc)
WrapQuotesMenu.Add("&4  [  Square Brackets"   ,WrapQuotesFunc)
WrapQuotesMenu.Add("&5  *  Asterix"           ,WrapQuotesFunc)
WrapQuotesMenu.Add("&6  ``  Accent/Backtick"  ,WrapQuotesFunc)
WrapQuotesMenu.Add("&7  `%  Percentage Sign"  ,WrapQuotesFunc)
WrapQuotesMenu.Add("&8  ‘’  Single Quotation" ,WrapQuotesFunc)
WrapQuotesMenu.Add("&9  “”  Double Quotation" ,WrapQuotesFunc)
WrapQuotesMenu.Add("&0  Remove All Quotes"    ,WrapQuotesFunc)
WrapQuotesMenu.Show
}

#HotIf

; Wrap Text In Quotes Functions

WrapQuotesFunc(item, position, WrapQuotesMenu) {
If position = 1
    EncQuote("'","'")           ; enclose in single quotes ''
Else if position = 2
    EncQuote(Chr(34),Chr(34))   ; enclose in double quotes ""
Else if position = 3
    EncQuote("(",")")           ; enclose in round breackets () 
Else if position = 4
    EncQuote("[","]")           ; enclose in square brackets [] 
Else if position = 5
    EncQuote("{{}","{}}")       ; enclose in flower brackets {}
Else if position = 6
    EncQuote(Chr(96),Chr(96))   ; enclose in accent/backtick ``
Else if position = 7
    EncQuote("%","%")           ; enclose in percentage sign %%
Else if position = 8
    EncQuote("‘","’")           ; enclose in single quotes ‘’
Else if position = 9
    EncQuote("“","”")           ; enclose in double quotes “”
Else if position = 10
    EncQuote("","")             ; remove quotes
}

EncQuote(q,p) {
CallClipboard(2) ; 2s
QuoteStringInitial := A_Clipboard
QuoteString := A_Clipboard
QuoteString := StrReplace(QuoteString, "`r`n", "`n")
QuoteString := RegExReplace(QuoteString,'[\[\]\*`'\(\)\{\}%`"“”‘’]+|``')     ;"; remove "*`'(){}%“”‘’[]
QuoteString := RegExReplace(QuoteString,'^\s+|\s+$')     ; RegEx remove leading/trailing space
QuoteString := q QuoteString p
QuoteString := StrReplace(QuoteString, "`r`n" p, p)
Len1 := "0"
Len2 := "0"
If q ~= "{" ; RegEx match
    Len1 := Strlen(QuoteString) - 4 ; to account for extra {} in q and p
Else
    Len1 := Strlen(QuoteString)
; if you regularly include leading/trailing spaces within quotes, comment out above RegEx and below if statements
If (RegExMatch(QuoteStringInitial, "^\s+")) {   ; if initial string has Leading space
    QuoteString := " " QuoteString              ; add Leading space to string
    Len1++                                      ; add 1 to len
    }
If (RegExMatch(QuoteStringInitial, "\s+$")) {   ; if initial string has Trailing space
    QuoteString .= " "                          ; append trailing space to string
    Len1++                                      ; add 1 to len
    }
Len2 := "+{left " Len1 "}"
Send QuoteString    ; send string with quotes
Send Len2           ; and select it
clipSave := ""
}

;  = Call Clipboard and ClipWait
; Check what happens to clipSave later in script

CallClipboard(secs) {
clipSave := ClipboardAll()
A_Clipboard := ""
Send "^c"
If !ClipWait(secs) {   
    MyNotification(A_ThisHotkey ":: Clip Failed", "2000", "1650", "985") ; personal preferrence coz tooltip conflict
    ; ToolTip "Clip Failed"           ; Alternatively to MyNotification
    ; sleep 500
    ; ToolTip
    A_Clipboard := clipSave
    clipSave := ""
    Exit
    }
}

;  = Notification GUI
; replace Progress command from AHK v1 with GUI in AHK v2

MyNotification(mytext, myduration, xAxis, yAxis) {
MyNotification := Gui()
MyNotification.Opt("+AlwaysOnTop -Caption +ToolWindow")  ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
MyNotification.BackColor := "EEEEEE"  ; White background
MyNotification.SetFont("s9 w1000", "Arial")  ; font size 9, bold
MyNotification.Add("Text", "cBlack Center", mytext)  ; black text
MyNotification.Show("x1650 y985 NoActivate")  ; NoActivate avoids deactivating the currently active window
WinMove xAxis, yAxis,,, MyNotification
Sleep myduration
MyNotification.Destroy
}
This code is old.
Updates will be posted here On GitHub.
Last edited by xypha on 26 Dec 2023, 13:06, edited 1 time in total.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 69 guests