Jump to content

Switch & Case help needed


Recommended Posts

Hi there,

I'm writing this script which is supposed to modify certain selected letters when a hotkey is pressed.

It should work like this: select text -> press hotkey -> letter changes .

But my switch statement seems to bug up, and I have no idea why. It always seems to active the first case code. Here's my code, use 1 to turn a letter (A,E,O and all their variants) into a "short" one, and 2 to make it a "long" one. Press 0 to generate a short text you can use to test:

HotKeySet("1","Replace")
HotKeySet("2","Replace")
HotKeySet("0","GenerateTestText")

While 1
WEnd

Func Replace()
    If WinActive("[CLASS:Notepad]") Then
        Send("^c")
        $Letter = ClipGet()
        ;MsgBox(0,"",$Letter)
        Switch $Letter

            Case "a" or ChrW(dec("0101")) or ChrW(dec("0103")) or "A" or ChrW(dec("0100")) or ChrW(dec("0102")) ;A and variants
                ;MsgBox("Case",$Letter & " <-Letter = Clipget-> " & ClipGet() ,"Case actived: A and variants")
                If StringIsLower($Letter) Then ;LOWER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("0101")),1) ;long
                    Else
                        Send(ChrW(dec("0103")),1) ;short
                    EndIf
                Else                            ;UPPER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("0100")),1) ;long
                    Else
                        Send(ChrW(dec("0102")),1) ;short
                    EndIf
                EndIf

            Case "e" or ChrW(dec("0113")) or ChrW(dec("0115")) or "E" or ChrW(dec("0112")) or ChrW(dec("0114")) ;E and variants
                ;MsgBox("Case",$Letter & " <-Letter = Clipget-> " & ClipGet() ,"Case actived: E and variants")
                If StringIsLower($Letter) Then  ;LOWER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("0113")),1) ;long
                    Else
                        Send(ChrW(dec("0115")),1) ;short
                    EndIf
                Else                            ;UPPER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("0112")),1) ;long
                    Else
                        Send(ChrW(dec("0114")),1) ;short
                    EndIf
                EndIf

            Case "o" or ChrW(dec("014D")) or ChrW(dec("014F")) or "O" or ChrW(dec("014E")) or ChrW(dec("014C")) ;O and variants
                ;MsgBox("Case",$Letter & " <-Letter = Clipget-> " & ClipGet() ,"Case actived: O and variants")
                If StringIsLower($Letter) Then  ;LOWER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("014D")),1) ;long
                    Else
                        Send(ChrW(dec("014F")),1) ;short
                    EndIf
                Else                            ;UPPER CASE
                    If @HotKeyPressed = "1" Then
                        Send(ChrW(dec("014E")),1) ;long
                    Else
                        Send(ChrW(dec("014C")),1) ;short
                    EndIf
                EndIf

        EndSwitch
    Else
        MsgBox(0,"Wrong Window","Use notepad.")
    EndIf
EndFunc

Func GenerateTestText()
    Run("notepad.exe")
    Sleep(1000)
    If WinActive("[CLASS:Notepad]") Then
        Send("A and variants: A a A a" & @CRLF & _
     "E and variants: E e E e" & @CRLF & _
     "O and variants: O o O o")
    EndIf
EndFunc

Thanks a lot :(

P.S: "debug" things are commented out, you may want to remove the ;

Edit: Apparently when you put a different case on top, it uses that one instead, so basically it always runs the first case code. Also, when using If $Letter = ... (instead of a switch) it runs all of them, so apparently it's all true.

Edited by nf67
Link to comment
Share on other sites

It may be:

;...
Switch $Letter

    Case "a", ChrW(dec("0101")), ChrW(dec("0103")), "A", ChrW(dec("0100")), ChrW(dec("0102")) ;A and variants
;...
EndSwitch
;...

UDFS & Apps:

Spoiler

DDEML.au3 - DDE Client + Server
Localization.au3 - localize your scripts
TLI.au3 - type information on COM objects (TLBINF emulation)
TLBAutoEnum.au3 - auto-import of COM constants (enums)
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector - OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Link to comment
Share on other sites

, instead of or gives me a syntax error :(

Ensure that you've replaced ALL or. No syntax errors here.

UDFS & Apps:

Spoiler

DDEML.au3 - DDE Client + Server
Localization.au3 - localize your scripts
TLI.au3 - type information on COM objects (TLBINF emulation)
TLBAutoEnum.au3 - auto-import of COM constants (enums)
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector - OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Link to comment
Share on other sites

Thanks so much, perfect :(. Must have missed one, even though I used Ctrl H.

Glad to hear.

You may also consider this:

Dim $Letters[18] = ["a", "A", "ä", "Ä", "á", "Á", "e", "E", "ê", "Ê", "o", "O", "ö", "Ö", "õ", "Õ", "b", "#"]

For $Letter In $Letters
    Dim $c = AscW($Letter)
    $c -= BitAnd($c, 0x20)
    Switch $c
        Case 0x0041, 0x00c0 To 0x00c6 ;A and variants
            ConsoleWrite("CASE A" & @LF)
        Case 0x0045, 0x00c8 To 0x00cb ;E and variants
            ConsoleWrite("CASE E" & @LF)
        Case 0x004f, 0x00d2 To 0x00d6 ;O and variants
            ConsoleWrite("CASE O" & @LF)
        Case Else
            ConsoleWrite("CASE ELSE" & @LF)
    EndSwitch
Next

Kinda simpler, isn't it?

UDFS & Apps:

Spoiler

DDEML.au3 - DDE Client + Server
Localization.au3 - localize your scripts
TLI.au3 - type information on COM objects (TLBINF emulation)
TLBAutoEnum.au3 - auto-import of COM constants (enums)
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector - OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Link to comment
Share on other sites

Perhaps it is, but had some trouble getting the characters I use into SciTe, only managed to do so using Copy & Paste, and then they'd still change after saving and/or running the script. Can't really be bothered to look into all the encoding options :(.

Link to comment
Share on other sites

Are you just replacing the screwy foreign characters?

This also works for that purpose:

Dim $aConvert[7][2] = [["[àáâãäå]","a"],["[éèêë]","e"],["[ìíîï]","i"],["ñ","n"],["[òóôõö]","o"],["[ùúûü]","u"],["[ýÿ]","y"]]

$in = "èïabcåñ heýýoü"

For $i = 0 to 6 ; loop for a,e,i,n,o,u,y replacements
    $in = StringRegExpReplace($in, $aConvert[$i][0], $aConvert[$i][1])
Next

MsgBox (0, "result", $in)
Link to comment
Share on other sites

Perhaps it is, but had some trouble getting the characters I use into SciTe, only managed to do so using Copy & Paste, and then they'd still change after saving and/or running the script. Can't really be bothered to look into all the encoding options :(.

You misunderstood my sample: you don't have to define the $Letters array (it was only there for demonstration), just read the letters the way you'd done before.

UDFS & Apps:

Spoiler

DDEML.au3 - DDE Client + Server
Localization.au3 - localize your scripts
TLI.au3 - type information on COM objects (TLBINF emulation)
TLBAutoEnum.au3 - auto-import of COM constants (enums)
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector - OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...