AutoHotkey Community

It is currently May 27th, 2012, 1:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: October 26th, 2011, 11:15 pm 
Offline

Joined: July 29th, 2011, 7:23 pm
Posts: 24
hello, i would like to put a string in title case, all of it except for acronyms that are in the format of two or three capped letter surrounded by spaces, i.e. " [A-Z][A-Z] " and " [A-Z][A-Z][A-Z] ".

i have integrated some code I found in a different help topic but i does not appear to be working:

Code:
^RButton::
ClipSaved := ClipboardAll
send, ^c
sleep, 100

pos = 0 ; initial offset
Loop
   If pos := RegExMatch(clipboard, "\w+\b(?=(.*?)(?:\w|$))", word, ++pos) { ; match word
      pos += StrLen(word) ; add word length to offset
      StringUpper, caps, word
      If !(caps == word) ; if word is not all caps
         StringUpper, word, word, T ; change it to title case
      str .= word . word1
   } Else Break ; no more matches

StringCaseSense, On
StringReplace, clipboard, clipboard, %A_Space%And%A_Space%, %A_Space%and%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%Into%A_Space%, %A_Space%into%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%And%A_Space%, %A_Space%and%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%Via%A_Space%, %A_Space%via%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%From%A_Space%, %A_Space%from%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%With%A_Space%, %A_Space%with%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%In%A_Space%, %A_Space%in%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%At%A_Space%, %A_Space%at%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%For%A_Space%, %A_Space%for%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%To%A_Space%, %A_Space%to%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%Or%A_Space%, %A_Space%or%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%But%A_Space%, %A_Space%but%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%The%A_Space%, %A_Space%the%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%Of%A_Space%, %A_Space%of%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%A%A_Space%, %A_Space%a%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%An%A_Space%, %A_Space%an%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%By%A_Space%, %A_Space%by%A_Space%, All
StringReplace, clipboard, clipboard, %A_Space%On%A_Space%, %A_Space%on%A_Space%, All
Clipboard := RegExReplace(Clipboard, "(-[a-z])", "$u1")
send, ^v
Clipboard := ClipSaved
ClipSaved =
return


I am at a loss :roll: any help would be greatly appreciated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2011, 11:40 pm 
Offline

Joined: February 16th, 2010, 8:01 am
Posts: 800
Location: SciTE
Something like this?

Code:
MaxAcroLength := 3 ; Maximmum number of letters for the Acronyms


^RButton::
ClipSaved := ClipboardAll
Clipboard := ""
send, ^c
ClipWait

Loop, Parse, Clipboard, %A_Space% ; Parse by spaces
{
   If (RegExMatch(A_LoopField, "[A-Z]{2," MaxAcroLength "}")) ; Check for Acronym
   {
         if (NewClipboard == "") ; If new clipboard is blank write this                        ; Code for Acronyms   
         NewClipboard = %A_LoopField%
         Else               ; If there is content, write this
         NewClipboard = %NewClipboard% %A_LoopField%
   }
   Else if (!RegExMatch(A_LoopField, "[A-Z]{2," MaxAcroLength "}"))               ; Title Case everything else
   {
      StringUpper, B_LoopField, A_Loopfield, T
      if (NewClipboard == "") ; If new clipboard is blank write this
         NewClipboard = %B_LoopField%
      Else               ; If there is content, write this
         NewClipboard = %NewClipboard% %B_LoopField%
   }
}
Clipboard := NewClipboard ; Assign new set to clipboard
NewClipboard := "" ; Clear new clipboard data
Send, ^v ; Send clipboard
Sleep 100
Clipboard := "" ; Blank clipboard
Clipboard := ClipSaved ; Restore clipboard content
ClipWait ; Wait for clipboard to have content
return


Edit: You can replace ^v with a msgbox to see the content obviously.

_________________
AutoHotkey Basic - Windows 7
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2011, 10:33 pm 
Offline

Joined: July 29th, 2011, 7:23 pm
Posts: 24
yes thank you very much :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 7:14 am 
Here is another solution. It does not capitalize articles, conjunctions and prepositions unless they are in the start of the sentence.
Maybe the list of them needs editing. My English grammar is terrible.
Code:
Needle =
(join ltrim comments
  (^|[.!?:;])\W*\K(([A-Z]{2,4})\b          ; first (in the sentence) acronym (upper case $U3)
                  |([\w']+))               ; any other first word   (title case $T4)
  |\b(?i)(a|after|an|and|at|but|by|for|from
     |in|nor|of|on|or|s|the|through|to)\b  ; not first small words (lower case $L5)
  |\b(?-i)([A-Z]{2,4})\b                   ; not first acronym  (upper case $U6)
  |\b([\w']+)                              ; any other word   (title case $T7)
)

^t::       ; Select some text and press Ctrl-t to test
  Send ^c
  MsgBox % RegExReplace(Clipboard,Needle,"$U3$T4$L5$U6$T7")
  return


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 6:20 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
GoGo thanks for the modified Title Case script and OP thanks for the idea.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 14th, 2011, 12:42 am 
Offline

Joined: July 29th, 2011, 7:23 pm
Posts: 24
thank you, it seems to be working great.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 14th, 2011, 10:20 pm 
Offline

Joined: July 29th, 2011, 7:23 pm
Posts: 24
Thanks, jpjazzy. I turned autotrim off and it stopped removing the spaces.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: brilliant!
PostPosted: December 8th, 2011, 6:38 am 
Gogo wrote:
Here is another solution. It does not capitalize articles, conjunctions and prepositions unless they are in the start of the sentence.


I would only suggest taking the "through" out - the principle isn't "prepositions" as such, just "very short and unimportant words", so there very well may be more to add.

Would you mind having a look at this? I'm trying to adapt your code to the

Code:
TITLE:
Gosub,COPY
StringLower,string,string,T
Gosub,PASTE
Return

section of Nascent's version.

http://www.autohotkey.com/forum/topic4559.html

I'm a total noob, both to AHK and coding in general, and for some reason I can't get the variable "string" to match the contents of "clipboard", the output of Needle, or whatever.

Feel free to post your reply over in that thread if you think that's more appropriate.

Thanks so much in advance for your help, this RegEx stuff is a real mind-bender. . .


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 8th, 2011, 9:22 pm 
To build in this script into that one you should place the needle somewhere in the auto-execute section before all hotkeys and subroutines. If you are uncertain where it is, put it on the top of the whole script.

In the TITLE: subroutine you should replace StringLower,string,string,T with string := RegExReplace(string,Needle,"$U3$T4$L5$U6$T7")
Code:
Needle =              ;
(join ltrim comments  ; put these lines in the
; regex ... regex     ; auto-execute section
)                     ;

; ...   ...   ...   ...

TITLE:
Gosub,COPY
string := RegExReplace(string,Needle,"$U3$T4$L5$U6$T7")
Gosub,PASTE
Return

You might be interested in these two related threads
http://www.autohotkey.com/forum/viewtop ... 546#471546
http://www.autohotkey.com/forum/viewtop ... 213#493213


Report this post
Top
  
Reply with quote  
 Post subject: Gogo you da man!
PostPosted: December 9th, 2011, 8:18 am 
Works perfectly!

I take it the string "Needle" ('function' name?) needs to be unique, so if I need more than one such block ('function'? 'routine'?) I could name them Needle1, Needle2 etc?

And I walked through the other threads you cited on my way here, but I'll go back and take a closer look at the script you posted in the second one.

Thanks a lot!


Report this post
Top
  
Reply with quote  
 Post subject: I know I'm being greedy
PostPosted: December 9th, 2011, 11:21 am 
Gogo (or anyone)

As you know, no good deed goes unpunished 8-)

I realize I'm asking a lot, but I think it's a worthwhile functionality - convert "Phrases with spaces_or_underscores" to camelCase (Wiki-style) "phrasesWithSpacesOrUnderscores"

There's a well-working example here going the other way (camelCase to ALL_UPPER_WITH_UNDERSCORES), and I've spent some time monkeying around but I'm just not up to that level yet.

http://www.autohotkey.com/forum/post-495756.html#495756

Also I'm sure you'd prefer doing it in RegEx, whatever works 8-)

If you don't have time, obviously just ignore and thanks anyway for all your other contributions here. . .


Report this post
Top
  
Reply with quote  
PostPosted: December 9th, 2011, 12:10 pm 
Offline

Joined: February 16th, 2010, 8:01 am
Posts: 800
Location: SciTE
Something like this? (F1 to toggle on and once you have selected a piece of text it will automatically turn off so you don't trigger it again until wanted.).

Code:
SetBatchLines, -1
Hotkey, ~LButton Up, SelectText
Hotkey, ~LButton Up, Off

F1:: ; Toggles on and off
T := !T
If (T)
{
    Hotkey, ~LButton Up, On
    ToolTip, CamelCase conversion on clipboard ENABLED
    SetTimer, ToolTipOff, -3000  ; Turns off the tooltip in 3 seconds
}
else
{
    Hotkey, ~LButton Up, Off
    ToolTip, CamelCase conversion on clipboard DISABLED
    SetTimer, ToolTipOff, -3000  ; Turns off the tooltip in 3 seconds
}
return

SelectText:
Clipboard := ""
SendInput, ^c
ClipWait, 0.2
Clipboard := ConvertToCamel(Clipboard)
ToolTip, Converted clipboard to CamelCase
Hotkey, ~LButton Up, Off
T := !T
SetTimer, ToolTipOff, -3000  ; Turns off the tooltip in 3 seconds
return




ToolTipOff: ; Turns off tooltip
ToolTip
return

ConvertToCamel( ConvertToCamelString ){
    StringReplace, ConvertToCamelString, ConvertToCamelString, _, %A_Space%, 1
    StringUpper, ConvertToCamelString, ConvertToCamelString, T ; Title case
    EndPos := RegExMatch(ConvertToCamelString, " ")
    FirstWord := SubStr(ConvertToCamelString, 1, EndPos-1)
    RestWords := SubStr(ConvertToCamelString, EndPos)
    StringLower, FirstWord, FirstWord
    ConvertToCamelString := FirstWord RestWords
    StringReplace, ConvertToCamelString, ConvertToCamelString, %A_Space%,, 1
    return ConvertToCamelString
}

_________________
AutoHotkey Basic - Windows 7
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: jpjazzy rocks!
PostPosted: December 10th, 2011, 2:55 am 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
Like a charm jpjazzy, learned a lot of other stuff from your sample as well that will come in handy!

Thanks so much. . .

------------
I've already started posting scripts, but my first effort is "hidden" by the forum software, any idea why?

http://www.autohotkey.com/forum/viewtopic.php?t=79763


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: jpjazzy rocks!
PostPosted: December 10th, 2011, 3:07 am 
Offline

Joined: February 16th, 2010, 8:01 am
Posts: 800
Location: SciTE
The only reason I can think of is a bug. I also have a bug (since firefox version 8) where I can no longer expand scripts. AHK forum could do with some scripting updates website-wise. I see it made you make an account though, so in all honesty I can't say I see it as a bad thing. :lol:

What other samples are you referring to specifically?

_________________
AutoHotkey Basic - Windows 7
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 10th, 2011, 4:15 am 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
Maybe anti-spam - because I included an external URL, first time as a guest, then I signed up but it was still my first post.

I didn't say samples (plural), just talking about the toggling-hotkey-status code in your snippet, very cool, although I'm currently just using the function (is that the right term?) you defined.

Thanks again. . .


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, chaosad, Google Feedfetcher, Yahoo [Bot] and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group