Inserting a coloured "character"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DavidPaul
Posts: 12
Joined: 04 Apr 2017, 18:28

Inserting a coloured "character"

Post by DavidPaul » 04 Dec 2021, 16:16

I play contract bridge and want to be able to quickly insert one of the four suit symbols into a Google Docs document. The clubs and spades symbols need to be black and the diamonds and hearts symbols need to be red, like this:
image.png
image.png (5.93 KiB) Viewed 389 times
Thanks in advance.

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

Re: Inserting a coloured "character"

Post by mikeyww » 04 Dec 2021, 17:23

Code: Select all

F2::paste("diamonds")
F3::paste("clubs")
F4::paste("spades")
F5::paste("hearts")

paste(suit) {
 Clipboard := "", setClipboardBitmap(A_ScriptDir "\" suit ".png")
 ClipWait, 0, WaitForAnyData := True
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the clipboard.
 Else Send ^v
}

setClipboardBitmap(Filename) {            ; By SKAN on D3AG @ tiny.cc/setclipboardbitmap
 Local  HBM := 0,  RES := 0,  OCB := 0,  CF_BITMAP := 2
 If ( HBM := LoadPicture(Filename) )
  If ( HBM := DllCall("CopyImage", "Ptr",HBM, "Int",0, "Int",0, "Int",0, "Int",8, "Ptr") )
   If ( OCB := DllCall("OpenClipboard", "Ptr",A_ScriptHwnd) )
    If DllCall("EmptyClipboard")
     RES := DllCall("SetClipboardData", "Int",CF_BITMAP, "Ptr",HBM)
 OCB := OCB ? DllCall("CloseClipboard")*0 : 0
 HBM := HBM ? DllCall("DeleteObject", "Ptr",HBM)*0 : 0
 Return !!RES
}
Attachments
diamonds.png
diamonds.png (247 Bytes) Viewed 372 times
hearts.png
hearts.png (265 Bytes) Viewed 372 times
spades.png
spades.png (248 Bytes) Viewed 372 times

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Inserting a coloured "character"

Post by flyingDman » 04 Dec 2021, 18:01

Or you can use ascii characters for these 4. Make sure you encode your script UTF-8 with BOM:

Code: Select all

oWord := ComObjActive("word.application")
#!1:: 
oword.Selection.Font.textcolor := 0
oWord.Selection.TypeText("♠")
return

#!2:: 
oword.Selection.Font.textcolor := 0
oWord.Selection.TypeText("♣")
return

#!3:: 
oword.Selection.Font.textcolor := 255
oWord.Selection.TypeText("♦")
oword.Selection.Font.textcolor := 0
return

#!4:: 
oword.Selection.Font.textcolor := 255
oWord.Selection.TypeText("♥")
oword.Selection.Font.textcolor := 0
return
♥ some text ⯁ some text ♣ some text ♠
20211204_153733.jpg
20211204_153733.jpg (10.29 KiB) Viewed 351 times
14.3 & 1.3.7

DavidPaul
Posts: 12
Joined: 04 Apr 2017, 18:28

Re: Inserting a coloured "character"

Post by DavidPaul » 05 Dec 2021, 04:59

mikeyww wrote:
04 Dec 2021, 17:23

Code: Select all

F2::paste("diamonds")
F3::paste("clubs")
F4::paste("spades")
F5::paste("hearts")

paste(suit) {
 Clipboard := "", setClipboardBitmap(A_ScriptDir "\" suit ".png")
 ClipWait, 0, WaitForAnyData := True
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the clipboard.
 Else Send ^v
}

setClipboardBitmap(Filename) {            ; By SKAN on D3AG @ tiny.cc/setclipboardbitmap
 Local  HBM := 0,  RES := 0,  OCB := 0,  CF_BITMAP := 2
 If ( HBM := LoadPicture(Filename) )
  If ( HBM := DllCall("CopyImage", "Ptr",HBM, "Int",0, "Int",0, "Int",0, "Int",8, "Ptr") )
   If ( OCB := DllCall("OpenClipboard", "Ptr",A_ScriptHwnd) )
    If DllCall("EmptyClipboard")
     RES := DllCall("SetClipboardData", "Int",CF_BITMAP, "Ptr",HBM)
 OCB := OCB ? DllCall("CloseClipboard")*0 : 0
 HBM := HBM ? DllCall("DeleteObject", "Ptr",HBM)*0 : 0
 Return !!RES
}
That works great, thanks. The only difficulty is that the symbol is too large for the text. Is there a way I could programmatically resize to match the fontsize?

DavidPaul
Posts: 12
Joined: 04 Apr 2017, 18:28

Re: Inserting a coloured "character"

Post by DavidPaul » 05 Dec 2021, 05:15

flyingDman wrote:
04 Dec 2021, 18:01
Or you can use ascii characters for these 4. Make sure you encode your script UTF-8 with BOM:

Code: Select all

oWord := ComObjActive("word.application")
#!1:: 
oword.Selection.Font.textcolor := 0
oWord.Selection.TypeText("♠")
return

#!2:: 
oword.Selection.Font.textcolor := 0
oWord.Selection.TypeText("♣")
return

#!3:: 
oword.Selection.Font.textcolor := 255
oWord.Selection.TypeText("♦")
oword.Selection.Font.textcolor := 0
return

#!4:: 
oword.Selection.Font.textcolor := 255
oWord.Selection.TypeText("♥")
oword.Selection.Font.textcolor := 0
return
♥ some text ⯁ some text ♣ some text ♠

20211204_153733.jpg
Thanks. My original post wasn't clear enough, I'm afraid. Your method works nicely from within Microsoft Word, but I'm actually trying to do this in a Google Docs environment. Can the method be modified for that?

Or, even better, you came up with a method elsewhere of pressing F12 to display special characters in a popup menu (I don't know how to link to it, sorry). Could that method be modified to include a coloured character. I suspect not, because I think that method used a character set that doesn't include the suits symbols.

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

Re: Inserting a coloured "character"

Post by mikeyww » 05 Dec 2021, 05:58

I was just using the image that you posted. I don't know much about Google Docs or how to get the font, but you can add a resizer. The forum has some posts & scripts about resizing. Here is one that you can try. viewtopic.php?t=2505

Post Reply

Return to “Ask for Help (v1)”