?

Log in

No account? Create an account

Peter Rogers's Blog
Artist-in-Residence at Chez Firth

Thursday (2/28/13) 6:02pm - ... wherein Peter Can Now Easily Make His Text Sound Important.

Just procrastinated a lot by rewriting all the functions in my AutoHotkey script-file that say, "Take the currently-selected string and do something to it".

Previously, I had separate functions to:
* Replace spaces with hyphens
* Replace spaces with underscores
* Clean up text for HTML
* Remove facebook-incompatible HTML
* Toggle caps-lock on the string
* Alternate-caps-lock the string

I just rewrote all that code to share one common "process a string" function (which takes a function parameter for its specific behavior), and I added new code to Apply Initial Capitalization To All The Words In A String.

This is probably not of general interest.




; String-processing functions!

ReplaceSpaces(CurString, ReplacementChar)
{
        StringReplace CurString, CurString, %A_Space%, %ReplacementChar%, All
        return CurString
}

ToggleCase(CurString, Dummy)
{
        OldString := CurString
        StringUpper, CurString, OldString
        if (CurString == OldString)
        StringLower, CurString, CurString
        return CurString
}

CrazyCase(CurString, Dummy)
{
        StringUpper, CurString, CurString
        StringLength := StrLen(CurString)
        OutputString :=
        
        static StartWithCaps := false
        StartWithCaps := !StartWithCaps
        ModCompare := (StartWithCaps) ? 1 : 0
        
        Loop %StringLength% {
                NewChar := SubStr(CurString, A_Index , 1)
                if (Mod(A_Index, 2) == ModCompare)
                        StringLower, NewChar, NewChar
                        
                OutputString := OutputString . NewChar
        }
        
        return OutputString
}

InitialCase(CurString, Dummy)
{
        StringLower, CurString, CurString
        StringLength := StrLen(CurString)
        OutputString := 

        JustSawASpace := true
        Loop %StringLength% {
                NewChar := SubStr(CurString, A_Index , 1)
                if (JustSawASpace)
                        StringUpper, NewChar, NewChar
                        
                JustSawASpace := ((RegExMatch(NewChar, "\s")) != 0)
                        
                OutputString := OutputString . NewChar
        }

        return OutputString
}

FacebookClean(CurString, Dummy)
{
        StringReplace CurString, CurString, `r, , All
        StringReplace CurString, CurString, <hr>, , All
        StringReplace CurString, CurString, <font color="#008080">, , All
        StringReplace CurString, CurString, </font>, , All
        StringReplace CurString, CurString, </lj-cut>, , All
        CurString := RegExReplace(CurString, "<lj-cut text=""([\w\s])*"">") 
        CurString := RegExReplace(CurString, "<lj user=""([\w]*)"">", "<a href=""http://$1.livejournal.com"">$1</a>") 
        return CurString
}

HTMLClean(CurString, Dummy)
{
        Transform, CurString, HTML, %CurString%
        StringReplace CurString, CurString, <br>`n, , All
        return CurString
}


ProcessSelectedString(ProcessFunction, Parameter, Reselect)
{
        ClipSaved_PSS := ClipboardAll
        
        PreviousClip := Clipboard
        Send {Ctrl Down}c{Ctrl Up}
        Sleep 100
        CurString := Clipboard
        
        if (CurString != PreviousClip) {
                CurString := %ProcessFunction%(CurString, Parameter)
                
                SendInput {raw}%CurString%
                
                if (Reselect)
                {
                        StringLength := StrLen(CurString)
                        Lefts :=
                        Loop %StringLength%
                                Lefts = %Lefts%{Left}
                        SendInput {Shift Down}%Lefts%{Shift Up}
                }
        }
        
        Clipboard := ClipSaved_PSS
        return
}

; Convert spaces to hyphens
^-::
ProcessSelectedString("ReplaceSpaces", "-", false)
return

; Convert spaces to underscores
^!_::
ProcessSelectedString("ReplaceSpaces", "_", false)
return

; Upper-case this string.
; (Or, if it's already upper-case, lower-case it.)
$^CapsLock::
ProcessSelectedString("ToggleCase", "", true)
return

; Initial-case this string
$!CapsLock::
ProcessSelectedString("InitialCase", "", true)
return

; Alternate-case this string, middle-schooler-style
$^!CapsLock::
ProcessSelectedString("CrazyCase", "", true)
return

; Clean up text for facebook
^!+f::
ProcessSelectedString("FacebookClean", "", false)
return

; Clean up text for HTML
^!h::
ProcessSelectedString("HTMLClean", "", false)
return

Tags: ,
Mood: [mood icon] contemplative · Music: none
Previous Entry Share Next Entry

Comments:

[User Picture]
From:gregstoll
Date:Thursday (2/28/13) 10:59pm
(Link)
Interesting! I don't recognize the language - is it something AutoHotKey-specific, or something else?
(Reply to this) (Thread)
[User Picture]
From:hujhax
Date:Thursday (2/28/13) 11:29pm
(Link)
Yeah, AutoHotkey is its own little scripting language.  So that last section says, "when the user types ctrl-alt-h, run 'ProcessSelectedString' with the given parameters".
(Reply to this) (Parent) (Thread)