Change the case of text in-line using AHK v2 Menu()

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

Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 09:32

This script was inspired by a AutoHotkey v1 script I altered and used for several years - sourced from here.

The script below is in AutoHotkey v2 and uses a pop up menu that allows you to select the type of case you want the text to be changed to.
It should work with special characters such as é and â (unicode compatible). See TestString below for a more comprehensive example.

Code: Select all

#Requires AutoHotkey v2.0

!c:: { ; ALT + C
CaseChangeMenu := Menu()
CaseChangeMenu.Delete
CaseChangeMenu.Add("&1 lower case",ConvertLower)
CaseChangeMenu.Add("&2 Sentence case",ConvertSentence)
CaseChangeMenu.Add("&3 Title Case",ConvertTitle)
CaseChangeMenu.Add("&4 UPPER CASE",ConvertUpper)
CaseChangeMenu.Add("&5 iNVERT cASE",ConvertInvert)
CaseChangeMenu.Show
}

ConvertLower(*) {
CallClipboard(2)
A_Clipboard := StrLower(A_Clipboard)
CaseConvert()
}

ConvertSentence(*) {
CallClipboard(2)
lowered := StrLower(A_Clipboard)
A_Clipboard := RegExReplace(lowered, "(((^\s*|([.!?]+\s*))[a-z])|\Wi\W)", "$U1") ; Code Credit #1
CaseConvert()
}

ConvertTitle(*) {
CallClipboard(2)
A_Clipboard := StrTitle(A_Clipboard)
CaseConvert()
}

ConvertUpper(*) {
CallClipboard(2)
A_Clipboard := StrUpper(A_Clipboard)
CaseConvert()
}

ConvertInvert(*) {
CallClipboard(2)
inverted := ""
Loop Parse A_Clipboard {     ; Code Credit #2
  if (StrLower(A_LoopField) == A_LoopField) ; * Code Credit #3
    inverted .= StrUpper(A_LoopField)       ; * 
  else                                      ; * 
    inverted .= StrLower(A_LoopField)       ; * 
    }
A_Clipboard := inverted
CaseConvert()
}
; TestString      := "abcdefghijklmnopqrstuvwxyzéâäàåçêëèïîìæôöòûùÿáíóúñ`n"
;                  . "ABCDEFGHIJKLMNOPQRSTUVWXYZÉÂÄÀÅÇÊËÈÏÎÌÆÔÖÒÛÙŸÁÍÓÚÑ"
; TestInverted    := "ABCDEFGHIJKLMNOPQRSTUVWXYZÉÂÄÀÅÇÊËÈÏÎÌÆÔÖÒÛÙŸÁÍÓÚÑ`n"
;                  . "abcdefghijklmnopqrstuvwxyzéâäàåçêëèïîìæôöòûùÿáíóúñ"

CaseConvert() {
Len := "0"
Len := "+{left " Strlen(A_Clipboard) "}"
Send "^v" ; Pastes new text
Send Len  ; and selects it
}

; Code Credit #1 NeedleRegEx pattern modified from the one posted by ManaUser here - //www.autohotkey.com/board/topic/24431-convert-text-uppercase-lowercase-capitalized-or-inverted/?p=158295
; Code Credit #2 idea for loop from kon's post here - https://www.autohotkey.com/boards/viewtopic.php?p=58417#p58417
; Code Credit #3 - 4 lines of code with a comment "; *" were adapted from a (inaccurate) answer generated from a auto-query to DuckDuckGPT by KudoAI via https://greasyfork.org/en/scripts/459849-duckduckgpt

;  = Call Clipboard and ClipWait

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

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.
Any suggestions for improving the code are welcome.
Last edited by xypha on 24 Dec 2023, 00:44, edited 5 times in total.
User avatar
andymbody
Posts: 995
Joined: 02 Jul 2017, 23:47

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 10:41

FYI... a couple bugs in the code

Will not run until these 2 steps are done
1. Add closing brace to line 61 - for CaseConvert()
2. Delete (rather than comment out) line 53 - not sure why this line causes a missing brace error even after adding the brace on line 61
; . "abcdefghijklmnopqrstuvwxyzéâäàåçêëèïîìæôöòûùÿáíóúñ"

After these changes, the code runs and works well... thanks!
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 12:40

andymbody wrote:
18 Dec 2023, 10:41
1. Add closing brace to line 61 - for CaseConvert()
That was a copy paste error, corrected now. I will be more careful when posting code.
2. Delete (rather than comment out) line 53 - not sure why this line causes a missing brace error even after adding the brace on line 61
; . "abcdefghijklmnopqrstuvwxyzéâäàåçêëèïîìæôöòûùÿáíóúñ"
Moved the TestString out of the braces. didn't get an error when I downloaded it and ran it.
Glad you found this useful :)
User avatar
andymbody
Posts: 995
Joined: 02 Jul 2017, 23:47

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 13:16

xypha wrote:
18 Dec 2023, 12:40
didn't get an error when I downloaded it and ran it
Could be an interpreter bug in the version of Scite4Autohotkey that I am using.

Also, when this tool is used in that app, the text that receives the edit will be the text that is displayed in the "Find" toolbar, rather than the highlighted text in the editor. This is not the fault of your script of course, it is because the find bar gains focus immediately after the popup menu closes, and focus returns to Scite (but only when the Find toolbar is visible). But I thought you might want to be aware of this side effect, in case it affects other apps as well.
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 14:14

andymbody wrote:
18 Dec 2023, 13:16
in case it affects other apps as well.
I don't use Scite, so it doesn't affect me. And this is the only script that has such a TestString, so your post should help those who encounter an error.
Thanks for the explanation.
Also, there was an error in CallClipboard function. I had forgotten to change Clipboard to A_Clipboard. Corrected that in the 1st post. Please change that bit of code in your script as well, if you haven't noticed it yet.
User avatar
andymbody
Posts: 995
Joined: 02 Jul 2017, 23:47

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 14:32

xypha wrote:
18 Dec 2023, 14:14
Please change that bit of code in your script
Ok... thanks for the heads up! No I did not notice the v1 clipboard command. Maybe that's because I bounce between versions so often, ping-pong balls appear to be standing still...
User avatar
xypha
Posts: 24
Joined: 24 Apr 2020, 04:14

Re: Change the case of text in-line using AHK v2 Menu()

18 Dec 2023, 14:38

@andymbody
Argh! One more correctIon. ClipboardAll to ClipboardAll()
There are probably more snafu's in there... somewhere. Sorry.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: vmech and 51 guests