So I've been typing away at this problem for the better part of a day now and it's driving me nuts.
What I'm trying to do is fairly simple, I want to highlight text on the screen, run a function and if the text has capitals in it, replace it with a lowercase version, otherwise replace it with a sentence case version. Once I get this working I'll then make adjustments to capitalize each word.
The process I have so far goes as follows:
1) Send highlighted text to clipboard
2) Retrieve clipboard contents and store in variable
3) Use parsing loop to store each word in an array index
4) Use StrSplit to store each letter in an array index
5) Check if the letter is upper or lower case, if upper, set a flag to 1 ***
6) Modify the stored sentence according to the flag setting
7) Output the modified sentence
The problem I'm having is with Step 6, the offending code is:
Code: Select all
CheckCase(inArr) {
result:=0
Loop % inArr.length() {
tmpVar2:=inArr[A_Index]
if (%tmpVar2% is upper)
{
result:=1
}
if (result == 1)
{
return result
}
}
return result
}
Code: Select all
if (t is upper)
{
result:=1
}
if (tmpVar2 is upper)
{
result:=1
}
if (tmpVar2 is upper)
{
result:=1
}
if (%tmpVar2% is upper)
{
result:=1
}
My full code is below, can someone point out where I'm going wrong and save what's left of my sanity? Thanks!
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;AutoSentenceCase - This script takes selected text into the clipboard and converts between lower and sentence case.
;If any of the words are capatalised they will all be made lower case, if all of the words are already lower case sentence case will be applied.
Sleep, 300
Send ^c
Sleep, 100
inpArr:=[]
inpStr:=Clip()
i:=1
;Splits the string into its constituent words
Loop, parse, inpStr, " ",
{
inpArr[i]:=A_LoopField
i++
}
Loop % inpArr.length() {
tmpArr:=StrSplit(inpArr[A_Index])
result:=CheckCase(tmpArr)
If (result=1)
{
outStr:=ChangeCase(inpStr,"L")
break
}
else
{
outStr:=ChangeCase(inpStr,"S")
}
}
SendRaw, %outStr%
ExitApp
Clip(Text="", Reselect="") ; http://www.autohotkey.com/forum/viewtopic.php?p=467710 , modified February 19, 2013
{
Static BackUpClip, Stored, LastClip
If (A_ThisLabel = A_ThisFunc) {
If (Clipboard == LastClip)
Clipboard := BackUpClip
BackUpClip := LastClip := Stored := ""
} Else {
If !Stored {
Stored := True
BackUpClip := ClipboardAll ; ClipboardAll must be on its own line
} Else
SetTimer, %A_ThisFunc%, Off
LongCopy := A_TickCount, Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need
If (Text = "") {
SendInput, ^c
ClipWait, LongCopy ? 0.6 : 0.2, True
} Else {
Clipboard := LastClip := Text
ClipWait, 10
SendInput, ^v
}
SetTimer, %A_ThisFunc%, -700
Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
If (Text = "")
Return LastClip := Clipboard
Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) {
StringReplace, Text, Text, `r, , All
SendInput, % "{Shift Down}{Left " StrLen(Text) "}{Shift Up}"
}
}
Return
Clip:
Return Clip()
}
ChangeCase(String,Type) { ;type is S,I,U,L, or T
If (Type="S") { ;Sentence case.
X = I,AHK,AutoHotkey ;comma seperated list of words that should always be capitalized
S := RegExReplace(RegExReplace(String, "(.*)", "$L{1}"), "(?<=[\.\!\?]\s|\n).|^.", "$U{0}")
Loop Parse, X, `, ;Parse the exceptions
S := RegExReplace(S,"i)\b" A_LoopField "\b", A_LoopField)
Return S
}
If (Type="I") ;iNVERSE
Return % RegExReplace(String, "([A-Z])|([a-z])", "$L1$U2")
Return % RegExReplace(String, "(.*)", "$" Type "{1}") ;All other types
}
CheckCase(inArr) {
result:=0
Loop % inArr.length() {
tmpVar2:=inArr[A_Index]
if (%tmpVar2% is upper)
{
result:=1
}
if (result == 1)
{
return result
}
}
return result
}