Uppersace/Lowersace Swap Script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Uppersace/Lowersace Swap Script?

02 Dec 2016, 10:41

So I see StringUpper and StringLower to convert a string entirely to uppercase or lowercase, but that's not exactly what I want. My job requires me to use CapsLock a lot due to things that have to be written all in caps, and I often forget and write things that shouldn't be caps with capslock, or vice versa. What I am trying to make is a hotkey that takes the text I have selected, copies it, switches the case of each letter, and pastes it. So that, for example: "hI i AM bOB" becomes "Hi I am Bob".

Is there an easier way to do this than looking at the string letter by letter? (Alternatively, is there an easy way to loop through the string letter by letter?)
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Uppersace/Lowersace Swap Script?  Topic is solved

02 Dec 2016, 10:48

MaxAstro wrote:(Alternatively, is there an easy way to loop through the string letter by letter?)
try this:

Code: Select all

text = just a test

loop, parse, text
msgbox, %A_LoopField%
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 11:10

MaxAstro wrote:What I am trying to make is a hotkey that .. switches the case of each letter
http://lmgtfy.com/?q=ahk+toggle+case

first result searches letter by letter and adds or subtracts 32 from the ASCII value


MaxAstro wrote: (is there an easy way to loop through the string letter by letter?)
https://autohotkey.com/docs/commands/LoopParse.htm


MaxAstro wrote: Is there an easier way to do this than looking at the string letter by letter?
second result shows a simple RegExReplace. this is probably easiest:

Code: Select all

string := "hI i AM bOB"
converted := RegExReplace(string, "([A-Z])|([a-z])", "$L1$U2")
MsgBox, %converted%



MaxAstro wrote: So that, for example: "hI i AM bOB" becomes "Hi I am Bob".
of course, Vim has the command g~ to toggle case, but i'm guessing you dont want to spend months learning a whole new text editor :HeHe:

Image

MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 11:58

Guest3456, thanks for the detailed set of options. :)

I think I'm gonna go with V's idea of using a parse loop, though - more readable than a RegEx.

Thanks everyone!
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 11:59

MaxAstro wrote: I think I'm gonna go with V's idea of using a parse loop, though - more readable than a RegEx.
thats a fine good idea. code is always read much more than it is written. so when it comes time to edit the script later, you want to be able to quickly understand whats going on. so i always support more readable code. if regex is difficult for you, stick with the loop.

MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 12:44

I've used a RegEx in exactly one place in my code, and I slightly regret it to this day - while it is by far the best way of doing what that bit of code needs to do, every time I scroll past it I think "what the heck does that do?" and it takes me a couple seconds to remember.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 12:57

Hrm. This is odd. So I set up a script that causes Shift + CapsLock to toggle the capslock state of the current selection, and it works fine. However, I would like Shift + CapsLock to also toggle capslock on or off. I tried two different ways of doing this, one by having the hotkey be ~+CAPSLOCK, and the other by adding Send {Capslock} at the end of the script. Both had the same result: if capslock was off, shift + capslock would turn it on. But if capslock is on, shift + capslock does not turn it off. Any idea why? Here is my script:

Code: Select all

+CAPSLOCK::
	ClipSave := ClipboardAll
	clipboard = 
	Send ^c
	While clipboard = 
	{
		Sleep, -1
	}
	Loop, parse, clipboard
	{
		if A_Loopfield is not alpha
			TempVar1 := A_Loopfield
		else if A_Loopfield is upper
			StringLower, TempVar1, A_Loopfield
		else if A_Loopfield is lower
			StringUpper, TempVar1, A_Loopfield
		Result .= TempVar1
	}
	Send % Result
	clipboard := ClipSave
	Result := ""
	Send {CapsLock}
	return
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 13:35

see if using the $ modifier for the hotkey definition changes it

$+CAPSLOCK::

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 13:55

Not sure if it's useful here... but a neat trick is that XOR'ing an ANSI A-Z character with the number 32 produces the lowercase character and vice-versa.
(It won't work for alpha characters that are not A-Z)

Code: Select all

Test := "abc123XYZ"

Loop, parse, Test
{
    if A_Loopfield is not alpha
        Result .= A_Loopfield
    else
        Result .= Chr(Asc(A_LoopField) ^ 0x20)
}
MsgBox, % Result
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 14:07

$ modifier does not seem to help.

Another issue I am running into: curly braces disappear when this script is run. For example, "{hello}" gets changed to "HELLO". Not sure why this would be the case...
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Uppersace/Lowersace Swap Script?

02 Dec 2016, 19:20

MaxAstro wrote:Another issue I am running into: curly braces disappear when this script is run. For example, "{hello}" gets changed to "HELLO". Not sure why this would be the case...
see the help page for Send. curly braces have special meaning for that command. you'll probably need to escape them or use SendRaw


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, mebelantikjaya, ThePeter and 241 guests