Capitalize initials in Clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Capitalize initials in Clipboard

Post by Avastgard » 13 May 2022, 15:33

A few days ago I posted this on reddit and, with the help of other users, I managed to put together the script below.

Code: Select all

#IfWinActive, ahk_exe WINWORD.EXE

F2::
    ClipSaved := ClipboardAll
    Clipboard := "" ; empty the clipboard
    Send, {CTRLDOWN}c{CTRLUP}
    if (Clipboard = "") {
        SendInput, ^+{Left}
        SendInput, ^c
        ClipWait, 0.5
        Send, ("
        Send, ^{s}%Clipboard%+{F3}^{s}
        Send, ")
    }
    else {
        Send, ("
        Send, ^{s}%Clipboard%+{F3}^{s}
        Send, ")
    }
    return
I use this to format defined terms in Microsoft Word, so that
parties
becomes
("Parties")
The script works reasonably well, except when I have to format more than one word, when it will only capitalize the last word. So
multiple words
become
("multiple Words")
when ideally it would be
("Multiple Words")
That happens because I am using Microsoft Word's shortcut to cycle between all lowercase, ALL UPPERCASE and Capitalized Initials. The shortcut is F3, but it might be a different one if you are using Microsoft Word in a different language (mine is in Portuguese). Another problem derived from that is that if the word is already capitalized when I hit the hotkey, it'll cycle to all uppercase, so that
multiple Words
becomes
("multiple WORDS")
So, what I want is a way to make the first letter of each word in the Clipboard upper case and all the other ones lower case, regardless of how they were initially. Is such a thing possible?

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Capitalize initials in Clipboard

Post by mikeyww » 13 May 2022, 15:50

Code: Select all

SendInput % "{Text}" Format("{:T}", Clipboard)

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 15 May 2022, 05:54

Thanks for the help!

In your example script, is "{Text}" supposed to be like that or should it be replaced with something?

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Capitalize initials in Clipboard

Post by boiler » 15 May 2022, 06:16

It is supposed to be like that. See text mode.

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 15 May 2022, 09:51

I tried this:

Code: Select all

#IfWinActive, ahk_exe WINWORD.EXE
F2::
    ClipSaved := ClipboardAll
    Clipboard := "" ; empty the clipboard
    Send, {CTRLDOWN}c{CTRLUP}
    if (Clipboard = "") {
        SendInput, ^+{Left}
        SendInput, ^c
        ClipWait, 0.5
        Send, ("
        Send, ^{s}
        SendInput % "{Text}" Format("{:T}", Clipboard)
        Send, ^{s}
        Send, ")
    }
    else {
        Send, ("
        Send, ^{s}
        SendInput % "{Text}" Format("{:T}", Clipboard)
        Send, ^{s}
        Send, ")
    }
    return
It works perfectly if I'm using just the first if (i.e., when I place the cursor after the word and press F2

But it doesn't work when I select one or more than one word in Microsoft Word.

If I select one word,
word
becomes
(“Word”)word
(note that the word is not underlined).

If I select two words,
multiple words
becomes
("Multiple ")words
Any idea why this is happening?

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Capitalize initials in Clipboard

Post by mikeyww » 15 May 2022, 10:03

I would try it this way.

Code: Select all

#IfWinActive ahk_exe WINWORD.EXE
F2::
oWord := ComObjActive("Word.Application"), Clipboard := "", oWord.Selection.Copy
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Clipboard := Format("({:T})", Clipboard), oWord.Selection.Paste
Return
#IfWinActive

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 16 May 2022, 16:08

Thanks again for the code, it runs almost perfectly!

Is there any way to include quotation marks on the pasted text? I assume that if parentheses are possible, then quotation marks may also be possible. I tried editing "({:T})" to "("{:T}")", but got an error message saying
Unexpected "{"
I would also like the text to be underlined, but I think that is something I will have to work out on my own (any ideas are appreciated, of course).

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Capitalize initials in Clipboard  Topic is solved

Post by mikeyww » 16 May 2022, 16:44

Code: Select all

#IfWinActive ahk_exe WINWORD.EXE
F2::
oWord := ComObjActive("Word.Application"), Clipboard := "", oWord.Selection.Copy
ClipWait, 0
If !ErrorLevel {
 Clipboard := Format("(""{:T}"")", Clipboard)
 oWord.Selection.Font.Underline := True
 oWord.Selection.Paste
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
#IfWinActive
Explained: Escape sequences
Within an expression, two consecutive quotes enclosed inside a literal string resolve to a single literal quote.

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 16 May 2022, 17:58

Awesome, it's excellent now! Thank you very much!

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 01 Jun 2022, 09:00

Hey there, came back here to ask if there is a way to make only the word itself be underlined (i.e., not underline the quotes and parentheses).

I tried this:

Code: Select all

F2::
oWord := ComObjActive("Word.Application"), Clipboard := "", oWord.Selection.Copy
ClipWait, 0
If !ErrorLevel {
 oWord.Selection.Font.Underline := True
 Clipboard := Format("(""{:T}"")", Clipboard) 
 oWord.Selection.Paste
 } Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
But it works the same as the previous code, so
example
becomes
("Example")
and not
("Example")
☝ this is what I want

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Capitalize initials in Clipboard

Post by mikeyww » 01 Jun 2022, 09:12

You may want to try inserting the unformatted text first, then set the underlining, and then insert the text to be underlined.

Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: Capitalize initials in Clipboard

Post by Avastgard » 29 Jun 2022, 14:01

I assume I have to edit line 5 to insert ("") in the clipboard, but I can't do it for the life of me without messing up with the formatting. Do you have any suggestions on how I could do it? Simple Send commands do not seem to be very reliable and tend to execute out of sync with the rest of the script.

Code: Select all

F2::
oWord := ComObjActive("Word.Application"), Clipboard := "", oWord.Selection.Copy
ClipWait, 0
If !ErrorLevel {
 Clipboard := Format("{:T}", Clipboard)
 oWord.Selection.Font.Underline := True
 oWord.Selection.Paste
 } Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return

User avatar
mikeyww
Posts: 26607
Joined: 09 Sep 2014, 18:38

Re: Capitalize initials in Clipboard

Post by mikeyww » 29 Jun 2022, 18:02

My last script posted in this thread shows how to do it.

Post Reply

Return to “Ask for Help (v1)”