Search code examples
textcaseautohotkey

Is there a better approach using AutoHotkey v1 to this inverted text case script?


I have a script I came across quite awhile back to simply change text case to inverted. It works well, but has a couple second delay before converting the text. I can't help but wonder if there is a better approach as the script is fairly lengthy for what it does and I also am not sure it needs a handful of functions to work correctly? I have googled till my eyes started blurring trying to find another version of an inverted text case script that actually works correctly but have turned up very little on this case change. Other case changes I have come across have been fairly easy to work out and are able to convert almost instantly on the fly, but I am stumped on this one for some reason. Can anyone assist or point me to a functional inverted case script that is different than this one for me to try working with?

^Numpad0::

  {
        CopyClipboardCLM()
        ClipWait
         Inv_Char_Out := ""
         Loop % StrLen(Clipboard)
         {
             Inv_Char := SubStr(Clipboard, A_Index, 1)
             if Inv_Char is Upper
                 Inv_Char_Out := Inv_Char_Out Chr(Asc(Inv_Char) + 32)
             else if Inv_Char is Lower
                 Inv_Char_Out := Inv_Char_Out Chr(Asc(Inv_Char) - 32)
             else
                 Inv_Char_Out := Inv_Char_Out Inv_Char
         }
         Clipboard := Inv_Char_Out
         PasteClipboardCLM()
      }

; * * * * * * * * * * * * * * 

GetText(ByRef MyText = "")
{
   SavedClip := ClipboardAll
   Clipboard =
   Send ^c
   ClipWait 0.5
   If ERRORLEVEL
   {
      Clipboard := SavedClip
      MyText =
      Return
   }
   MyText := Clipboard
   Clipboard := SavedClip
   Return MyText
SetCapsLockState, Off
}


PutText(MyText)
{
   SavedClip := ClipboardAll 
   Clipboard = 
   Sleep 50 
   Clipboard := MyText
   Send ^v
   Sleep 100
   Clipboard := SavedClip
SetCapsLockState, Off
   Return
}
SetCapsLockState, Off
Send, {capslock up}


CopyClipboard()
{
    global ClipSaved := ""
    ClipSaved := ClipboardAll 
    Clipboard := "" 
    Send {Ctrl down}c{Ctrl up}
    Sleep 150
    ClipWait, 1.5, 1
    if ErrorLevel
    {
        MsgBox, 262208, AutoHotkey, Copy to clipboard failed.
        Clipboard := ClipSaved 
        ClipSaved := "" 
        return
    }
}


CopyClipboardCLM()
{
    global ClipSaved
    WinGet, id, ID, A
    WinGetClass, class, ahk_id %id%
    if (class ~= "(Cabinet|Explore)WClass|Progman")
        Send {F2}
    Sleep 100
    CopyClipboard()
    if (ClipSaved != "")
        Clipboard := Clipboard
    else
        Exit
}


PasteClipboardCLM()
{
    global ClipSaved
    WinGet, id, ID, A
    WinGetClass, class, ahk_id %id%
    if (class ~= "(Cabinet|Explore)WClass|Progman")
        Send {F2}
    Send ^v
    Sleep 100
    Clipboard := ClipSaved
    ClipSaved := ""
    Exit
}

Solution

  • StringLower / StringUpper says

    Format() can also be used for case conversions, as shown below:

    MsgBox % Format("{:U}, {:L} and {:T}", "upper", "LOWER", "title")
    

    To invert text you need a parsing loop to convert each letter to its opposite:

    my_text := "Hello Word!"
    
    MsgBox % Invert(my_text)
    
    
    Invert(text) {
        Loop, Parse, text
            If A_LoopField is lower
                inverted .= Format("{:U}", A_LoopField)
            else
                inverted .= Format("{:L}", A_LoopField)
        Return inverted
    }
    

    V0RT3X said in a comment below:

    Thank you @user3419297. Wish I had seen this sooner. Looks a little cleaner than what I ended up going with.
    So for any future readers, yet another option:

    ; Press F1 in an editor to invert highlighted text
    
    F1::
    WinActivate % "ahk_id" hWnd 
    out := Clipboard := "" 
    Send ^c 
    ClipWait 0 
    If !ErrorLevel 
    {
        SendInput % "{Text}" invertCase(Clipboard)
        SendInput % "+{Left " StrLen(Clipboard) "}"
    }
    Else 
        MsgBox 48, ERROR, An ERROR occurred while waiting for the clipboard. 
    Return 
    
    
    invertCase(str) {
        inverted := ""
        Loop Parse, str
            inverted .= Format("{:" (A_LoopField ~= "[ÀÈÌÒÙA-Z]" ? "L" : "U") "}", A_LoopField)
        Return inverted
    }